e4673bbcddb8e3785dbf56c9b2ece7ded538b9ec
[apps/core/preloaded/email.git] / common / src / email-engine.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *    http://www.tizenopensource.org/license
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <ctype.h>
18 #include <string.h>
19 #include <glib.h>
20 #include <glib/gprintf.h>
21 #include <email-api.h>
22 #include <db-util.h>
23 #include <vconf.h>
24 #include <ui-gadget-module.h>
25
26 #include "email-debug.h"
27 #include "email-utils.h"
28 #include "email-engine.h"
29
30 #define ACCOUNT_MIN                                             -1
31
32 ui_gadget_h _g_mailbox_ug = NULL;
33
34 static guint _g_edb_ref_count = 0;
35
36
37 gboolean email_engine_initialize(void)
38 {
39         debug_log("");
40         ++_g_edb_ref_count;
41
42         if (_g_edb_ref_count > 1) {
43                 debug_log("already opened - EDB ref_count(%d)", _g_edb_ref_count);
44                 return TRUE;
45         }
46
47         int err = 0;
48
49         debug_log("email_service_begin");
50
51         err = email_service_begin();
52         if (err != EMAIL_ERROR_NONE) {
53                 debug_critical("fail to email_service_begin - err(%d)", err);
54                 /*
55                 g_assert(0);
56                 */
57                 return FALSE;
58         }
59
60         err = email_open_db();
61         if (err != EMAIL_ERROR_NONE) {
62                 debug_critical("fail to open db - err(%d)", err);
63                 return FALSE;
64         }
65
66         return TRUE;
67 }
68
69 void email_engine_finalize(void)
70 {
71         debug_log("");
72         --_g_edb_ref_count;
73
74         if (_g_edb_ref_count > 0) {
75                 debug_log("remain EDB ref_count(%d)", _g_edb_ref_count);
76                 return;
77         }
78
79         int err = 0;
80
81         err = email_close_db();
82         if (err != EMAIL_ERROR_NONE) {
83                 debug_critical("fail to close db - err(%d)", err);
84         }
85
86         debug_log("email_service_end");
87
88         err = email_service_end();
89         if (err != EMAIL_ERROR_NONE) {
90                 debug_critical("fail to email_service_end - err(%d)", err);
91         }
92 }
93
94 void email_engine_finalize_force(void)
95 {
96         debug_log("");
97
98         int err = 0;
99
100         err = email_close_db();
101         if (err != EMAIL_ERROR_NONE) {
102                 debug_critical("fail to close db - err(%d)", err);
103         }
104
105         debug_log("email_service_end");
106
107         err = email_service_end();
108         if (err != EMAIL_ERROR_NONE) {
109                 debug_critical("fail to email_service_begin - err(%d)", err);
110         }
111 }
112
113 gboolean email_engine_add_account(email_account_t *_account, int *account_id)
114 {
115         debug_log("");
116         RETURN_VAL_IF_FAIL(_account != NULL, FALSE);
117
118         int err = 0;
119
120         err = email_add_account(_account);
121         if (err != EMAIL_ERROR_NONE) {
122                 debug_critical("Fail to Create Account");
123                 debug_critical("Error code(%d)", err);
124                 return FALSE;
125         }
126         debug_log("Succeed in adding account");
127         *account_id = _account->account_id;
128         debug_log("account id is %d", _account->account_id);
129
130         return TRUE;
131 }
132
133 gboolean email_engine_add_account_with_validation(email_account_t *_account, int *account_id, int *handle, int *error_code)
134 {
135         debug_log("");
136         RETURN_VAL_IF_FAIL(_account != NULL, FALSE);
137
138         int err = 0;
139
140         err = email_add_account_with_validation(_account, handle);
141         if (error_code != NULL)
142                 *error_code = err;
143         if (err != EMAIL_ERROR_NONE) {
144                 debug_critical("Fail to Create Account with validation");
145                 debug_critical("Error code(%d)", err);
146                 return FALSE;
147         }
148
149         debug_log("Succeed in adding account with validation");
150         *account_id = _account->account_id;
151         debug_log("account id is %d", _account->account_id);
152
153         return TRUE;
154 }
155
156 gboolean email_engine_update_account(gint account_id, email_account_t *_account)
157 {
158         debug_log("");
159         RETURN_VAL_IF_FAIL(account_id != 0, FALSE);
160         RETURN_VAL_IF_FAIL(_account != NULL, FALSE);
161
162         int err = 0;
163
164         err = email_update_account(account_id, _account);
165         if (err == EMAIL_ERROR_NONE) {
166                 debug_log("Suceeded in email_update_account");
167                 return TRUE;
168         } else {
169                 debug_critical("Failed to update account Err(%d)", err);
170                 return FALSE;
171         }
172 }
173
174 gboolean email_engine_update_account_with_validation(gint account_id, email_account_t *_account)
175 {
176         debug_log("");
177         RETURN_VAL_IF_FAIL(account_id != 0, FALSE);
178         RETURN_VAL_IF_FAIL(_account != NULL, FALSE);
179
180         int err = 0;
181
182         err = email_update_account_with_validation(account_id, _account);
183         if (err == EMAIL_ERROR_NONE) {
184                 debug_log("Suceeded in email_update_account_with_validation");
185                 return TRUE;
186         } else {
187                 debug_critical("Failed to update account with validation Err(%d)", err);
188                 return FALSE;
189         }
190 }
191
192 gboolean email_engine_delete_account(gint account_id)
193 {
194         debug_log("");
195         RETURN_VAL_IF_FAIL(account_id != 0, FALSE);
196
197         int err = 0;
198
199         err = email_delete_account(account_id);
200         if (err == EMAIL_ERROR_NONE) {
201                 debug_log("Account is Successfully deleted");
202                 return TRUE;
203         } else {
204                 debug_critical("Failed to delete account Err(%d)", err);
205                 return FALSE;
206         }
207
208 }
209
210 gboolean email_engine_get_account_list(int *count, email_account_t **_account_list)
211 {
212         debug_log("");
213         int i, err = 0;
214
215         err = email_get_account_list(_account_list, count);
216         if (err != EMAIL_ERROR_NONE) {
217                 debug_critical("email_get_account_list error Err(%d)", err);
218                 return FALSE;
219         }
220         debug_log("valid account count :(%d)", *count);
221
222         for (i = 0; i < *count; i++) {
223                 debug_log("%2d) %-15s %-30s\n", (*_account_list)[i].account_id, (*_account_list)[i].account_name, (*_account_list)[i].user_email_address);
224         }
225         debug_log("Get All Account List");
226
227         return TRUE;
228 }
229
230 gboolean email_engine_free_account_list(email_account_t **_account_list, int count)
231 {
232         debug_log("");
233         int err = 0;
234
235         err = email_free_account(_account_list, count);
236         if (err != EMAIL_ERROR_NONE) {
237                 debug_critical("Fail to free account list Err(%d)", err);
238                 return FALSE;
239         } else {
240                 debug_log("Succeed in freeing account list");
241                 return TRUE;
242         }
243 }
244
245 gboolean email_engine_get_account_full_data(int acctid, email_account_t **account)
246 {
247         debug_log("");
248         debug_log("email_engine_get_account_full_data. acctid:%d", acctid);
249         RETURN_VAL_IF_FAIL(acctid > ACCOUNT_MIN, FALSE);
250         int err = 0;
251
252         err = email_get_account(acctid, EMAIL_ACC_GET_OPT_FULL_DATA, account);
253         if (err != EMAIL_ERROR_NONE) {
254                 debug_critical("email_get_account full data error Err(%d)", err);
255                 return FALSE;
256         }
257         if (*account) {
258                 debug_log("Account name: %s", (*account)->account_name);
259                 if ((*account)->options.signature)
260                         debug_log("Signature: %s", (*account)->options.signature);
261         } else {
262                 debug_critical("account is NULL");
263                 return FALSE;
264         }
265
266         return TRUE;
267 }
268
269 gboolean email_engine_set_default_account(gint account_id)
270 {
271         debug_log("");
272         RETURN_VAL_IF_FAIL(account_id > ACCOUNT_MIN, FALSE);
273         int err = 0;
274
275         err = email_save_default_account_id(account_id);
276         debug_log("email_save_default_account_id returns %d.", err);
277         if (err != EMAIL_ERROR_NONE) {
278                 debug_critical("email_save_default_account_id: Err(%d)", err);
279                 return FALSE;
280         } else {
281                 debug_log("default account is set as account_id %d.", account_id);
282                 return TRUE;
283         }
284 }
285
286 gboolean email_engine_get_default_account(gint *account_id)
287 {
288         debug_log("");
289         RETURN_VAL_IF_FAIL(account_id != NULL, FALSE);
290
291         email_account_t *_account = NULL;
292         int count = 0;
293         int err = 0;
294
295         err = email_load_default_account_id(account_id);
296         debug_log("email_load_default_account_id returns %d.", err);
297
298         /* if account_id is default account, then check account_id whether it is valid or not */
299         if (err == EMAIL_ERROR_NONE) {
300                 debug_log("default account id is %d.", *account_id);
301                 if (email_get_account(*account_id, EMAIL_ACC_GET_OPT_DEFAULT, &_account) == EMAIL_ERROR_NONE) {
302                         email_free_account(&_account, 1);
303                         return TRUE;
304                 }
305         }
306
307         /* if slp_ret have no value or account id is not valid */
308         err = email_get_account_list(&_account, &count);
309         if (err != EMAIL_ERROR_NONE) {
310                 debug_critical("fail to get account list - err(%d)", err);
311                 return FALSE;
312         }
313
314         /* no account */
315         if (_account == NULL) {
316                 debug_log("account info is NULL");
317                 return FALSE;
318         }
319
320         *account_id = _account[0].account_id;
321         debug_log("account id (%d)", *account_id);
322
323         err = email_free_account(&_account, count);
324         if (err != EMAIL_ERROR_NONE) {
325                 debug_critical("fail to free account - err(%d)", err);
326                 return FALSE;
327         }
328
329         RETURN_VAL_IF_FAIL((*account_id) > 0, FALSE);
330         email_engine_set_default_account(*account_id);
331
332         return TRUE;
333 }
334
335 gboolean email_engine_sync_folder(gint account_id, int mailbox_id, int *handle)
336 {
337         debug_log("");
338
339         debug_log("account id (%d)", account_id);
340
341         gboolean res = FALSE;
342         int email_handle = 0;
343         int err = 0;
344
345         err = email_sync_header(account_id, mailbox_id, &email_handle);
346         debug_log("email_handle: %d", email_handle);
347
348         if (err != EMAIL_ERROR_NONE) {
349                 debug_critical("fail to sync current folder - err (%d)", err);
350                 res = FALSE;
351         } else {
352                 res = TRUE;
353         }
354
355         if (handle != NULL) {
356                 debug_log("email_handle for folder sync: %d", email_handle);
357                 *handle = email_handle;
358         }
359
360         return res;
361 }
362
363 void email_engine_stop_working(gint account_id, int handle)
364 {
365         debug_log("");
366         debug_log("account_id:%d, handle:%d", account_id, handle);
367
368         RETURN_IF_FAIL(account_id > ACCOUNT_MIN);
369         RETURN_IF_FAIL(handle != 0);
370
371         int err = 0;
372
373         debug_log("handle (%d)", handle);
374
375         err = email_cancel_job(account_id, handle, EMAIL_CANCELED_BY_USER);
376
377         if (err != EMAIL_ERROR_NONE) {
378                 debug_warning("fail to cancel job");
379         }
380 }
381
382 gboolean email_engine_check_seen_mail(gint account_id, gint mail_id)
383 {
384         debug_log("");
385         RETURN_VAL_IF_FAIL(account_id > ACCOUNT_MIN, FALSE);
386         RETURN_VAL_IF_FAIL(mail_id > 0, FALSE);
387
388         int res = 0;
389         int err = 0;
390         email_mail_data_t *mail_info = NULL;
391
392         if ((err = email_get_mail_data(mail_id, &mail_info)) != EMAIL_ERROR_NONE) {
393                 debug_log("fail to get mail data - err (%d)", err);
394                 email_free_mail_data(&mail_info, 1);
395                 return 0;
396         }
397
398         if (mail_info == NULL) {
399                 debug_critical("mail_info is @niL");
400                 return 0;
401         }
402
403         res = mail_info->flags_seen_field;
404         debug_log("flags_seen_field: %d", res);
405
406         err = email_free_mail_data(&mail_info, 1);
407
408         if (err != EMAIL_ERROR_NONE) {
409                 debug_critical("fail to free mail info - err (%d)", err);
410         }
411
412         return res;
413 }
414
415 int email_engine_check_body_download(int mail_id)
416 {
417         debug_log("");
418         RETURN_VAL_IF_FAIL(mail_id > 0, FALSE);
419
420         int res = 0;
421         int err = 0;
422         email_mail_data_t *mail_info = NULL;
423
424         if ((err = email_get_mail_data(mail_id, &mail_info)) != EMAIL_ERROR_NONE) {
425                 debug_log("fail to get mail data - err (%d)", err);
426                 email_free_mail_data(&mail_info, 1);
427                 return 0;
428         }
429
430         if (mail_info == NULL) {
431                 debug_critical("mail_info is @niL");
432                 return 0;
433         }
434
435         res = mail_info->body_download_status;
436         debug_log("body_download_yn: %d", res);
437
438         err = email_free_mail_data(&mail_info, 1);
439
440         if (err != EMAIL_ERROR_NONE) {
441                 debug_critical("fail to free mail info - err (%d)", err);
442         }
443
444         return res;
445 }
446
447 gboolean email_engine_body_download(gint account_id, gint mail_id, int *handle)
448 {
449         debug_log("");
450         RETURN_VAL_IF_FAIL(account_id > ACCOUNT_MIN, FALSE);
451         RETURN_VAL_IF_FAIL(mail_id > 0, FALSE);
452
453         int err = 0;
454         int email_handle = 0;
455         gboolean res = FALSE;
456
457         err = email_download_body(mail_id, 0, &email_handle);
458
459         if (err != EMAIL_ERROR_NONE) {
460                 debug_warning("fail to download body - err (%d)", err);
461                 goto error;
462         }
463
464         if (handle != NULL) {
465                 debug_log("email_handle for body download: %d", email_handle);
466                 *handle = email_handle;
467         }
468
469         res = TRUE;
470
471  error:
472         return res;
473 }
474
475 gboolean email_engine_attachment_download(gint account_id, gint mail_id, gint index, int *handle)
476 {
477         debug_log("");
478         RETURN_VAL_IF_FAIL(account_id > ACCOUNT_MIN, FALSE);
479         RETURN_VAL_IF_FAIL(mail_id > 0, FALSE);
480         RETURN_VAL_IF_FAIL(index > 0, FALSE);
481
482         int err = 0;
483         int email_handle = 0;
484         gboolean res = FALSE;
485
486         err = email_download_attachment(mail_id, index, &email_handle);
487
488         if (err != EMAIL_ERROR_NONE) {
489                 debug_warning("fail to download attachment - err (%d)", err);
490                 goto error;
491         }
492
493         if (handle != NULL) {
494                 debug_log("email_handle for attachment download: %d", email_handle);
495                 *handle = email_handle;
496         }
497
498         res = TRUE;
499
500  error:
501         return res;
502 }
503
504 gboolean email_engine_delete_mail(gint account_id, int mailbox_id, gint mail_id, int sync)
505 {
506         debug_log("");
507         RETURN_VAL_IF_FAIL(account_id > ACCOUNT_MIN, FALSE);
508         RETURN_VAL_IF_FAIL(mailbox_id > 0, FALSE);
509         RETURN_VAL_IF_FAIL(mail_id > 0, FALSE);
510
511         int err = 0;
512         int mail_ids[1] = { 0 };
513         gboolean res = TRUE;    /* MUST BE initialized TRUE. */
514
515         mail_ids[0] = mail_id;
516
517         debug_log("account_id : %d", account_id);
518         debug_log("mail_ids[0] : %d", mail_ids[0]);
519         debug_log("sync : %d", sync);
520         err = email_delete_mail(mailbox_id, mail_ids, 1, sync);
521
522         if (err != EMAIL_ERROR_NONE) {
523                 debug_warning("failed to delete message - err (%d)", err);
524                 res = FALSE;
525         }
526
527         return res;
528 }
529
530 gboolean email_engine_delete_all_mail(gint account_id, int mailbox_id, int sync)
531 {
532         debug_log("");
533         RETURN_VAL_IF_FAIL(account_id > ACCOUNT_MIN, FALSE);
534
535         int err = 0;
536         gboolean res = TRUE;    /* MUST BE initialized TRUE. */
537
538         err = email_delete_all_mails_in_mailbox(mailbox_id, sync);
539
540         if (err != EMAIL_ERROR_NONE) {
541                 debug_warning("failed to delete all message - err (%d)", err);
542                 res = FALSE;
543         }
544
545         return res;
546 }
547
548 gboolean email_engine_move_mail(gint account_id, int mailbox_id, gint mail_id)
549 {
550         debug_log("");
551         RETURN_VAL_IF_FAIL(account_id > ACCOUNT_MIN, FALSE);
552         RETURN_VAL_IF_FAIL(mail_id > 0, FALSE);
553
554         debug_log("account_id: %d", account_id);
555         debug_log("mail_id: %d", mail_id);
556
557         int err = 0;
558         gboolean res = TRUE;    /* MUST BE initialized TRUE. */
559
560         RETURN_VAL_IF_FAIL(mailbox_id > 0, FALSE);
561
562         int mail_ids[1] = { 0 };
563         mail_ids[0] = mail_id;
564
565         err = email_move_mail_to_mailbox(mail_ids, 1, mailbox_id);
566
567         if (err != EMAIL_ERROR_NONE) {
568                 debug_warning("failed to move message - err (%d)", err);
569                 res = FALSE;
570         }
571
572         return res;
573 }
574
575 gboolean email_engine_move_all_mail(gint account_id, int old_mailbox_id, int new_mailbox_id)
576 {
577         debug_log("");
578         RETURN_VAL_IF_FAIL(account_id > ACCOUNT_MIN, FALSE);
579
580         int err = 0;
581         gboolean res = TRUE;    /* MUST BE initialized TRUE. */
582
583         err = email_move_all_mails_to_mailbox(old_mailbox_id, new_mailbox_id);
584
585         if (err != EMAIL_ERROR_NONE) {
586                 debug_warning("failed to move all message - err (%d)", err);
587                 res = FALSE;
588         }
589
590         return res;
591 }
592
593 gchar *email_engine_get_attachment_path(gint attach_id)
594 {
595         debug_log("");
596         RETURN_VAL_IF_FAIL(attach_id > 0, FALSE);
597
598         int err = 0;
599         email_attachment_data_t *attachments = NULL;
600         gchar *attachment_path = NULL;
601
602         err = email_get_attachment_data(attach_id, &attachments);
603
604         if (err != EMAIL_ERROR_NONE) {
605                 debug_critical("fail to get attachment info - err (%d)", err);
606                 goto error;
607         }
608
609         if (attachments == NULL) {
610                 debug_critical("attachments is @niL");
611                 goto error;
612         }
613
614         if (STR_VALID(attachments->attachment_path)) {
615                 debug_log("attachment path (%s)", attachments->attachment_path);
616                 attachment_path = g_strdup(attachments->attachment_path);
617         }
618
619         err = email_free_attachment_data(&attachments, 1);
620
621         if (err != EMAIL_ERROR_NONE) {
622                 debug_warning("fail to free attachment info - err(%d)", err);
623         }
624
625  error:
626         return attachment_path;
627 }
628
629 gboolean email_engine_get_account_info(gint account_id, EmailAccountInfo **account_info)
630 {
631         debug_log("");
632         RETURN_VAL_IF_FAIL(account_id > ACCOUNT_MIN, FALSE);
633
634         (*account_info) = NULL;
635
636         EmailAccountInfo *info = (EmailAccountInfo *) calloc(1, sizeof(EmailAccountInfo));
637
638         if (info == NULL) {
639                 debug_critical("failed to memory allocation");
640                 return FALSE;
641         }
642
643         email_account_t *account = NULL;
644         int err = 0;
645
646         err = email_get_account(account_id, EMAIL_ACC_GET_OPT_FULL_DATA, &account);
647
648         if (err != EMAIL_ERROR_NONE) {
649                 debug_critical("failed to get account info - err (%d)", err);
650                 goto error;
651         }
652
653         if (STR_VALID(account->account_name)) {
654                 info->account_name = strdup(account->account_name);
655         }
656
657         if (STR_VALID(account->user_email_address)) {
658                 info->email_address = strdup(account->user_email_address);
659         }
660
661         if (STR_VALID(account->user_display_name)) {
662                 info->user_name = strdup(account->user_display_name);
663         }
664
665         if (STR_VALID(account->incoming_server_password)) {
666                 info->password = strdup(account->incoming_server_password);
667         }
668
669         if (STR_VALID(account->outgoing_server_address)) {
670                 info->smtp_address = strdup(account->outgoing_server_address);
671         }
672
673         if (STR_VALID(account->outgoing_server_user_name)) {
674                 info->smtp_user_name = strdup(account->outgoing_server_user_name);
675         }
676
677         if (STR_VALID(account->outgoing_server_password)) {
678                 info->smtp_password = strdup(account->outgoing_server_password);
679         }
680
681         if (STR_VALID(account->incoming_server_address)) {
682                 info->receiving_address = strdup(account->incoming_server_address);
683         }
684
685         info->smtp_auth = account->outgoing_server_need_authentication;
686         info->smtp_port = account->outgoing_server_port_number;
687         info->receiving_port = account->incoming_server_port_number;
688         info->receiving_type = account->incoming_server_type;
689         info->same_as = account->outgoing_server_use_same_authenticator;
690         info->smtp_ssl = account->outgoing_server_secure_connection;
691         info->receiving_ssl = account->incoming_server_secure_connection;
692         info->download_mode = account->auto_download_size;
693
694         err = email_free_account(&account, 1);
695
696         if (err != EMAIL_ERROR_NONE) {
697                 debug_critical("failed to free account info - err (%d)", err);
698         }
699
700         (*account_info) = info;
701
702         return TRUE;
703
704  error:
705         if (info) {
706                 free(info);
707         }
708         if (account) {
709                 err = email_free_account(&account, 1);
710                 if (err != EMAIL_ERROR_NONE) {
711                         debug_critical("failed to free account info - err (%d)", err);
712                 }
713         }
714         return FALSE;
715 }
716
717 void email_engine_free_account_info(EmailAccountInfo **account_info)
718 {
719         debug_log("");
720         RETURN_IF_FAIL(*account_info != NULL);
721
722         EmailAccountInfo *info = (*account_info);
723
724         if (STR_VALID(info->account_name)) {
725                 free(info->account_name);
726                 info->account_name = NULL;
727         }
728
729         if (STR_VALID(info->email_address)) {
730                 free(info->email_address);
731                 info->email_address = NULL;
732         }
733
734         if (STR_VALID(info->user_name)) {
735                 free(info->user_name);
736                 info->user_name = NULL;
737         }
738
739         if (STR_VALID(info->password)) {
740                 free(info->password);
741                 info->password = NULL;
742         }
743
744         if (STR_VALID(info->receiving_address)) {
745                 free(info->receiving_address);
746                 info->receiving_address = NULL;
747         }
748
749         if (STR_VALID(info->smtp_address)) {
750                 free(info->smtp_address);
751                 info->smtp_address = NULL;
752         }
753
754         if (STR_VALID(info->smtp_user_name)) {
755                 free(info->smtp_user_name);
756                 info->smtp_user_name = NULL;
757         }
758
759         if (STR_VALID(info->smtp_password)) {
760                 free(info->smtp_password);
761                 info->smtp_password = NULL;
762         }
763
764         free(info);
765         info = NULL;
766 }
767
768 GList *email_engine_get_ca_mailbox_list_using_glist(int account_id)
769 {
770         debug_log("");
771         int i, count = 0;
772         email_mailbox_t *mailbox_list = NULL;
773         GList *ret = NULL;
774         int err = 0;
775         /*
776         debug_log("account_id: %d", account_id);
777         debug_log("&mailbox_list: 0x%x", &mailbox_list);
778         */
779
780         err = email_get_mailbox_list_ex(account_id, -1, 1, &mailbox_list, &count);
781         if (err != EMAIL_ERROR_NONE) {
782                 debug_critical("email_get_mailbox_list return error");
783                 goto finally;
784         }
785         /*
786         debug_log("Executing email_get_mailbox_list is ended.");
787         debug_log("count: %d", count);
788         */
789
790         for (i = 0; i < count; i++) {
791                 EmailMailboxNameAndAlias *nameandalias = calloc(1, sizeof(EmailMailboxNameAndAlias));
792                 if (mailbox_list[i].mailbox_name == NULL) {
793                         debug_critical("mailbox_list[%d].name is null", i);
794                         free(nameandalias);
795                         continue;
796                 } else {
797                         /* debug_log("mailbox_list[].name is %s", mailbox_list[i].mailbox_name); */
798                 }
799                 nameandalias->name = g_strdup(mailbox_list[i].mailbox_name);
800                 nameandalias->mailbox_id = mailbox_list[i].mailbox_id;
801
802                 if (mailbox_list[i].alias == NULL) {
803                         debug_critical("alias is NULL");
804                         nameandalias->alias = nameandalias->name;
805                 } else {
806                         /* debug_log("mailbox_list[].alias is %s", mailbox_list[i].alias); */
807                         nameandalias->alias = g_strdup(mailbox_list[i].alias);
808                 }
809
810                 nameandalias->mailbox_type = mailbox_list[i].mailbox_type;
811                 nameandalias->unread_count = mailbox_list[i].unread_count;
812                 nameandalias->total_mail_count_on_local = mailbox_list[i].total_mail_count_on_local;
813                 nameandalias->total_mail_count_on_server = mailbox_list[i].total_mail_count_on_server;
814
815                 ret = g_list_append(ret, (gpointer)nameandalias);
816
817                 /* debug_log("mailbox name: %s", mailbox_list[i].mailbox_name); */
818         }
819
820  finally:
821
822         email_free_mailbox(&mailbox_list, count);
823         return ret;
824 }
825
826 void email_engine_free_ca_mailbox_list_using_glist(GList **mailbox_list)
827 {
828         debug_log("");
829         RETURN_IF_FAIL(mailbox_list != NULL);
830         RETURN_IF_FAIL(*mailbox_list != NULL);
831
832         GList *list = (GList *)(*mailbox_list);
833         int list_cnt = g_list_length(list);
834         int i = 0;
835
836         for (i = 0; i < list_cnt; i++) {
837                 EmailMailboxNameAndAlias *nameandalias = (EmailMailboxNameAndAlias *) g_list_nth_data(list, i);
838                 if (nameandalias == NULL) {
839                         debug_warning("nameandalias is NULL");
840                 } else {
841                         g_free(nameandalias->name);
842                         g_free(nameandalias->alias);
843                 }
844         }
845         g_list_free(list);
846         *mailbox_list = NULL;
847 }
848
849 int email_engine_get_max_account_id(void)
850 {
851         debug_enter();
852
853         email_account_t *account_list = NULL;
854         int count = 0;
855         int e = email_get_account_list(&account_list, &count);
856         if (e != EMAIL_ERROR_NONE) {
857                 debug_critical("email_get_account_list - err(%d)", e);
858                 return -1;
859         }
860         debug_log("-- total account count : %d", count);
861
862         int max_account_id = 0;
863         int i = 0;
864         for (i = 0; i < count; i++) {
865                 max_account_id = (account_list[i].account_id > max_account_id) ?
866                                                                         account_list[i].account_id : max_account_id;
867                 debug_log("%2d) %-15s %-30s\n", account_list[i].account_id, account_list[i].account_name,
868                                                                                 account_list[i].user_email_address);
869         }
870         email_free_account(&account_list, count);
871
872         debug_leave();
873         return max_account_id;
874 }
875
876
877 int email_engine_get_count_account(void)
878 {
879         debug_enter();
880
881         email_account_t *account_list = NULL;
882         int count = 0;
883         int e = email_get_account_list(&account_list, &count);
884         if (e != EMAIL_ERROR_NONE) {
885                 debug_critical("email_get_account_list - err(%d)", e);
886                 return -1;
887         }
888         debug_log("-- total account count : %d", count);
889
890         email_free_account(&account_list, count);
891
892         debug_leave();
893         return count;
894 }
895
896
897 /* EOF */