2 * Copyright 2012 Samsung Electronics Co., Ltd
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
8 * http://www.tizenopensource.org/license
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.
20 #include <glib/gprintf.h>
21 #include <email-api.h>
24 #include <ui-gadget-module.h>
26 #include "email-debug.h"
27 #include "email-utils.h"
28 #include "email-engine.h"
30 #define ACCOUNT_MIN -1
32 ui_gadget_h _g_mailbox_ug = NULL;
34 static guint _g_edb_ref_count = 0;
37 gboolean email_engine_initialize(void)
42 if (_g_edb_ref_count > 1) {
43 debug_log("already opened - EDB ref_count(%d)", _g_edb_ref_count);
49 debug_log("email_service_begin");
51 err = email_service_begin();
52 if (err != EMAIL_ERROR_NONE) {
53 debug_critical("fail to email_service_begin - err(%d)", err);
60 err = email_open_db();
61 if (err != EMAIL_ERROR_NONE) {
62 debug_critical("fail to open db - err(%d)", err);
69 void email_engine_finalize(void)
74 if (_g_edb_ref_count > 0) {
75 debug_log("remain EDB ref_count(%d)", _g_edb_ref_count);
81 err = email_close_db();
82 if (err != EMAIL_ERROR_NONE) {
83 debug_critical("fail to close db - err(%d)", err);
86 debug_log("email_service_end");
88 err = email_service_end();
89 if (err != EMAIL_ERROR_NONE) {
90 debug_critical("fail to email_service_end - err(%d)", err);
94 void email_engine_finalize_force(void)
100 err = email_close_db();
101 if (err != EMAIL_ERROR_NONE) {
102 debug_critical("fail to close db - err(%d)", err);
105 debug_log("email_service_end");
107 err = email_service_end();
108 if (err != EMAIL_ERROR_NONE) {
109 debug_critical("fail to email_service_begin - err(%d)", err);
113 gboolean email_engine_add_account(email_account_t *_account, int *account_id)
116 RETURN_VAL_IF_FAIL(_account != NULL, FALSE);
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);
126 debug_log("Succeed in adding account");
127 *account_id = _account->account_id;
128 debug_log("account id is %d", _account->account_id);
133 gboolean email_engine_add_account_with_validation(email_account_t *_account, int *account_id, int *handle, int *error_code)
136 RETURN_VAL_IF_FAIL(_account != NULL, FALSE);
140 err = email_add_account_with_validation(_account, handle);
141 if (error_code != NULL)
143 if (err != EMAIL_ERROR_NONE) {
144 debug_critical("Fail to Create Account with validation");
145 debug_critical("Error code(%d)", err);
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);
156 gboolean email_engine_update_account(gint account_id, email_account_t *_account)
159 RETURN_VAL_IF_FAIL(account_id != 0, FALSE);
160 RETURN_VAL_IF_FAIL(_account != NULL, FALSE);
164 err = email_update_account(account_id, _account);
165 if (err == EMAIL_ERROR_NONE) {
166 debug_log("Suceeded in email_update_account");
169 debug_critical("Failed to update account Err(%d)", err);
174 gboolean email_engine_update_account_with_validation(gint account_id, email_account_t *_account)
177 RETURN_VAL_IF_FAIL(account_id != 0, FALSE);
178 RETURN_VAL_IF_FAIL(_account != NULL, FALSE);
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");
187 debug_critical("Failed to update account with validation Err(%d)", err);
192 gboolean email_engine_delete_account(gint account_id)
195 RETURN_VAL_IF_FAIL(account_id != 0, FALSE);
199 err = email_delete_account(account_id);
200 if (err == EMAIL_ERROR_NONE) {
201 debug_log("Account is Successfully deleted");
204 debug_critical("Failed to delete account Err(%d)", err);
210 gboolean email_engine_get_account_list(int *count, email_account_t **_account_list)
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);
220 debug_log("valid account count :(%d)", *count);
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);
225 debug_log("Get All Account List");
230 gboolean email_engine_free_account_list(email_account_t **_account_list, int count)
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);
240 debug_log("Succeed in freeing account list");
245 gboolean email_engine_get_account_full_data(int acctid, email_account_t **account)
248 debug_log("email_engine_get_account_full_data. acctid:%d", acctid);
249 RETURN_VAL_IF_FAIL(acctid > ACCOUNT_MIN, FALSE);
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);
258 debug_log("Account name: %s", (*account)->account_name);
259 if ((*account)->options.signature)
260 debug_log("Signature: %s", (*account)->options.signature);
262 debug_critical("account is NULL");
269 gboolean email_engine_set_default_account(gint account_id)
272 RETURN_VAL_IF_FAIL(account_id > ACCOUNT_MIN, FALSE);
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);
281 debug_log("default account is set as account_id %d.", account_id);
286 gboolean email_engine_get_default_account(gint *account_id)
289 RETURN_VAL_IF_FAIL(account_id != NULL, FALSE);
291 email_account_t *_account = NULL;
295 err = email_load_default_account_id(account_id);
296 debug_log("email_load_default_account_id returns %d.", err);
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);
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);
315 if (_account == NULL) {
316 debug_log("account info is NULL");
320 *account_id = _account[0].account_id;
321 debug_log("account id (%d)", *account_id);
323 err = email_free_account(&_account, count);
324 if (err != EMAIL_ERROR_NONE) {
325 debug_critical("fail to free account - err(%d)", err);
329 RETURN_VAL_IF_FAIL((*account_id) > 0, FALSE);
330 email_engine_set_default_account(*account_id);
335 gboolean email_engine_sync_folder(gint account_id, int mailbox_id, int *handle)
339 debug_log("account id (%d)", account_id);
341 gboolean res = FALSE;
342 int email_handle = 0;
345 err = email_sync_header(account_id, mailbox_id, &email_handle);
346 debug_log("email_handle: %d", email_handle);
348 if (err != EMAIL_ERROR_NONE) {
349 debug_critical("fail to sync current folder - err (%d)", err);
355 if (handle != NULL) {
356 debug_log("email_handle for folder sync: %d", email_handle);
357 *handle = email_handle;
363 void email_engine_stop_working(gint account_id, int handle)
366 debug_log("account_id:%d, handle:%d", account_id, handle);
368 RETURN_IF_FAIL(account_id > ACCOUNT_MIN);
369 RETURN_IF_FAIL(handle != 0);
373 debug_log("handle (%d)", handle);
375 err = email_cancel_job(account_id, handle, EMAIL_CANCELED_BY_USER);
377 if (err != EMAIL_ERROR_NONE) {
378 debug_warning("fail to cancel job");
382 gboolean email_engine_check_seen_mail(gint account_id, gint mail_id)
385 RETURN_VAL_IF_FAIL(account_id > ACCOUNT_MIN, FALSE);
386 RETURN_VAL_IF_FAIL(mail_id > 0, FALSE);
390 email_mail_data_t *mail_info = NULL;
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);
398 if (mail_info == NULL) {
399 debug_critical("mail_info is @niL");
403 res = mail_info->flags_seen_field;
404 debug_log("flags_seen_field: %d", res);
406 err = email_free_mail_data(&mail_info, 1);
408 if (err != EMAIL_ERROR_NONE) {
409 debug_critical("fail to free mail info - err (%d)", err);
415 int email_engine_check_body_download(int mail_id)
418 RETURN_VAL_IF_FAIL(mail_id > 0, FALSE);
422 email_mail_data_t *mail_info = NULL;
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);
430 if (mail_info == NULL) {
431 debug_critical("mail_info is @niL");
435 res = mail_info->body_download_status;
436 debug_log("body_download_yn: %d", res);
438 err = email_free_mail_data(&mail_info, 1);
440 if (err != EMAIL_ERROR_NONE) {
441 debug_critical("fail to free mail info - err (%d)", err);
447 gboolean email_engine_body_download(gint account_id, gint mail_id, int *handle)
450 RETURN_VAL_IF_FAIL(account_id > ACCOUNT_MIN, FALSE);
451 RETURN_VAL_IF_FAIL(mail_id > 0, FALSE);
454 int email_handle = 0;
455 gboolean res = FALSE;
457 err = email_download_body(mail_id, 0, &email_handle);
459 if (err != EMAIL_ERROR_NONE) {
460 debug_warning("fail to download body - err (%d)", err);
464 if (handle != NULL) {
465 debug_log("email_handle for body download: %d", email_handle);
466 *handle = email_handle;
475 gboolean email_engine_attachment_download(gint account_id, gint mail_id, gint index, int *handle)
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);
483 int email_handle = 0;
484 gboolean res = FALSE;
486 err = email_download_attachment(mail_id, index, &email_handle);
488 if (err != EMAIL_ERROR_NONE) {
489 debug_warning("fail to download attachment - err (%d)", err);
493 if (handle != NULL) {
494 debug_log("email_handle for attachment download: %d", email_handle);
495 *handle = email_handle;
504 gboolean email_engine_delete_mail(gint account_id, int mailbox_id, gint mail_id, int sync)
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);
512 int mail_ids[1] = { 0 };
513 gboolean res = TRUE; /* MUST BE initialized TRUE. */
515 mail_ids[0] = mail_id;
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);
522 if (err != EMAIL_ERROR_NONE) {
523 debug_warning("failed to delete message - err (%d)", err);
530 gboolean email_engine_delete_all_mail(gint account_id, int mailbox_id, int sync)
533 RETURN_VAL_IF_FAIL(account_id > ACCOUNT_MIN, FALSE);
536 gboolean res = TRUE; /* MUST BE initialized TRUE. */
538 err = email_delete_all_mails_in_mailbox(mailbox_id, sync);
540 if (err != EMAIL_ERROR_NONE) {
541 debug_warning("failed to delete all message - err (%d)", err);
548 gboolean email_engine_move_mail(gint account_id, int mailbox_id, gint mail_id)
551 RETURN_VAL_IF_FAIL(account_id > ACCOUNT_MIN, FALSE);
552 RETURN_VAL_IF_FAIL(mail_id > 0, FALSE);
554 debug_log("account_id: %d", account_id);
555 debug_log("mail_id: %d", mail_id);
558 gboolean res = TRUE; /* MUST BE initialized TRUE. */
560 RETURN_VAL_IF_FAIL(mailbox_id > 0, FALSE);
562 int mail_ids[1] = { 0 };
563 mail_ids[0] = mail_id;
565 err = email_move_mail_to_mailbox(mail_ids, 1, mailbox_id);
567 if (err != EMAIL_ERROR_NONE) {
568 debug_warning("failed to move message - err (%d)", err);
575 gboolean email_engine_move_all_mail(gint account_id, int old_mailbox_id, int new_mailbox_id)
578 RETURN_VAL_IF_FAIL(account_id > ACCOUNT_MIN, FALSE);
581 gboolean res = TRUE; /* MUST BE initialized TRUE. */
583 err = email_move_all_mails_to_mailbox(old_mailbox_id, new_mailbox_id);
585 if (err != EMAIL_ERROR_NONE) {
586 debug_warning("failed to move all message - err (%d)", err);
593 gchar *email_engine_get_attachment_path(gint attach_id)
596 RETURN_VAL_IF_FAIL(attach_id > 0, FALSE);
599 email_attachment_data_t *attachments = NULL;
600 gchar *attachment_path = NULL;
602 err = email_get_attachment_data(attach_id, &attachments);
604 if (err != EMAIL_ERROR_NONE) {
605 debug_critical("fail to get attachment info - err (%d)", err);
609 if (attachments == NULL) {
610 debug_critical("attachments is @niL");
614 if (STR_VALID(attachments->attachment_path)) {
615 debug_log("attachment path (%s)", attachments->attachment_path);
616 attachment_path = g_strdup(attachments->attachment_path);
619 err = email_free_attachment_data(&attachments, 1);
621 if (err != EMAIL_ERROR_NONE) {
622 debug_warning("fail to free attachment info - err(%d)", err);
626 return attachment_path;
629 gboolean email_engine_get_account_info(gint account_id, EmailAccountInfo **account_info)
632 RETURN_VAL_IF_FAIL(account_id > ACCOUNT_MIN, FALSE);
634 (*account_info) = NULL;
636 EmailAccountInfo *info = (EmailAccountInfo *) calloc(1, sizeof(EmailAccountInfo));
639 debug_critical("failed to memory allocation");
643 email_account_t *account = NULL;
646 err = email_get_account(account_id, EMAIL_ACC_GET_OPT_FULL_DATA, &account);
648 if (err != EMAIL_ERROR_NONE) {
649 debug_critical("failed to get account info - err (%d)", err);
653 if (STR_VALID(account->account_name)) {
654 info->account_name = strdup(account->account_name);
657 if (STR_VALID(account->user_email_address)) {
658 info->email_address = strdup(account->user_email_address);
661 if (STR_VALID(account->user_display_name)) {
662 info->user_name = strdup(account->user_display_name);
665 if (STR_VALID(account->incoming_server_password)) {
666 info->password = strdup(account->incoming_server_password);
669 if (STR_VALID(account->outgoing_server_address)) {
670 info->smtp_address = strdup(account->outgoing_server_address);
673 if (STR_VALID(account->outgoing_server_user_name)) {
674 info->smtp_user_name = strdup(account->outgoing_server_user_name);
677 if (STR_VALID(account->outgoing_server_password)) {
678 info->smtp_password = strdup(account->outgoing_server_password);
681 if (STR_VALID(account->incoming_server_address)) {
682 info->receiving_address = strdup(account->incoming_server_address);
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;
694 err = email_free_account(&account, 1);
696 if (err != EMAIL_ERROR_NONE) {
697 debug_critical("failed to free account info - err (%d)", err);
700 (*account_info) = info;
709 err = email_free_account(&account, 1);
710 if (err != EMAIL_ERROR_NONE) {
711 debug_critical("failed to free account info - err (%d)", err);
717 void email_engine_free_account_info(EmailAccountInfo **account_info)
720 RETURN_IF_FAIL(*account_info != NULL);
722 EmailAccountInfo *info = (*account_info);
724 if (STR_VALID(info->account_name)) {
725 free(info->account_name);
726 info->account_name = NULL;
729 if (STR_VALID(info->email_address)) {
730 free(info->email_address);
731 info->email_address = NULL;
734 if (STR_VALID(info->user_name)) {
735 free(info->user_name);
736 info->user_name = NULL;
739 if (STR_VALID(info->password)) {
740 free(info->password);
741 info->password = NULL;
744 if (STR_VALID(info->receiving_address)) {
745 free(info->receiving_address);
746 info->receiving_address = NULL;
749 if (STR_VALID(info->smtp_address)) {
750 free(info->smtp_address);
751 info->smtp_address = NULL;
754 if (STR_VALID(info->smtp_user_name)) {
755 free(info->smtp_user_name);
756 info->smtp_user_name = NULL;
759 if (STR_VALID(info->smtp_password)) {
760 free(info->smtp_password);
761 info->smtp_password = NULL;
768 GList *email_engine_get_ca_mailbox_list_using_glist(int account_id)
772 email_mailbox_t *mailbox_list = NULL;
776 debug_log("account_id: %d", account_id);
777 debug_log("&mailbox_list: 0x%x", &mailbox_list);
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");
786 debug_log("Executing email_get_mailbox_list is ended.");
787 debug_log("count: %d", count);
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);
797 /* debug_log("mailbox_list[].name is %s", mailbox_list[i].mailbox_name); */
799 nameandalias->name = g_strdup(mailbox_list[i].mailbox_name);
800 nameandalias->mailbox_id = mailbox_list[i].mailbox_id;
802 if (mailbox_list[i].alias == NULL) {
803 debug_critical("alias is NULL");
804 nameandalias->alias = nameandalias->name;
806 /* debug_log("mailbox_list[].alias is %s", mailbox_list[i].alias); */
807 nameandalias->alias = g_strdup(mailbox_list[i].alias);
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;
815 ret = g_list_append(ret, (gpointer)nameandalias);
817 /* debug_log("mailbox name: %s", mailbox_list[i].mailbox_name); */
822 email_free_mailbox(&mailbox_list, count);
826 void email_engine_free_ca_mailbox_list_using_glist(GList **mailbox_list)
829 RETURN_IF_FAIL(mailbox_list != NULL);
830 RETURN_IF_FAIL(*mailbox_list != NULL);
832 GList *list = (GList *)(*mailbox_list);
833 int list_cnt = g_list_length(list);
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");
841 g_free(nameandalias->name);
842 g_free(nameandalias->alias);
846 *mailbox_list = NULL;
849 int email_engine_get_max_account_id(void)
853 email_account_t *account_list = NULL;
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);
860 debug_log("-- total account count : %d", count);
862 int max_account_id = 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);
870 email_free_account(&account_list, count);
873 return max_account_id;
877 int email_engine_get_count_account(void)
881 email_account_t *account_list = NULL;
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);
888 debug_log("-- total account count : %d", count);
890 email_free_account(&account_list, count);