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"
46 #ifdef __FEATURE_LOCAL_ACTIVITY__
47 extern int g_local_activity_run;
50 INTERNAL_FUNC int emdaemon_get_imap_mailbox_list(int account_id, char* mailbox, int *handle, int* err_code)
52 EM_DEBUG_FUNC_BEGIN("account_id[%d] mailbox[%p] err_code[%p]", account_id, mailbox, err_code);
54 /* default variable */
56 int err = EMAIL_ERROR_NONE;
57 email_account_t* ref_account = NULL;
59 if (account_id <= 0 ||!mailbox) {
60 EM_DEBUG_EXCEPTION("account_id[%d], mailbox[%p]", account_id, mailbox);
61 err = EMAIL_ERROR_INVALID_PARAM;
65 ref_account = emcore_get_account_reference(account_id);
68 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
69 err = EMAIL_ERROR_INVALID_ACCOUNT;
73 email_event_t event_data;
74 memset(&event_data, 0x00, sizeof(email_event_t));
76 event_data.type = EMAIL_EVENT_SYNC_IMAP_MAILBOX;
77 event_data.account_id = account_id;
78 event_data.event_param_data_3 = EM_SAFE_STRDUP(mailbox);
80 if (!emcore_insert_event(&event_data, (int*)handle, &err)) {
81 EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
90 emcore_free_account(ref_account);
91 EM_SAFE_FREE(ref_account);
100 INTERNAL_FUNC int emdaemon_get_mailbox_list(int account_id, email_mailbox_t** mailbox_list, int* count, int* err_code)
102 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], count[%p], err_code[%p]", account_id, mailbox_list, count, err_code);
105 int err = EMAIL_ERROR_NONE;
106 email_account_t* ref_account = NULL;
108 if (account_id <= 0 || !mailbox_list || !count) {
109 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_list[%p], count[%p]", account_id, mailbox_list, count);
110 err = EMAIL_ERROR_INVALID_PARAM;
114 ref_account = emcore_get_account_reference(account_id);
117 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
118 err = EMAIL_ERROR_INVALID_ACCOUNT;
122 if (!emcore_get_mailbox_list(account_id, mailbox_list, count, &err)) {
123 EM_DEBUG_EXCEPTION("emcore_get_mailbox_list failed [%d]", err);
132 emcore_free_account(ref_account);
133 EM_SAFE_FREE(ref_account);
143 INTERNAL_FUNC int emdaemon_get_mail_count_of_mailbox(email_mailbox_t* mailbox, int* total, int* unseen, int* err_code)
145 EM_DEBUG_FUNC_BEGIN("mailbox[%p], total[%p], unseen[%p], err_code[%p]", mailbox, total, unseen, err_code);
147 /* default variable */
149 int err = EMAIL_ERROR_NONE;
150 email_account_t* ref_account = NULL;
152 if (!mailbox || !total || !unseen) {
153 EM_DEBUG_EXCEPTION("mailbox[%p], total[%p], unseen[%p]", mailbox, total, unseen);
154 err = EMAIL_ERROR_INVALID_PARAM;
158 ref_account = emcore_get_account_reference(mailbox->account_id);
159 if (ref_account == NULL) {
160 EM_DEBUG_EXCEPTION(" emcore_get_account_reference failed [%d]", mailbox->account_id);
161 err = EMAIL_ERROR_INVALID_ACCOUNT;
165 if (!emcore_get_mail_count(mailbox, total, unseen, &err)) {
166 EM_DEBUG_EXCEPTION("emcore_get_mail_count failed [%d]", err);
176 emcore_free_account(ref_account);
177 EM_SAFE_FREE(ref_account);
186 INTERNAL_FUNC int emdaemon_add_mailbox(email_mailbox_t* new_mailbox, int on_server, int *handle, int* err_code)
188 EM_DEBUG_FUNC_BEGIN("new_mailbox[%p], err_code[%p]", new_mailbox, err_code);
191 int err = EMAIL_ERROR_NONE;
192 email_account_t* ref_account = NULL;
194 if (!new_mailbox || new_mailbox->account_id <= 0 || !new_mailbox->mailbox_name) {
195 if (new_mailbox != NULL)
196 EM_DEBUG_EXCEPTION("new_mailbox->account_id[%d], new_mailbox->mailbox_name[%p]", new_mailbox->account_id, new_mailbox->mailbox_name);
197 err = EMAIL_ERROR_INVALID_PARAM;
201 ref_account = emcore_get_account_reference(new_mailbox->account_id);
204 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", new_mailbox->account_id);
205 err = EMAIL_ERROR_INVALID_ACCOUNT;
209 email_event_t event_data;
210 memset(&event_data, 0x00, sizeof(email_event_t));
213 /* on_server is allowed to be only 0 when server_type is EMAIL_SERVER_TYPE_ACTIVE_SYNC */
214 if ( ref_account->incoming_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC )
217 if ( on_server ) { /* async */
218 event_data.type = EMAIL_EVENT_CREATE_MAILBOX;
219 event_data.account_id = new_mailbox->account_id;
220 event_data.event_param_data_1 = EM_SAFE_STRDUP(new_mailbox->mailbox_name);
221 event_data.event_param_data_2 = EM_SAFE_STRDUP(new_mailbox->alias);
222 event_data.event_param_data_4 = on_server;
223 event_data.event_param_data_3 = GINT_TO_POINTER(new_mailbox->mailbox_type);
224 if(!emcore_insert_event(&event_data, (int*)handle, &err)) {
225 EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
230 if (!emcore_create_mailbox(new_mailbox, on_server, &err)) {
231 EM_DEBUG_EXCEPTION("emcore_create failed [%d]", err);
241 emcore_free_account(ref_account);
242 EM_SAFE_FREE(ref_account);
252 INTERNAL_FUNC int emdaemon_update_mailbox(email_mailbox_t* old_mailbox, email_mailbox_t* new_mailbox, int on_server, int *handle, int* err_code)
254 EM_DEBUG_FUNC_BEGIN("old_mailbox[%p], new_mailbox[%p], on_server[%d], handle[%p], err_code[%p]", old_mailbox, new_mailbox, on_server, handle, err_code);
256 /* default variable */
258 int err = EMAIL_ERROR_NONE;
259 email_account_t* ref_account = NULL;
261 if (!old_mailbox || old_mailbox->account_id <= 0 || !old_mailbox->mailbox_name
262 || !new_mailbox || new_mailbox->account_id <= 0 || !new_mailbox->mailbox_name) {
263 EM_DEBUG_EXCEPTION("INVALID PARAM");
264 if (old_mailbox != NULL)
265 EM_DEBUG_EXCEPTION("old_mailbox->account_id[%d], old_mailbox->mailbox_name[%p]", old_mailbox->account_id, old_mailbox->mailbox_name);
266 if (new_mailbox != NULL)
267 EM_DEBUG_EXCEPTION("new_mailbox->account_id[%d], new_mailbox->mailbox_name[%p]", new_mailbox->account_id, new_mailbox->mailbox_name);
269 err = EMAIL_ERROR_INVALID_PARAM;
273 ref_account = emcore_get_account_reference(old_mailbox->account_id);
276 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", old_mailbox->account_id);
277 err = EMAIL_ERROR_INVALID_ACCOUNT; /* instead of EMAIL_ERROR_INVALID_PARAM; */
281 email_event_t event_data;
282 memset(&event_data, 0x00, sizeof(email_event_t));
284 /* Update mailbox information only on local db */
285 if (!emcore_update_mailbox(old_mailbox, new_mailbox, &err)) {
286 EM_DEBUG_EXCEPTION("emcore_modify failed [%d]", err);
295 emcore_free_account(ref_account);
296 EM_SAFE_FREE(ref_account);
305 INTERNAL_FUNC int emdaemon_set_mailbox_type(int input_mailbox_id, email_mailbox_type_e input_mailbox_type)
307 EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d], input_mailbox_type [%d]", input_mailbox_id, input_mailbox_type);
309 /* default variable */
310 int err = EMAIL_ERROR_NONE;
311 emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
313 if (!input_mailbox_id) {
314 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
315 err = EMAIL_ERROR_INVALID_PARAM;
319 if ( (err = emstorage_get_mailbox_by_id(input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE) {
320 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
324 if (!emstorage_update_mailbox_type(mailbox_tbl->account_id, -1, mailbox_tbl->mailbox_name, input_mailbox_type, true, &err)) {
325 EM_DEBUG_EXCEPTION("emstorage_update_mailbox failed [%d]", err);
332 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
334 EM_DEBUG_FUNC_END("err [%d]", err);
338 INTERNAL_FUNC int emdaemon_set_local_mailbox(int input_mailbox_id, int input_is_local_mailbox)
340 EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d], input_mailbox_type [%d]", input_mailbox_id, input_is_local_mailbox);
342 /* default variable */
343 int err = EMAIL_ERROR_NONE;
345 if (input_mailbox_id <= 0) {
346 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
347 err = EMAIL_ERROR_INVALID_PARAM;
351 if ( (err = emstorage_set_local_mailbox(input_mailbox_id, input_is_local_mailbox, true)) != EMAIL_ERROR_NONE) {
352 EM_DEBUG_EXCEPTION("emstorage_set_local_mailbox failed [%d]", err);
359 EM_DEBUG_FUNC_END("err [%d]", err);
363 INTERNAL_FUNC int emdaemon_delete_mailbox(int input_mailbox_id, int on_server, int *handle, int* err_code)
365 EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], err_code[%p]", input_mailbox_id, err_code);
367 /* default variable */
369 int err = EMAIL_ERROR_NONE;
370 emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
371 email_account_t* ref_account = NULL;
373 if ((err = emstorage_get_mailbox_by_id(input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE || !mailbox_tbl) {
374 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
378 if (!input_mailbox_id || mailbox_tbl->account_id <= 0) {
379 if (input_mailbox_id != 0)
380 EM_DEBUG_EXCEPTION("mailbox_tbl->account_id[%d]", mailbox_tbl->account_id);
381 err = EMAIL_ERROR_INVALID_PARAM;
385 ref_account = emcore_get_account_reference(mailbox_tbl->account_id);
388 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", mailbox_tbl->account_id);
389 err = EMAIL_ERROR_INVALID_ACCOUNT;
393 email_event_t event_data;
394 memset(&event_data, 0x00, sizeof(email_event_t));
396 /* on_server is allowed to be only 0 when server_type is EMAIL_SERVER_TYPE_ACTIVE_SYNC */
397 if ( ref_account->incoming_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC ) {
400 if ( on_server ) { /* async */
401 event_data.type = EMAIL_EVENT_DELETE_MAILBOX;
402 event_data.account_id = mailbox_tbl->account_id;
403 event_data.event_param_data_4 = input_mailbox_id;
404 event_data.event_param_data_5 = on_server;
405 if(!emcore_insert_event(&event_data, (int*)handle, &err)) {
406 EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
411 if (!emcore_delete_mailbox(input_mailbox_id, on_server, &err)) {
412 EM_DEBUG_EXCEPTION("emcore_delete failed [%d]", err);
422 emcore_free_account(ref_account);
423 EM_SAFE_FREE(ref_account);
432 INTERNAL_FUNC int emdaemon_delete_mailbox_all(email_mailbox_t* mailbox, int* err_code)
434 EM_DEBUG_FUNC_BEGIN("malibox[%p], err_code[%p]", mailbox, err_code);
436 /* default variable */
438 int err = EMAIL_ERROR_NONE;
439 email_account_t* ref_account = NULL;
442 err = EMAIL_ERROR_INVALID_PARAM;
446 if (mailbox->account_id <= 0) {
447 EM_DEBUG_EXCEPTION("malibox->account_id[%d]", mailbox->account_id);
448 err = EMAIL_ERROR_INVALID_PARAM;
452 ref_account = emcore_get_account_reference(mailbox->account_id);
455 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", mailbox->account_id);
456 err = EMAIL_ERROR_INVALID_ACCOUNT;
460 if (!emcore_delete_mailbox_all(mailbox, &err)) {
461 EM_DEBUG_EXCEPTION("emcore_delete_all failed [%d]", err);
470 emcore_free_account(ref_account);
471 EM_SAFE_FREE(ref_account);
481 INTERNAL_FUNC int emdaemon_sync_header(int input_account_id, int input_mailbox_id, int *handle, int* err_code)
483 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);
485 /* default variable */
487 int err = EMAIL_ERROR_NONE;
489 email_event_t event_data;
490 email_account_t* ref_account = NULL;
492 memset(&event_data, 0x00, sizeof(email_event_t));
494 if (input_mailbox_id < 0) {
495 EM_DEBUG_EXCEPTION("parameter is invalid");
496 err = EMAIL_ERROR_INVALID_ACCOUNT;
500 if(input_account_id == ALL_ACCOUNT) {
501 EM_DEBUG_LOG(">>>> emdaemon_sync_header for all account event_data.event_param_data_4 [%d]", event_data.event_param_data_4);
502 event_data.type = EMAIL_EVENT_SYNC_HEADER;
503 event_data.account_id = input_account_id;
504 event_data.event_param_data_5 = input_mailbox_id;
505 /* In case of Mailbox NULL, we need to set arg as EMAIL_SYNC_ALL_MAILBOX */
506 if (input_mailbox_id == 0)
507 event_data.event_param_data_4 = EMAIL_SYNC_ALL_MAILBOX;
509 if (!emcore_insert_event(&event_data, (int*)handle, &err)) {
510 EM_DEBUG_EXCEPTION("emcore_insert_event falied [%d]", err);
516 if (!(ref_account = emcore_get_account_reference(input_account_id))) {
517 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", input_account_id);
518 err = EMAIL_ERROR_INVALID_ACCOUNT;
522 /* Modified the code to sync all mailbox in a Single Event */
523 event_data.type = EMAIL_EVENT_SYNC_HEADER;
524 event_data.account_id = input_account_id;
525 event_data.event_param_data_5 = input_mailbox_id;
526 /* In case of Mailbox NULL, we need to set arg as EMAIL_SYNC_ALL_MAILBOX */
527 if (input_mailbox_id == 0)
528 event_data.event_param_data_4 = EMAIL_SYNC_ALL_MAILBOX;
529 EM_DEBUG_LOG(">>>> EVENT ARG [ %d ] ", event_data.event_param_data_4);
531 if (!emcore_insert_event(&event_data, (int*)handle, &err)) {
532 EM_DEBUG_EXCEPTION("emcore_insert_event falied [%d]", err);
537 #ifdef __FEATURE_LOCAL_ACTIVITY__
538 EM_DEBUG_LOG("Setting g_local_activity_run ");
539 g_local_activity_run = 1;
547 emcore_free_account(ref_account);
548 EM_SAFE_FREE(ref_account);
557 INTERNAL_FUNC int emdaemon_set_mail_slot_size_of_mailbox(int account_id, int mailbox_id, int new_slot_size, int *handle, int *err_code)
559 EM_DEBUG_FUNC_BEGIN("account_id [%d], mailbox_id[%d], handle[%p], err_code[%p]", account_id, mailbox_id, handle, err_code);
562 int err = EMAIL_ERROR_NONE;
563 email_event_t event_data;
566 EM_DEBUG_EXCEPTION("handle is required");
567 err = EMAIL_ERROR_INVALID_PARAM;
571 memset(&event_data, 0x00, sizeof(email_event_t));
573 event_data.type = EMAIL_EVENT_SET_MAIL_SLOT_SIZE;
574 event_data.account_id = account_id;
575 event_data.event_param_data_4 = mailbox_id;
576 event_data.event_param_data_5 = new_slot_size;
578 if (!emcore_insert_event(&event_data, (int*)handle, &err)) {
579 EM_DEBUG_EXCEPTION("emcore_insert_event falied [%d]", err);
592 INTERNAL_FUNC int emdaemon_rename_mailbox(int input_mailbox_id, char *input_mailbox_path, char *input_mailbox_alias, int input_on_server, int *output_handle)
594 EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d], input_mailbox_path [%p], input_mailbox_alias[%p], input_on_server [%d], output_handle[%p]", input_mailbox_id, input_mailbox_path, input_mailbox_alias, input_on_server, output_handle);
596 int err = EMAIL_ERROR_NONE;
597 emstorage_account_tbl_t *account_data = NULL;
598 emstorage_mailbox_tbl_t *old_mailbox_data = NULL;
599 email_event_t event_data;
601 if(input_mailbox_id <= 0 || output_handle == NULL) {
602 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
603 err = EMAIL_ERROR_INVALID_PARAM;
607 if (input_on_server) {
608 if ((err = emstorage_get_mailbox_by_id(input_mailbox_id, &old_mailbox_data)) != EMAIL_ERROR_NONE) {
609 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
613 if (!emstorage_get_account_by_id(old_mailbox_data->account_id, EMAIL_ACC_GET_OPT_DEFAULT, &account_data, true, &err)) {
614 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed [%d]", err);
618 if (account_data->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) {
619 memset(&event_data, 0x00, sizeof(email_event_t));
621 event_data.type = EMAIL_EVENT_RENAME_MAILBOX_ON_IMAP_SERVER;
622 event_data.event_param_data_1 = EM_SAFE_STRDUP(old_mailbox_data->mailbox_name);
623 event_data.event_param_data_2 = EM_SAFE_STRDUP(input_mailbox_path);
624 event_data.event_param_data_3 = EM_SAFE_STRDUP(input_mailbox_alias);
625 event_data.event_param_data_4 = input_mailbox_id;
626 event_data.account_id = old_mailbox_data->account_id;
628 if (!emcore_insert_event(&event_data, (int*)output_handle, &err)) {
629 EM_DEBUG_EXCEPTION("emcore_insert_event falied [%d]", err);
635 if ((err = emstorage_rename_mailbox(input_mailbox_id, input_mailbox_path, input_mailbox_alias, true)) != EMAIL_ERROR_NONE) {
636 EM_DEBUG_EXCEPTION("emstorage_rename_mailbox failed [%d]", err);
642 if (old_mailbox_data)
643 emstorage_free_mailbox(&old_mailbox_data, 1, NULL);
646 emstorage_free_account(&account_data, 1, NULL);
648 EM_DEBUG_FUNC_END("err [%d]", err);