4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
24 /******************************************************************************
25 * File: email-daemon-mailbox.c
26 * Desc: email-daemon Mailbox Operation
31 * 2006.08.16 : created
32 *****************************************************************************/
37 #include "email-daemon.h"
38 #include "email-core-event.h"
39 #include "email-daemon-account.h"
40 #include "email-debug-log.h"
41 #include "email-core-mailbox.h"
42 #include "email-core-account.h"
43 #include "email-core-global.h"
44 #include "email-core-utils.h"
45 #include "email-core-signal.h"
46 #include "email-utilities.h"
48 #ifdef __FEATURE_LOCAL_ACTIVITY__
49 extern int g_local_activity_run;
52 INTERNAL_FUNC int emdaemon_get_imap_mailbox_list(char *multi_user_name, int account_id, char* mailbox, int *handle, int* err_code)
54 EM_DEBUG_FUNC_BEGIN("account_id[%d] mailbox[%p] err_code[%p]", account_id, mailbox, err_code);
57 int err = EMAIL_ERROR_NONE;
58 email_event_t *event_data = NULL;
60 if (account_id <= 0 ||!mailbox) {
61 EM_DEBUG_EXCEPTION("account_id[%d], mailbox[%p]", account_id, mailbox);
62 err = EMAIL_ERROR_INVALID_PARAM;
66 event_data = em_malloc(sizeof(email_event_t));
67 if (event_data == NULL) {
68 EM_DEBUG_EXCEPTION("em_malloc failed");
69 err = EMAIL_ERROR_OUT_OF_MEMORY;
73 event_data->type = EMAIL_EVENT_SYNC_IMAP_MAILBOX;
74 event_data->account_id = account_id;
75 event_data->event_param_data_3 = EM_SAFE_STRDUP(mailbox);
76 event_data->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
78 if (!emcore_insert_event(event_data, (int*)handle, &err)) {
79 EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
87 if (ret == false && event_data) {
88 emcore_free_event(event_data);
89 EM_SAFE_FREE(event_data);
98 INTERNAL_FUNC int emdaemon_get_mailbox_list(char *multi_user_name, int account_id, email_mailbox_t** mailbox_list, int* count, int* err_code)
100 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], count[%p], err_code[%p]", account_id, mailbox_list, count, err_code);
103 int err = EMAIL_ERROR_NONE;
104 email_account_t* ref_account = NULL;
106 if (account_id <= 0 || !mailbox_list || !count) {
107 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_list[%p], count[%p]", account_id, mailbox_list, count);
108 err = EMAIL_ERROR_INVALID_PARAM;
112 ref_account = emcore_get_account_reference(multi_user_name, account_id, false);
114 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
115 err = EMAIL_ERROR_INVALID_ACCOUNT;
119 if (!emcore_get_mailbox_list(multi_user_name, account_id, mailbox_list, count, &err)) {
120 EM_DEBUG_EXCEPTION("emcore_get_mailbox_list failed [%d]", err);
129 emcore_free_account(ref_account);
130 EM_SAFE_FREE(ref_account);
140 INTERNAL_FUNC int emdaemon_get_mail_count_of_mailbox(char *multi_user_name, email_mailbox_t* mailbox, int* total, int* unseen, int* err_code)
142 EM_DEBUG_FUNC_BEGIN("mailbox[%p], total[%p], unseen[%p], err_code[%p]", mailbox, total, unseen, err_code);
144 /* default variable */
146 int err = EMAIL_ERROR_NONE;
147 email_account_t* ref_account = NULL;
149 if (!mailbox || !total || !unseen) {
150 EM_DEBUG_EXCEPTION("mailbox[%p], total[%p], unseen[%p]", mailbox, total, unseen);
151 err = EMAIL_ERROR_INVALID_PARAM;
155 ref_account = emcore_get_account_reference(multi_user_name, mailbox->account_id, false);
156 if (ref_account == NULL) {
157 EM_DEBUG_EXCEPTION(" emcore_get_account_reference failed [%d]", mailbox->account_id);
158 err = EMAIL_ERROR_INVALID_ACCOUNT;
162 if (!emcore_get_mail_count(multi_user_name, mailbox, total, unseen, &err)) {
163 EM_DEBUG_EXCEPTION("emcore_get_mail_count failed [%d]", err);
173 emcore_free_account(ref_account);
174 EM_SAFE_FREE(ref_account);
183 INTERNAL_FUNC int emdaemon_add_mailbox(char *multi_user_name, email_mailbox_t* new_mailbox, int on_server, int *handle, int* err_code)
185 EM_DEBUG_FUNC_BEGIN("new_mailbox[%p], err_code[%p]", new_mailbox, err_code);
188 int err = EMAIL_ERROR_NONE;
189 email_account_t *ref_account = NULL;
190 email_event_t *event_data = NULL;
191 email_mailbox_t *mailbox = NULL;
193 if (!new_mailbox || new_mailbox->account_id <= 0 || !new_mailbox->mailbox_name) {
194 if (new_mailbox != NULL)
195 EM_DEBUG_EXCEPTION("new_mailbox->account_id[%d], new_mailbox->mailbox_name[%p]", new_mailbox->account_id, new_mailbox->mailbox_name);
196 err = EMAIL_ERROR_INVALID_PARAM;
200 ref_account = emcore_get_account_reference(multi_user_name, new_mailbox->account_id, false);
202 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", new_mailbox->account_id);
203 err = EMAIL_ERROR_INVALID_ACCOUNT;
207 /* on_server is allowed to be only 0 when server_type is EMAIL_SERVER_TYPE_ACTIVE_SYNC */
208 if ( ref_account->incoming_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC )
211 if ( on_server ) { /* async */
213 mailbox = em_malloc(sizeof(email_mailbox_t));
215 if(mailbox == NULL) {
216 EM_DEBUG_EXCEPTION("em_malloc failed");
217 err = EMAIL_ERROR_OUT_OF_MEMORY;
221 memcpy(mailbox, new_mailbox, sizeof(email_mailbox_t));
222 mailbox->mailbox_name = EM_SAFE_STRDUP(new_mailbox->mailbox_name);
223 mailbox->alias = EM_SAFE_STRDUP(new_mailbox->alias);
225 if(new_mailbox->eas_data_length > 0 && new_mailbox->eas_data) {
226 mailbox->eas_data = em_malloc(new_mailbox->eas_data_length);
227 if(mailbox->eas_data == NULL) {
228 EM_DEBUG_EXCEPTION("em_malloc failed");
229 err = EMAIL_ERROR_OUT_OF_MEMORY;
232 memcpy(mailbox->eas_data, new_mailbox->eas_data, new_mailbox->eas_data_length);
235 event_data = em_malloc(sizeof(email_event_t));
236 if (event_data == NULL) {
237 EM_DEBUG_EXCEPTION("em_malloc failed");
238 err = EMAIL_ERROR_OUT_OF_MEMORY;
242 event_data->type = EMAIL_EVENT_CREATE_MAILBOX;
243 event_data->account_id = new_mailbox->account_id;
244 event_data->event_param_data_1 = (char*)mailbox;
245 event_data->event_param_data_4 = on_server;
246 event_data->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
248 if(!emcore_insert_event(event_data, (int*)handle, &err)) {
249 EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
253 if (!emcore_create_mailbox(multi_user_name, new_mailbox, on_server, -1, -1, &err)) {
254 EM_DEBUG_EXCEPTION("emcore_create failed [%d]", err);
265 emcore_free_event(event_data);
266 EM_SAFE_FREE(event_data);
269 emcore_free_mailbox(mailbox);
270 EM_SAFE_FREE(mailbox);
275 emcore_free_account(ref_account);
276 EM_SAFE_FREE(ref_account);
285 INTERNAL_FUNC int emdaemon_set_mailbox_type(char *multi_user_name, int input_mailbox_id, email_mailbox_type_e input_mailbox_type)
287 EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d], input_mailbox_type [%d]", input_mailbox_id, input_mailbox_type);
289 /* default variable */
290 int err = EMAIL_ERROR_NONE;
291 emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
293 if (!input_mailbox_id) {
294 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
295 err = EMAIL_ERROR_INVALID_PARAM;
299 if ( (err = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE) {
300 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
304 if (!emstorage_update_mailbox_type(multi_user_name, mailbox_tbl->account_id, -1, input_mailbox_id, input_mailbox_type, true, &err)) {
305 EM_DEBUG_EXCEPTION("emstorage_update_mailbox failed [%d]", err);
313 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
315 EM_DEBUG_FUNC_END("err [%d]", err);
319 INTERNAL_FUNC int emdaemon_set_local_mailbox(char *multi_user_name, int input_mailbox_id, int input_is_local_mailbox)
321 EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d], input_mailbox_type [%d]", input_mailbox_id, input_is_local_mailbox);
323 /* default variable */
324 int err = EMAIL_ERROR_NONE;
326 if (input_mailbox_id <= 0) {
327 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
328 err = EMAIL_ERROR_INVALID_PARAM;
332 if ( (err = emstorage_set_local_mailbox(multi_user_name, input_mailbox_id, input_is_local_mailbox, true)) != EMAIL_ERROR_NONE) {
333 EM_DEBUG_EXCEPTION("emstorage_set_local_mailbox failed [%d]", err);
340 EM_DEBUG_FUNC_END("err [%d]", err);
344 INTERNAL_FUNC int emdaemon_delete_mailbox(char *multi_user_name, int input_mailbox_id, int on_server, int *handle, int* err_code)
346 EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], err_code[%p]", input_mailbox_id, err_code);
349 int err = EMAIL_ERROR_NONE;
351 emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
352 email_account_t *ref_account = NULL;
353 email_event_t *event_data = NULL;
355 if ((err = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE || !mailbox_tbl) {
356 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
360 if (!input_mailbox_id || mailbox_tbl->account_id <= 0) {
361 if (input_mailbox_id != 0)
362 EM_DEBUG_EXCEPTION("mailbox_tbl->account_id[%d]", mailbox_tbl->account_id);
363 err = EMAIL_ERROR_INVALID_PARAM;
367 ref_account = emcore_get_account_reference(multi_user_name, mailbox_tbl->account_id, false);
369 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", mailbox_tbl->account_id);
370 err = EMAIL_ERROR_INVALID_ACCOUNT;
374 /* on_server is allowed to be only 0 when server_type is EMAIL_SERVER_TYPE_ACTIVE_SYNC */
375 if ( ref_account->incoming_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC ) {
380 if ( on_server ) { /* async */
381 event_data = em_malloc(sizeof(email_event_t));
382 if (event_data == NULL) {
383 EM_DEBUG_EXCEPTION("em_malloc failed");
384 err = EMAIL_ERROR_OUT_OF_MEMORY;
388 event_data->type = EMAIL_EVENT_DELETE_MAILBOX;
389 event_data->account_id = mailbox_tbl->account_id;
390 event_data->event_param_data_4 = input_mailbox_id;
391 event_data->event_param_data_5 = on_server;
392 event_data->event_param_data_6 = recursive;
393 event_data->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
395 if(!emcore_insert_event(event_data, (int*)handle, &err)) {
396 EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
400 if (!emcore_delete_mailbox(multi_user_name, input_mailbox_id, on_server, recursive)) {
401 EM_DEBUG_EXCEPTION("emcore_delete failed [%d]", err);
410 if (ret == false && event_data) {
411 emcore_free_event(event_data);
412 EM_SAFE_FREE(event_data);
416 emcore_free_account(ref_account);
417 EM_SAFE_FREE(ref_account);
426 INTERNAL_FUNC int emdaemon_delete_mailbox_all(char *multi_user_name, email_mailbox_t* mailbox, int* err_code)
428 EM_DEBUG_FUNC_BEGIN("malibox[%p], err_code[%p]", mailbox, err_code);
430 /* default variable */
432 int err = EMAIL_ERROR_NONE;
433 email_account_t* ref_account = NULL;
436 err = EMAIL_ERROR_INVALID_PARAM;
440 if (mailbox->account_id <= 0) {
441 EM_DEBUG_EXCEPTION("malibox->account_id[%d]", mailbox->account_id);
442 err = EMAIL_ERROR_INVALID_PARAM;
446 ref_account = emcore_get_account_reference(multi_user_name, mailbox->account_id, false);
448 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", mailbox->account_id);
449 err = EMAIL_ERROR_INVALID_ACCOUNT;
453 if (!emcore_delete_mailbox_all(multi_user_name, mailbox, &err)) {
454 EM_DEBUG_EXCEPTION("emcore_delete_all failed [%d]", err);
463 emcore_free_account(ref_account);
464 EM_SAFE_FREE(ref_account);
474 INTERNAL_FUNC int emdaemon_sync_header(char *multi_user_name, int input_account_id, int input_mailbox_id, int *handle, int* err_code)
476 EM_DEBUG_FUNC_BEGIN("input_account_id[%d], input_mailbox_id[%d], handle[%p], err_code[%p]", input_account_id, input_mailbox_id, handle, err_code);
479 int err = EMAIL_ERROR_NONE;
480 email_event_t *event_data = NULL;
482 if (input_mailbox_id < 0 || handle == NULL) {
483 EM_DEBUG_EXCEPTION("parameter is invalid");
484 err = EMAIL_ERROR_INVALID_PARAM;
488 event_data = em_malloc(sizeof(email_event_t));
489 if (event_data == NULL) {
490 EM_DEBUG_EXCEPTION("em_malloc failed");
491 err = EMAIL_ERROR_OUT_OF_MEMORY;
495 if(input_account_id == ALL_ACCOUNT) {
496 EM_DEBUG_LOG(">>>> emdaemon_sync_header for all account event_data.event_param_data_4 [%d]", event_data->event_param_data_4);
497 event_data->type = EMAIL_EVENT_SYNC_HEADER;
498 event_data->account_id = input_account_id;
499 event_data->event_param_data_5 = input_mailbox_id;
500 event_data->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
502 /* In case of Mailbox NULL, we need to set arg as EMAIL_SYNC_ALL_MAILBOX */
503 if (input_mailbox_id == 0)
504 event_data->event_param_data_4 = EMAIL_SYNC_ALL_MAILBOX;
506 if (!emcore_insert_event(event_data, (int*)handle, &err)) {
507 EM_DEBUG_EXCEPTION("emcore_insert_event falied [%d]", err);
511 /* Modified the code to sync all mailbox in a Single Event */
512 event_data->type = EMAIL_EVENT_SYNC_HEADER;
513 event_data->account_id = input_account_id;
514 event_data->event_param_data_5 = input_mailbox_id;
515 event_data->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
517 /* In case of Mailbox NULL, we need to set arg as EMAIL_SYNC_ALL_MAILBOX */
518 if (input_mailbox_id == 0)
519 event_data->event_param_data_4 = EMAIL_SYNC_ALL_MAILBOX;
520 EM_DEBUG_LOG(">>>> EVENT ARG [ %d ] ", event_data->event_param_data_4);
522 if (!emcore_insert_event(event_data, (int*)handle, &err)) {
523 EM_DEBUG_EXCEPTION("emcore_insert_event falied [%d]", err);
528 /* Due to fast response, event noti is moved here from worker_event_queue */
529 char input_mailbox_id_str[10] = {0};
530 snprintf(input_mailbox_id_str, 10, "%d ", input_mailbox_id);
531 if (!emcore_notify_network_event(NOTI_DOWNLOAD_START, input_account_id, ((input_mailbox_id==0)? NULL:input_mailbox_id_str), *handle, 0))
532 EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_DOWNLOAD_START] Failed >>>> ");
534 /* if ((err = emcore_update_sync_status_of_account(multi_user_name, input_account_id, SET_TYPE_SET, SYNC_STATUS_SYNCING)) != EMAIL_ERROR_NONE)
535 EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
539 #ifdef __FEATURE_LOCAL_ACTIVITY__
540 EM_DEBUG_LOG("Setting g_local_activity_run ");
541 g_local_activity_run = 1;
548 if (ret == false && event_data) {
549 emcore_free_event(event_data);
550 EM_SAFE_FREE(event_data);
559 INTERNAL_FUNC int emdaemon_set_mail_slot_size_of_mailbox(char *multi_user_name, int account_id, int mailbox_id, int new_slot_size, int *handle, int *err_code)
561 EM_DEBUG_FUNC_BEGIN("account_id [%d], mailbox_id[%d], handle[%p], err_code[%p]", account_id, mailbox_id, handle, err_code);
564 int err = EMAIL_ERROR_NONE;
565 email_event_t *event_data = NULL;
568 EM_DEBUG_EXCEPTION("handle is required");
569 err = EMAIL_ERROR_INVALID_PARAM;
573 event_data = em_malloc(sizeof(email_event_t));
574 if (event_data == NULL) {
575 EM_DEBUG_EXCEPTION("em_malloc failed");
576 err = EMAIL_ERROR_OUT_OF_MEMORY;
580 event_data->type = EMAIL_EVENT_SET_MAIL_SLOT_SIZE;
581 event_data->account_id = account_id;
582 event_data->event_param_data_4 = mailbox_id;
583 event_data->event_param_data_5 = new_slot_size;
584 event_data->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
586 if (!emcore_insert_event(event_data, (int*)handle, &err)) {
587 EM_DEBUG_EXCEPTION("emcore_insert_event falied [%d]", err);
595 if (ret == false && event_data) {
596 emcore_free_event(event_data);
597 EM_SAFE_FREE(event_data);
606 INTERNAL_FUNC int emdaemon_rename_mailbox(char *multi_user_name, int input_mailbox_id, char *input_mailbox_path, char *input_mailbox_alias, void *input_eas_data, int input_eas_data_length, int input_on_server, int *output_handle)
608 EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d] input_mailbox_path[%p] input_mailbox_alias[%p] input_eas_data[%p] input_eas_data_length[%d] input_on_server[%d] output_handle[%p]", input_mailbox_id, input_mailbox_path, input_mailbox_alias, input_eas_data, input_eas_data_length, input_on_server, output_handle);
611 int err = EMAIL_ERROR_NONE;
612 emstorage_account_tbl_t *account_data = NULL;
613 emstorage_mailbox_tbl_t *old_mailbox_data = NULL;
614 email_event_t *event_data = NULL;
616 if(input_mailbox_id <= 0 || output_handle == NULL) {
617 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
618 err = EMAIL_ERROR_INVALID_PARAM;
622 if (input_on_server) {
623 if ((err = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &old_mailbox_data)) != EMAIL_ERROR_NONE) {
624 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
628 if (!emstorage_get_account_by_id(multi_user_name, old_mailbox_data->account_id, EMAIL_ACC_GET_OPT_DEFAULT, &account_data, true, &err)) {
629 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed [%d]", err);
633 if (account_data->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) {
634 event_data = em_malloc(sizeof(email_event_t));
635 if (event_data == NULL) {
636 EM_DEBUG_EXCEPTION("event_data failed");
637 err = EMAIL_ERROR_OUT_OF_MEMORY;
641 event_data->type = EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER;
642 event_data->event_param_data_1 = EM_SAFE_STRDUP(old_mailbox_data->mailbox_name);
643 event_data->event_param_data_2 = EM_SAFE_STRDUP(input_mailbox_path);
644 event_data->event_param_data_3 = EM_SAFE_STRDUP(input_mailbox_alias);
645 event_data->event_param_data_4 = input_mailbox_id;
646 event_data->account_id = old_mailbox_data->account_id;
647 event_data->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
649 if (!emcore_insert_event(event_data, (int*)output_handle, &err)) {
650 EM_DEBUG_EXCEPTION("emcore_insert_event falied [%d]", err);
655 if ((err = emcore_rename_mailbox(multi_user_name, input_mailbox_id, input_mailbox_path, input_mailbox_alias, input_eas_data, input_eas_data_length, false, true, 0)) != EMAIL_ERROR_NONE) {
656 EM_DEBUG_EXCEPTION("emcore_rename_mailbox failed [%d]", err);
665 if (ret == false && event_data) {
666 emcore_free_event(event_data);
667 EM_SAFE_FREE(event_data);
670 if (old_mailbox_data)
671 emstorage_free_mailbox(&old_mailbox_data, 1, NULL);
674 emstorage_free_account(&account_data, 1, NULL);
676 EM_DEBUG_FUNC_END("err [%d]", err);