4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Minsoo Kim <minnsoo.kim@samsung.com>, Kyuho Jo <kyuho.jo@samsung.com>,
7 * Sunghyun Kwon <sh0701.kwon@samsung.com>
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
29 #include "email-storage.h"
30 #include "email-utilities.h"
31 #include "email-daemon.h"
32 #include "email-network.h"
33 #include "email-core-global.h"
34 #include "email-core-account.h"
35 #include "email-core-event.h"
36 #include "email-core-utils.h"
37 #include "email-core-mailbox.h"
38 #include "email-core-imap-mailbox.h"
39 #include "email-core-mail.h"
40 #include "email-core-mailbox-sync.h"
41 #include "email-core-smtp.h"
42 #include "email-core-utils.h"
43 #include "email-debug-log.h"
44 #include "email-types.h"
45 #include "email-internal-types.h"
46 #include "email-core-auto-download.h"
48 /*-----------------------------------------------------------------------------
49 * Auto Download Event Queue
50 *---------------------------------------------------------------------------*/
51 INTERNAL_FUNC thread_t g_auto_download_thread;
52 INTERNAL_FUNC pthread_cond_t _auto_downalod_available_signal = PTHREAD_COND_INITIALIZER;
53 INTERNAL_FUNC pthread_mutex_t *_auto_download_queue_lock = NULL;
55 INTERNAL_FUNC GQueue *g_auto_download_que = NULL;
56 INTERNAL_FUNC int g_auto_download_loop = 1;
57 INTERNAL_FUNC int auto_download_thread_run = 0;
59 #define AUTO_DOWNLOAD_QUEUE_MAX 100
61 static void* worker_auto_download_queue(void *arg);
62 static gint __auto_download_compare_func(gconstpointer a, gconstpointer b, gpointer user_data);
64 INTERNAL_FUNC int emcore_start_auto_download_loop(int *err_code)
66 EM_DEBUG_FUNC_BEGIN();
70 *err_code = EMAIL_ERROR_NONE;
72 if (g_auto_download_thread) {
73 EM_DEBUG_EXCEPTION("auto downlaod thread is already running...");
75 *err_code = EMAIL_ERROR_UNKNOWN;
79 g_auto_download_que = g_queue_new();
80 g_queue_init(g_auto_download_que);
81 g_auto_download_loop = 1;
84 INITIALIZE_RECURSIVE_CRITICAL_SECTION(_auto_download_queue_lock);
87 THREAD_CREATE(g_auto_download_thread, worker_auto_download_queue, NULL, thread_error);
89 if (thread_error != 0) {
90 EM_DEBUG_EXCEPTION("cannot create thread");
91 g_auto_download_loop = 0;
93 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
98 *err_code = EMAIL_ERROR_NONE;
104 INTERNAL_FUNC int emcore_auto_download_loop_continue(void)
106 return g_auto_download_loop;
110 INTERNAL_FUNC int emcore_stop_auto_download_loop(int *err_code)
112 EM_DEBUG_FUNC_BEGIN();
114 if (err_code != NULL)
115 *err_code = EMAIL_ERROR_NONE;
117 if (!g_auto_download_thread) {
118 if (err_code != NULL)
119 *err_code = EMAIL_ERROR_UNKNOWN;
123 /* stop event_data loop */
124 g_auto_download_loop = 0;
126 WAKE_CONDITION_VARIABLE(_auto_downalod_available_signal);
128 /* wait for thread finished */
129 THREAD_JOIN(g_auto_download_thread);
131 g_queue_free(g_auto_download_que);
133 g_auto_download_thread = 0;
135 DELETE_RECURSIVE_CRITICAL_SECTION(_auto_download_queue_lock);
136 DELETE_CONDITION_VARIABLE(_auto_downalod_available_signal);
138 if (err_code != NULL)
139 *err_code = EMAIL_ERROR_NONE;
145 INTERNAL_FUNC int emcore_insert_auto_download_event(email_event_auto_download *event_data, int *err_code)
147 EM_DEBUG_FUNC_BEGIN("event_data[%p], err_code[%p]", event_data, err_code);
150 EM_DEBUG_EXCEPTION("Invalid Parameter");
151 if (err_code != NULL)
152 *err_code = EMAIL_ERROR_INVALID_PARAM;
156 if (!g_auto_download_thread) {
157 EM_DEBUG_EXCEPTION("g_auto_download_thread is not ready");
158 if (err_code != NULL)
159 *err_code = EMAIL_ERROR_LOAD_ENGINE_FAILURE;
164 int error = EMAIL_ERROR_NONE;
167 ENTER_RECURSIVE_CRITICAL_SECTION(_auto_download_queue_lock);
169 if (g_auto_download_que)
170 q_length = g_queue_get_length(g_auto_download_que);
171 EM_DEBUG_LOG("Q Length : [%d]", q_length);
173 EM_DEBUG_LOG("event_data->status : [%d]", event_data->status);
175 if (q_length > AUTO_DOWNLOAD_QUEUE_MAX) {
176 EM_DEBUG_EXCEPTION("auto download que is full...");
177 error = EMAIL_ERROR_EVENT_QUEUE_FULL;
180 event_data->status = EMAIL_EVENT_STATUS_WAIT;
181 g_queue_insert_sorted(g_auto_download_que, event_data, __auto_download_compare_func, event_data);
182 //WAKE_CONDITION_VARIABLE(_auto_downalod_available_signal);
186 LEAVE_RECURSIVE_CRITICAL_SECTION(_auto_download_queue_lock);
189 EM_DEBUG_LOG("ERR [%d]", error);
197 INTERNAL_FUNC int emcore_retrieve_auto_download_event(email_event_auto_download **event_data, int *err_code)
199 EM_DEBUG_FUNC_BEGIN("event_data[%p], err_code[%p]", event_data, err_code);
202 int error = EMAIL_ERROR_NONE;
204 //email_event_auto_download *poped = NULL;
205 email_event_auto_download *head_event = NULL;
207 if (g_auto_download_que)
208 q_length = g_queue_get_length(g_auto_download_que);
209 EM_DEBUG_LOG("Q Length : [%d]", q_length);
212 error = EMAIL_ERROR_EVENT_QUEUE_EMPTY;
213 EM_DEBUG_LOG("QUEUE is empty");
218 head_event = (email_event_auto_download *)g_queue_peek_head(g_auto_download_que);
220 error = EMAIL_ERROR_EVENT_QUEUE_EMPTY;
221 EM_DEBUG_LOG_DEV("QUEUE is empty");
225 /*if (head_event->status != EMAIL_EVENT_STATUS_WAIT) {
226 EM_DEBUG_LOG("EVENT STATUS [%d]", head_event->status);
227 poped = g_queue_pop_head(g_auto_download_que);
231 error = EMAIL_ERROR_EVENT_QUEUE_EMPTY;
232 EM_DEBUG_LOG("QUEUE is empty");
237 head_event->status = EMAIL_EVENT_STATUS_STARTED;
238 *event_data = head_event;
246 if (err_code != NULL)
249 EM_DEBUG_FUNC_END("ret [%d]", ret);
254 INTERNAL_FUNC int emcore_is_auto_download_queue_empty(void)
256 EM_DEBUG_FUNC_BEGIN();
261 ENTER_RECURSIVE_CRITICAL_SECTION(_auto_download_queue_lock);
263 if (g_auto_download_que)
264 q_length = g_queue_get_length(g_auto_download_que);
266 EM_DEBUG_LOG("Q Length : [%d]", q_length);
269 EM_DEBUG_LOG("auto downlaod que is empty...");
273 LEAVE_RECURSIVE_CRITICAL_SECTION(_auto_download_queue_lock);
274 EM_DEBUG_FUNC_END("ret [%d]", ret);
279 INTERNAL_FUNC int emcore_is_auto_download_queue_full(void)
281 EM_DEBUG_FUNC_BEGIN();
286 ENTER_RECURSIVE_CRITICAL_SECTION(_auto_download_queue_lock);
288 if (g_auto_download_que)
289 q_length = g_queue_get_length(g_auto_download_que);
291 EM_DEBUG_LOG("Q Length : [%d]", q_length);
293 if (q_length > AUTO_DOWNLOAD_QUEUE_MAX) {
294 EM_DEBUG_LOG("auto downlaod que is full...");
298 LEAVE_RECURSIVE_CRITICAL_SECTION(_auto_download_queue_lock);
304 INTERNAL_FUNC int emcore_clear_auto_download_queue(void)
306 EM_DEBUG_FUNC_BEGIN();
310 email_event_auto_download *pop_elm = NULL;
312 ENTER_RECURSIVE_CRITICAL_SECTION(_auto_download_queue_lock);
314 q_length = g_auto_download_que ? g_queue_get_length(g_auto_download_que) : 0;
316 for (i = 0; i < q_length; i++) {
317 pop_elm = (email_event_auto_download *)g_queue_peek_nth(g_auto_download_que, i);
320 EM_SAFE_FREE(pop_elm);
324 g_queue_clear(g_auto_download_que);
327 LEAVE_RECURSIVE_CRITICAL_SECTION(_auto_download_queue_lock);
333 static void* worker_auto_download_queue(void *arg)
335 EM_DEBUG_FUNC_BEGIN();
336 int err = EMAIL_ERROR_NONE;
341 int account_count = 0;
342 int mailbox_count = 0;
343 int activity_count = 0;
344 int activity_list_count = 0;
345 int *account_list = NULL;
346 int *mailbox_list = NULL;
347 email_event_auto_download *event_data = NULL;
348 email_event_auto_download *started_event = NULL;
349 email_event_auto_download *activity_list = NULL;
352 if (!emstorage_open(NULL, &err)) {
353 EM_DEBUG_EXCEPTION("emstorage_open falied [%d]", err);
357 /* check that event_data loop is continuous */
358 while (emcore_auto_download_loop_continue()) {
360 ENTER_RECURSIVE_CRITICAL_SECTION(_auto_download_queue_lock);
362 if (!emcore_retrieve_auto_download_event(&event_data, &err)) {
363 /* no event_data pending */
364 if (err != EMAIL_ERROR_EVENT_QUEUE_EMPTY) {
365 LEAVE_RECURSIVE_CRITICAL_SECTION(_auto_download_queue_lock);
370 if (!emstorage_get_auto_download_activity_count(NULL, &activity_count, false, &err)) {
371 EM_DEBUG_LOG("emstorage_get_auto_download_activity_count failed [%d]", err);
375 if (activity_count <= 0) {
376 EM_DEBUG_LOG("auto download activity count is 0");
381 EM_SAFE_FREE(account_list);
382 if (!emstorage_get_auto_download_account_list(NULL, &account_list, &account_count, false, &err)) {
383 EM_DEBUG_EXCEPTION(" emstorage_get_auto_download_account_list failed.. [%d]", err);
387 if (account_count <= 0 || !account_list) {
388 EM_DEBUG_LOG("auto download account count is 0");
392 for (ai = 0; ai < account_count; ai++) {
394 email_account_t *account_ref = NULL;
395 if (!(account_ref = emcore_get_account_reference(NULL, account_list[ai], false))) {
396 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_list[ai]);
397 err = EMAIL_ERROR_INVALID_ACCOUNT;
401 if (!(account_ref->wifi_auto_download)) {
402 EM_DEBUG_LOG("ACCOUNT[%d] AUTO DOWNLOAD MODE OFF", account_ref->account_id);
403 emcore_free_account(account_ref);
404 EM_SAFE_FREE(account_ref);
408 emcore_free_account(account_ref);
409 EM_SAFE_FREE(account_ref);
412 EM_SAFE_FREE(mailbox_list);
413 if (!emstorage_get_auto_download_mailbox_list(NULL, account_list[ai], &mailbox_list, &mailbox_count, false, &err)) {
414 EM_DEBUG_EXCEPTION(" emstorage_get_auto_download_mailbox_list failed.. [%d]", err);
418 if (mailbox_count <= 0 || !mailbox_list) {
419 EM_DEBUG_LOG("auto download mailbox count is 0");
423 for (mi = 0; mi < mailbox_count; mi++) {
424 emstorage_mailbox_tbl_t *target_mailbox = NULL;
426 activity_list_count = 0;
427 EM_SAFE_FREE(activity_list);
429 if ((err = emstorage_get_mailbox_by_id(NULL, mailbox_list[mi], &target_mailbox)) != EMAIL_ERROR_NONE) {
430 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
433 if (target_mailbox) {
434 if (target_mailbox->mailbox_type != EMAIL_MAILBOX_TYPE_INBOX) {
435 EM_DEBUG_LOG("Not INBOX mail, skip to next");
436 emstorage_free_mailbox(&target_mailbox, 1, NULL);
439 emstorage_free_mailbox(&target_mailbox, 1, NULL);
442 if (!emstorage_get_auto_download_activity(NULL, account_list[ai], mailbox_list[mi], &activity_list, &activity_list_count, false, &err)) {
443 EM_DEBUG_EXCEPTION(" emstorage_get_auto_download_activity failed.. [%d]", err);
447 if (activity_list_count <= 0 || !activity_list) {
448 EM_DEBUG_LOG("auto download activity count is 0");
452 for (di = 0; di < activity_list_count; di++) {
453 email_event_auto_download *activity = NULL;
454 activity = (email_event_auto_download *)em_malloc(sizeof(email_event_auto_download));
456 EM_DEBUG_EXCEPTION("Malloc failed");
457 err = EMAIL_ERROR_OUT_OF_MEMORY;
461 activity->activity_id = activity_list[di].activity_id;
462 activity->status = EMAIL_EVENT_STATUS_DIRECT;
463 activity->account_id = activity_list[di].account_id;
464 activity->mail_id = activity_list[di].mail_id;
465 activity->server_mail_id = activity_list[di].server_mail_id;
466 activity->mailbox_id = activity_list[di].mailbox_id;
468 if (!emcore_insert_auto_download_event(activity, &err)) {
469 EM_DEBUG_EXCEPTION("emcore_insert_auto_download_event is failed [%d]", err);
470 EM_SAFE_FREE(activity);
471 if (err == EMAIL_ERROR_EVENT_QUEUE_FULL) goto CHECK_CONTINUE;
479 EM_SAFE_FREE(account_list);
480 EM_SAFE_FREE(mailbox_list);
481 EM_SAFE_FREE(activity_list);
483 if (!emcore_is_auto_download_queue_empty()) {
484 LEAVE_RECURSIVE_CRITICAL_SECTION(_auto_download_queue_lock);
488 auto_download_thread_run = 0;
489 //go to sleep when queue is empty
490 SLEEP_CONDITION_VARIABLE(_auto_downalod_available_signal, *_auto_download_queue_lock);
491 EM_DEBUG_LOG("Wake up by _auto_downalod_available_signal");
492 LEAVE_RECURSIVE_CRITICAL_SECTION(_auto_download_queue_lock);
494 LEAVE_RECURSIVE_CRITICAL_SECTION(_auto_download_queue_lock);
495 EM_DEBUG_LOG_DEV(">>>>>>>>>>>>>>> Got auto download event_data !!! <<<<<<<<<<<<<<<");
496 int state1 = 0, state2 = 0, state3 = 0, wifi_status = 0;
497 emstorage_mail_tbl_t *mail = NULL;
498 emstorage_mailbox_tbl_t *target_mailbox = NULL;
499 email_account_t *account_ref = NULL;
500 auto_download_thread_run = 1;
502 if ((err = emnetwork_get_wifi_status(&wifi_status)) != EMAIL_ERROR_NONE) {
503 EM_DEBUG_EXCEPTION("emnetwork_get_wifi_status failed [%d]", err);
506 EM_DEBUG_LOG("WIFI Status [%d]", wifi_status);
508 if ((state1 = emcore_get_pbd_thd_state()) == true ||
509 (state2 = emcore_is_event_queue_empty()) == false ||
510 (state3 = emcore_is_send_event_queue_empty()) == false ||
511 (wifi_status <= 1)) {
513 EM_DEBUG_LOG("Auto Download Thread going to SLEEP");
515 ENTER_RECURSIVE_CRITICAL_SECTION(_auto_download_queue_lock);
516 auto_download_thread_run = 0;
517 SLEEP_CONDITION_VARIABLE(_auto_downalod_available_signal, *_auto_download_queue_lock);
518 EM_DEBUG_LOG("Wake up by _auto_downalod_available_signal");
519 LEAVE_RECURSIVE_CRITICAL_SECTION(_auto_download_queue_lock);
521 auto_download_thread_run = 1;
524 if (!(account_ref = emcore_get_account_reference(event_data->multi_user_name, event_data->account_id, false))) {
525 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", event_data->account_id);
526 err = EMAIL_ERROR_INVALID_ACCOUNT;
530 if (!(account_ref->wifi_auto_download)) {
531 EM_DEBUG_LOG("ACCOUNT[%d] AUTO DOWNLOAD MODE OFF", event_data->account_id);
535 if ((err = emstorage_get_mailbox_by_id(event_data->multi_user_name, event_data->mailbox_id, &target_mailbox)) != EMAIL_ERROR_NONE) {
536 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
539 if (target_mailbox) {
540 if (target_mailbox->mailbox_type != EMAIL_MAILBOX_TYPE_INBOX) {
541 EM_DEBUG_LOG("Not INBOX mail, skip to next");
542 emstorage_free_mailbox(&target_mailbox, 1, NULL);
543 goto DELETE_ACTIVITY;
546 emstorage_free_mailbox(&target_mailbox, 1, NULL);
549 /* Handling storage full */
550 if ((err = emcore_is_storage_full()) == EMAIL_ERROR_MAIL_MEMORY_FULL) {
551 EM_DEBUG_EXCEPTION("Storage is full");
552 ENTER_RECURSIVE_CRITICAL_SECTION(_auto_download_queue_lock);
553 auto_download_thread_run = 0;
554 SLEEP_CONDITION_VARIABLE(_auto_downalod_available_signal, *_auto_download_queue_lock);
555 EM_DEBUG_LOG("Wake up by _auto_downalod_available_signal");
556 LEAVE_RECURSIVE_CRITICAL_SECTION(_auto_download_queue_lock);
559 emcore_free_account(account_ref);
560 EM_SAFE_FREE(account_ref);
566 // POP3's UID is string, not integer type. So event_data->server_mail_id is invalid in this time.
567 // emstorage_get_mail_data_by_servermailid() is always fail if it is POP3 event.
568 if (!emstorage_get_mail_by_id(event_data->multi_user_name,
573 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed : [%d]", err);
576 if (mail->body_download_status & EMAIL_BODY_DOWNLOAD_STATUS_FULLY_DOWNLOADED) {
577 EM_DEBUG_LOG("fully downloaded mail");
580 EM_DEBUG_LOG("partially downloaded mail");
581 if (account_ref->incoming_server_type == EMAIL_SERVER_TYPE_POP3) {
582 EM_DEBUG_LOG("#####AUTO DOWNLOAD BODY: ACCOUNT_ID[%d] MAILBOX_ID[%d] MAIL_ID[%d] UID[%lu] ACTIVITY[%d]#####",
583 event_data->account_id, event_data->mailbox_id,
584 event_data->mail_id, event_data->server_mail_id, event_data->activity_id);
585 if (!emcore_gmime_download_body_sections(event_data->multi_user_name,
587 event_data->account_id,
595 EM_DEBUG_EXCEPTION("emcore_gmime_download_body_sections failed - %d", err);
599 if (mail->attachment_count > 0) {
602 int attachment_count = 0;
603 emstorage_attachment_tbl_t *attachment_list = NULL;
605 if ((err = emstorage_get_attachment_list(event_data->multi_user_name,
609 &attachment_count)) != EMAIL_ERROR_NONE) {
610 EM_DEBUG_EXCEPTION("emstorage_get_attachment_list failed [%d]", err);
612 for (i = 0; i < attachment_count; i++) {
613 if (attachment_list[i].attachment_inline_content_status)
618 if (attachment_list[i].attachment_save_status)
621 EM_DEBUG_LOG("#####AUTO DOWNLOAD ATTACHMENT[%d]: ACCOUNT_ID[%d] "
622 "MAILBOX_ID[%d] MAIL_ID[%d] UID[%lu] ACTIVITY[%d]#####",
623 j, event_data->account_id, event_data->mailbox_id,
624 event_data->mail_id, event_data->server_mail_id, event_data->activity_id);
626 if (!emcore_gmime_download_attachment(event_data->multi_user_name,
633 EM_DEBUG_EXCEPTION("emcore_gmime_download_attachment failed [%d]", err);
634 EM_DEBUG_LOG("Retry again");
635 if (!emcore_gmime_download_attachment(event_data->multi_user_name,
642 EM_DEBUG_EXCEPTION("emcore_gmime_download_attachment failed [%d]", err);
647 emstorage_free_attachment(&attachment_list, attachment_count, NULL);
652 emstorage_free_mail(&mail, 1, NULL);
656 if (!emcore_delete_auto_download_activity(event_data->multi_user_name,
657 event_data->account_id,
659 event_data->activity_id,
661 EM_DEBUG_EXCEPTION("emcore_delete_auto_download_activity failed [%d]", err);
666 /* free event itself */
667 ENTER_RECURSIVE_CRITICAL_SECTION(_auto_download_queue_lock);
668 started_event = g_queue_pop_head(g_auto_download_que);
669 LEAVE_RECURSIVE_CRITICAL_SECTION(_auto_download_queue_lock);
670 if (!started_event) {
671 EM_DEBUG_EXCEPTION("Failed to g_queue_pop_head");
673 EM_SAFE_FREE(started_event->multi_user_name);
674 EM_SAFE_FREE(started_event);
678 emcore_free_account(account_ref);
679 EM_SAFE_FREE(account_ref);
684 if (!emstorage_close(&err))
685 EM_DEBUG_EXCEPTION("emstorage_close falied [%d]", err);
687 emcore_close_recv_stream_list();
694 INTERNAL_FUNC int emcore_insert_auto_download_job(char *multi_user_name,
698 int auto_download_on,
702 EM_DEBUG_FUNC_BEGIN("account_id [%d], maibox_id[%d], mail_id[%d], uid[%p]", account_id, mailbox_id, mail_id, uid);
705 int err = EMAIL_ERROR_NONE;
706 int event_pushed = 0;
707 email_event_auto_download *ad_event = NULL;
709 if (account_id < FIRST_ACCOUNT_ID || mailbox_id <= 0 || mail_id < 0 || !uid) {
710 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
711 err = EMAIL_ERROR_INVALID_PARAM;
715 ad_event = (email_event_auto_download *)em_malloc(sizeof(email_event_auto_download));
718 EM_DEBUG_EXCEPTION("em_mallocfailed...");
719 err = EMAIL_ERROR_OUT_OF_MEMORY;
723 ad_event->status = 0;
724 ad_event->account_id = account_id;
725 ad_event->mail_id = mail_id;
726 ad_event->server_mail_id = strtoul(uid, NULL, 0);
727 ad_event->mailbox_id = mailbox_id;
728 ad_event->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
730 if (!emcore_insert_auto_download_activity(ad_event, &(ad_event->activity_id), &err)) {
731 EM_DEBUG_EXCEPTION("Inserting auto download activity failed with error[%d]", err);
736 if (!auto_download_on) {
740 if (emcore_is_auto_download_queue_full()) {
741 EM_DEBUG_LOG("Activity inserted only in DB .. Queue is Full");
743 ad_event->status = EMAIL_EVENT_STATUS_DIRECT;
745 if (!emcore_insert_auto_download_event(ad_event, &err)) {
746 EM_DEBUG_EXCEPTION("emcore_insert_auto_download_event is failed [%d]", err);
757 EM_SAFE_FREE(ad_event->multi_user_name);
766 INTERNAL_FUNC int emcore_insert_auto_download_activity(email_event_auto_download *local_activity, int *activity_id, int *err_code)
768 EM_DEBUG_FUNC_BEGIN("local_activity[%p], activity_id[%p], err_code[%p]", local_activity, activity_id, err_code);
770 if (!local_activity || !activity_id) {
771 EM_DEBUG_EXCEPTION("local_activity[%p], activity_id[%p] err_code[%p]", local_activity, activity_id, err_code);
772 if (err_code != NULL)
773 *err_code = EMAIL_ERROR_INVALID_PARAM;
778 int err = EMAIL_ERROR_NONE;
779 int before_tr_begin = 0;
781 if (!emstorage_begin_transaction(local_activity->multi_user_name, NULL, NULL, &err)) {
782 EM_DEBUG_EXCEPTION("emstorage_begin_transaction failed [%d]", err);
787 if (!emstorage_add_auto_download_activity(local_activity->multi_user_name, local_activity, activity_id, false, &err)) {
788 EM_DEBUG_EXCEPTION("emstorage_add_auto_download_activity failed [%d]", err);
795 if (ret == true) { /* COMMIT TRANSACTION; */
796 if (emstorage_commit_transaction(local_activity->multi_user_name, NULL, NULL, NULL) == false) {
797 err = EMAIL_ERROR_DB_FAILURE;
800 } else { /* ROLLBACK TRANSACTION; */
801 if (!before_tr_begin && emstorage_rollback_transaction(local_activity->multi_user_name, NULL, NULL, NULL) == false)
802 err = EMAIL_ERROR_DB_FAILURE;
805 if (err_code != NULL)
812 INTERNAL_FUNC int emcore_delete_auto_download_activity(char *multi_user_name, int account_id, int mail_id, int activity_id, int *err_code)
814 EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], err_code[%p]", account_id, mail_id, err_code);
816 if (account_id < FIRST_ACCOUNT_ID || mail_id < 0) {
817 EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d], err_code[%p]", account_id, mail_id, err_code);
818 if (err_code != NULL)
819 *err_code = EMAIL_ERROR_INVALID_PARAM;
824 int err = EMAIL_ERROR_NONE;
825 int before_tr_begin = 0;
827 if (!emstorage_begin_transaction(multi_user_name, NULL, NULL, &err)) {
828 EM_DEBUG_EXCEPTION("emstorage_begin_transaction failed [%d]", err);
833 if (!emstorage_delete_auto_download_activity(multi_user_name, account_id, mail_id, activity_id, false, &err)) {
834 EM_DEBUG_EXCEPTION("emstorage_delete_auto_download_activity failed [%d]", err);
836 } else if (err == EMAIL_ERROR_DATA_NOT_FOUND)
837 err = EMAIL_ERROR_NONE;
843 if (ret == true) { /* COMMIT TRANSACTION; */
844 if (emstorage_commit_transaction(multi_user_name, NULL, NULL, NULL) == false) {
845 err = EMAIL_ERROR_DB_FAILURE;
848 } else { /* ROLLBACK TRANSACTION; */
849 if (!before_tr_begin && emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL) == false)
850 err = EMAIL_ERROR_DB_FAILURE;
853 if (err_code != NULL)
859 static gint __auto_download_compare_func(gconstpointer a, gconstpointer b, gpointer user_data)
861 email_event_auto_download *first = (email_event_auto_download*)a;
862 email_event_auto_download *second = (email_event_auto_download*)b;
863 if (first->account_id == second->account_id) {
864 if (first->server_mail_id == second->server_mail_id) return 0;
865 else if (first->server_mail_id > second->server_mail_id) return -1;
868 if (first->account_id < second->account_id) return -1;