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