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);
57 err = email_open_db();
58 if (err != EMAIL_ERROR_NONE) {
59 debug_critical("fail to open db - err(%d)", err);
66 void email_engine_finalize(void)
71 if (_g_edb_ref_count > 0) {
72 debug_log("remain EDB ref_count(%d)", _g_edb_ref_count);
78 err = email_close_db();
79 if (err != EMAIL_ERROR_NONE) {
80 debug_critical("fail to close db - err(%d)", err);
83 debug_log("email_service_end");
85 err = email_service_end();
86 if (err != EMAIL_ERROR_NONE) {
87 debug_critical("fail to email_service_end - err(%d)", err);
91 void email_engine_finalize_force(void)
97 err = email_close_db();
98 if (err != EMAIL_ERROR_NONE) {
99 debug_critical("fail to close db - err(%d)", err);
102 debug_log("email_service_end");
104 err = email_service_end();
105 if (err != EMAIL_ERROR_NONE) {
106 debug_critical("fail to email_service_begin - err(%d)", err);
110 gboolean email_engine_add_account(email_account_t *_account, int *account_id)
113 RETURN_VAL_IF_FAIL(_account != NULL, FALSE);
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);
123 debug_log("Succeed in adding account");
124 *account_id = _account->account_id;
125 debug_log("account id is %d", _account->account_id);
130 gboolean email_engine_add_account_with_validation(email_account_t *_account, int *account_id, int *handle, int *error_code)
133 RETURN_VAL_IF_FAIL(_account != NULL, FALSE);
137 err = email_add_account_with_validation(_account, handle);
138 if (error_code != NULL)
140 if (err != EMAIL_ERROR_NONE) {
141 debug_critical("Fail to Create Account with validation");
142 debug_critical("Error code(%d)", err);
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);
153 gboolean email_engine_update_account(gint account_id, email_account_t *_account)
156 RETURN_VAL_IF_FAIL(account_id != 0, FALSE);
157 RETURN_VAL_IF_FAIL(_account != NULL, FALSE);
161 err = email_update_account(account_id, _account);
162 if (err == EMAIL_ERROR_NONE) {
163 debug_log("Suceeded in email_update_account");
166 debug_critical("Failed to update account Err(%d)", err);
171 gboolean email_engine_update_account_with_validation(gint account_id, email_account_t *_account)
174 RETURN_VAL_IF_FAIL(account_id != 0, FALSE);
175 RETURN_VAL_IF_FAIL(_account != NULL, FALSE);
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");
184 debug_critical("Failed to update account with validation Err(%d)", err);
189 gboolean email_engine_delete_account(gint account_id)
192 RETURN_VAL_IF_FAIL(account_id != 0, FALSE);
196 err = email_delete_account(account_id);
197 if (err == EMAIL_ERROR_NONE) {
198 debug_log("Account is Successfully deleted");
201 debug_critical("Failed to delete account Err(%d)", err);
207 gboolean email_engine_get_account_list(int *count, email_account_t **_account_list)
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);
217 debug_log("valid account count :(%d)", *count);
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);
222 debug_log("Get All Account List");
227 gboolean email_engine_free_account_list(email_account_t **_account_list, int count)
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);
237 debug_log("Succeed in freeing account list");
242 gboolean email_engine_get_account_full_data(int acctid, email_account_t **account)
245 debug_log("email_engine_get_account_full_data. acctid:%d", acctid);
246 RETURN_VAL_IF_FAIL(acctid > ACCOUNT_MIN, FALSE);
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);
255 debug_log("Account name: %s", (*account)->account_name);
256 if ((*account)->options.signature)
257 debug_log("Signature: %s", (*account)->options.signature);
259 debug_critical("account is NULL");
266 gboolean email_engine_set_default_account(gint account_id)
269 RETURN_VAL_IF_FAIL(account_id > ACCOUNT_MIN, FALSE);
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);
278 debug_log("default account is set as account_id %d.", account_id);
283 gboolean email_engine_get_default_account(gint *account_id)
286 RETURN_VAL_IF_FAIL(account_id != NULL, FALSE);
288 email_account_t *_account = NULL;
292 err = email_load_default_account_id(account_id);
293 debug_log("email_load_default_account_id returns %d.", err);
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);
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);
312 if (_account == NULL) {
313 debug_log("account info is NULL");
317 *account_id = _account[0].account_id;
318 debug_log("account id (%d)", *account_id);
320 err = email_free_account(&_account, count);
321 if (err != EMAIL_ERROR_NONE) {
322 debug_critical("fail to free account - err(%d)", err);
326 RETURN_VAL_IF_FAIL((*account_id) > 0, FALSE);
327 email_engine_set_default_account(*account_id);
332 gboolean email_engine_sync_folder(gint account_id, int mailbox_id, int *handle)
336 debug_log("account id (%d)", account_id);
338 gboolean res = FALSE;
339 int email_handle = 0;
342 err = email_sync_header(account_id, mailbox_id, &email_handle);
343 debug_log("email_handle: %d", email_handle);
345 if (err != EMAIL_ERROR_NONE) {
346 debug_critical("fail to sync current folder - err (%d)", err);
352 if (handle != NULL) {
353 debug_log("email_handle for folder sync: %d", email_handle);
354 *handle = email_handle;
360 void email_engine_stop_working(gint account_id, int handle)
363 debug_log("account_id:%d, handle:%d", account_id, handle);
365 RETURN_IF_FAIL(account_id > ACCOUNT_MIN);
366 RETURN_IF_FAIL(handle != 0);
370 debug_log("handle (%d)", handle);
372 err = email_cancel_job(account_id, handle, EMAIL_CANCELED_BY_USER);
374 if (err != EMAIL_ERROR_NONE) {
375 debug_warning("fail to cancel job");
379 gboolean email_engine_check_seen_mail(gint account_id, gint mail_id)
382 RETURN_VAL_IF_FAIL(account_id > ACCOUNT_MIN, FALSE);
383 RETURN_VAL_IF_FAIL(mail_id > 0, FALSE);
387 email_mail_data_t *mail_info = NULL;
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);
395 if (mail_info == NULL) {
396 debug_critical("mail_info is @niL");
400 res = mail_info->flags_seen_field;
401 debug_log("flags_seen_field: %d", res);
403 err = email_free_mail_data(&mail_info, 1);
405 if (err != EMAIL_ERROR_NONE) {
406 debug_critical("fail to free mail info - err (%d)", err);
412 int email_engine_check_body_download(int mail_id)
415 RETURN_VAL_IF_FAIL(mail_id > 0, FALSE);
419 email_mail_data_t *mail_info = NULL;
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);
427 if (mail_info == NULL) {
428 debug_critical("mail_info is @niL");
432 res = mail_info->body_download_status;
433 debug_log("body_download_yn: %d", res);
435 err = email_free_mail_data(&mail_info, 1);
437 if (err != EMAIL_ERROR_NONE) {
438 debug_critical("fail to free mail info - err (%d)", err);
444 gboolean email_engine_body_download(gint account_id, gint mail_id, int *handle)
447 RETURN_VAL_IF_FAIL(account_id > ACCOUNT_MIN, FALSE);
448 RETURN_VAL_IF_FAIL(mail_id > 0, FALSE);
451 int email_handle = 0;
452 gboolean res = FALSE;
454 err = email_download_body(mail_id, 0, &email_handle);
456 if (err != EMAIL_ERROR_NONE) {
457 debug_warning("fail to download body - err (%d)", err);
461 if (handle != NULL) {
462 debug_log("email_handle for body download: %d", email_handle);
463 *handle = email_handle;
472 gboolean email_engine_attachment_download(gint account_id, gint mail_id, gint index, int *handle)
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);
480 int email_handle = 0;
481 gboolean res = FALSE;
483 err = email_download_attachment(mail_id, index, &email_handle);
485 if (err != EMAIL_ERROR_NONE) {
486 debug_warning("fail to download attachment - err (%d)", err);
490 if (handle != NULL) {
491 debug_log("email_handle for attachment download: %d", email_handle);
492 *handle = email_handle;
501 gboolean email_engine_delete_mail(gint account_id, int mailbox_id, gint mail_id, int sync)
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);
509 int mail_ids[1] = { 0 };
510 gboolean res = TRUE; /* MUST BE initialized TRUE. */
512 mail_ids[0] = mail_id;
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);
519 if (err != EMAIL_ERROR_NONE) {
520 debug_warning("failed to delete message - err (%d)", err);
527 gboolean email_engine_delete_all_mail(gint account_id, int mailbox_id, int sync)
530 RETURN_VAL_IF_FAIL(account_id > ACCOUNT_MIN, FALSE);
533 gboolean res = TRUE; /* MUST BE initialized TRUE. */
535 err = email_delete_all_mails_in_mailbox(mailbox_id, sync);
537 if (err != EMAIL_ERROR_NONE) {
538 debug_warning("failed to delete all message - err (%d)", err);
545 gboolean email_engine_move_mail(gint account_id, int mailbox_id, gint mail_id)
548 RETURN_VAL_IF_FAIL(account_id > ACCOUNT_MIN, FALSE);
549 RETURN_VAL_IF_FAIL(mail_id > 0, FALSE);
551 debug_log("account_id: %d", account_id);
552 debug_log("mail_id: %d", mail_id);
555 gboolean res = TRUE; /* MUST BE initialized TRUE. */
557 RETURN_VAL_IF_FAIL(mailbox_id > 0, FALSE);
559 int mail_ids[1] = { 0 };
560 mail_ids[0] = mail_id;
562 err = email_move_mail_to_mailbox(mail_ids, 1, mailbox_id);
564 if (err != EMAIL_ERROR_NONE) {
565 debug_warning("failed to move message - err (%d)", err);
572 gboolean email_engine_move_all_mail(gint account_id, int old_mailbox_id, int new_mailbox_id)
575 RETURN_VAL_IF_FAIL(account_id > ACCOUNT_MIN, FALSE);
578 gboolean res = TRUE; /* MUST BE initialized TRUE. */
580 err = email_move_all_mails_to_mailbox(old_mailbox_id, new_mailbox_id);
582 if (err != EMAIL_ERROR_NONE) {
583 debug_warning("failed to move all message - err (%d)", err);
590 gchar *email_engine_get_attachment_path(gint attach_id)
593 RETURN_VAL_IF_FAIL(attach_id > 0, FALSE);
596 email_attachment_data_t *attachments = NULL;
597 gchar *attachment_path = NULL;
599 err = email_get_attachment_data(attach_id, &attachments);
601 if (err != EMAIL_ERROR_NONE) {
602 debug_critical("fail to get attachment info - err (%d)", err);
606 if (attachments == NULL) {
607 debug_critical("attachments is @niL");
611 if (STR_VALID(attachments->attachment_path)) {
612 debug_log("attachment path (%s)", attachments->attachment_path);
613 attachment_path = g_strdup(attachments->attachment_path);
616 err = email_free_attachment_data(&attachments, 1);
618 if (err != EMAIL_ERROR_NONE) {
619 debug_warning("fail to free attachment info - err(%d)", err);
623 return attachment_path;
626 gboolean email_engine_get_account_info(gint account_id, EmailAccountInfo **account_info)
629 RETURN_VAL_IF_FAIL(account_id > ACCOUNT_MIN, FALSE);
631 (*account_info) = NULL;
633 EmailAccountInfo *info = (EmailAccountInfo *) calloc(1, sizeof(EmailAccountInfo));
636 debug_critical("failed to memory allocation");
640 email_account_t *account = NULL;
643 err = email_get_account(account_id, EMAIL_ACC_GET_OPT_FULL_DATA, &account);
645 if (err != EMAIL_ERROR_NONE) {
646 debug_critical("failed to get account info - err (%d)", err);
650 if (STR_VALID(account->account_name)) {
651 info->account_name = strdup(account->account_name);
654 if (STR_VALID(account->user_email_address)) {
655 info->email_address = strdup(account->user_email_address);
658 if (STR_VALID(account->user_display_name)) {
659 info->user_name = strdup(account->user_display_name);
662 if (STR_VALID(account->incoming_server_password)) {
663 info->password = strdup(account->incoming_server_password);
666 if (STR_VALID(account->outgoing_server_address)) {
667 info->smtp_address = strdup(account->outgoing_server_address);
670 if (STR_VALID(account->outgoing_server_user_name)) {
671 info->smtp_user_name = strdup(account->outgoing_server_user_name);
674 if (STR_VALID(account->outgoing_server_password)) {
675 info->smtp_password = strdup(account->outgoing_server_password);
678 if (STR_VALID(account->incoming_server_address)) {
679 info->receiving_address = strdup(account->incoming_server_address);
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;
691 err = email_free_account(&account, 1);
693 if (err != EMAIL_ERROR_NONE) {
694 debug_critical("failed to free account info - err (%d)", err);
697 (*account_info) = info;
706 err = email_free_account(&account, 1);
707 if (err != EMAIL_ERROR_NONE) {
708 debug_critical("failed to free account info - err (%d)", err);
714 void email_engine_free_account_info(EmailAccountInfo **account_info)
717 RETURN_IF_FAIL(*account_info != NULL);
719 EmailAccountInfo *info = (*account_info);
721 if (STR_VALID(info->account_name)) {
722 free(info->account_name);
723 info->account_name = NULL;
726 if (STR_VALID(info->email_address)) {
727 free(info->email_address);
728 info->email_address = NULL;
731 if (STR_VALID(info->user_name)) {
732 free(info->user_name);
733 info->user_name = NULL;
736 if (STR_VALID(info->password)) {
737 free(info->password);
738 info->password = NULL;
741 if (STR_VALID(info->receiving_address)) {
742 free(info->receiving_address);
743 info->receiving_address = NULL;
746 if (STR_VALID(info->smtp_address)) {
747 free(info->smtp_address);
748 info->smtp_address = NULL;
751 if (STR_VALID(info->smtp_user_name)) {
752 free(info->smtp_user_name);
753 info->smtp_user_name = NULL;
756 if (STR_VALID(info->smtp_password)) {
757 free(info->smtp_password);
758 info->smtp_password = NULL;
765 GList *email_engine_get_ca_mailbox_list_using_glist(int account_id)
769 email_mailbox_t *mailbox_list = NULL;
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");
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);
786 nameandalias->name = g_strdup(mailbox_list[i].mailbox_name);
787 nameandalias->mailbox_id = mailbox_list[i].mailbox_id;
789 if (mailbox_list[i].alias == NULL) {
790 debug_critical("alias is NULL");
791 nameandalias->alias = nameandalias->name;
793 nameandalias->alias = g_strdup(mailbox_list[i].alias);
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;
801 ret = g_list_append(ret, (gpointer)nameandalias);
806 email_free_mailbox(&mailbox_list, count);
810 void email_engine_free_ca_mailbox_list_using_glist(GList **mailbox_list)
813 RETURN_IF_FAIL(mailbox_list != NULL);
814 RETURN_IF_FAIL(*mailbox_list != NULL);
816 GList *list = (GList *)(*mailbox_list);
817 int list_cnt = g_list_length(list);
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");
825 g_free(nameandalias->name);
826 g_free(nameandalias->alias);
830 *mailbox_list = NULL;
833 int email_engine_get_max_account_id(void)
837 email_account_t *account_list = NULL;
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);
844 debug_log("-- total account count : %d", count);
846 int max_account_id = 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);
854 email_free_account(&account_list, count);
857 return max_account_id;
861 int email_engine_get_count_account(void)
865 email_account_t *account_list = NULL;
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);
872 debug_log("-- total account count : %d", count);
874 email_free_account(&account_list, count);