Fixed the bug for tizen 3.0
[platform/core/messaging/email-service.git] / email-daemon / email-daemon-mailbox.c
1 /*
2 *  email-service
3 *
4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5 *
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
7 *
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
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
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.
19 *
20 */
21
22
23
24 /******************************************************************************
25  * File: email-daemon-mailbox.c
26  * Desc: email-daemon Mailbox Operation
27  *
28  * Auth:
29  *
30  * History:
31  *    2006.08.16 : created
32  *****************************************************************************/
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36
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"
47
48 #ifdef __FEATURE_LOCAL_ACTIVITY__
49 extern int g_local_activity_run;
50 #endif
51
52 INTERNAL_FUNC int emdaemon_get_imap_mailbox_list(char *multi_user_name, int account_id, char* mailbox, int *handle, int* err_code)
53 {
54         EM_DEBUG_FUNC_BEGIN("account_id[%d] mailbox[%p] err_code[%p]", account_id, mailbox, err_code);
55
56         int ret = false;
57         int err = EMAIL_ERROR_NONE;
58         email_event_t *event_data = NULL;
59
60         if (account_id <= 0 ||!mailbox)  {
61                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox[%p]", account_id, mailbox);
62                 err = EMAIL_ERROR_INVALID_PARAM;
63                 goto FINISH_OFF;
64         }
65
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;
70                 goto FINISH_OFF;
71         }
72
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);
77
78         if (!emcore_insert_event(event_data, (int*)handle, &err)) {
79                 EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
80                 goto FINISH_OFF;
81         }
82
83         ret = true;
84
85 FINISH_OFF:
86
87         if (ret == false && event_data) {
88                 emcore_free_event(event_data);
89                 EM_SAFE_FREE(event_data);
90         }
91
92         if (err_code)
93                 *err_code = err;
94         EM_DEBUG_FUNC_END();
95         return ret;
96 }
97
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)
99 {
100         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], count[%p], err_code[%p]", account_id, mailbox_list, count, err_code);
101
102         int ret = false;
103         int err = EMAIL_ERROR_NONE;
104         email_account_t* ref_account = NULL;
105
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;
109                 goto FINISH_OFF;
110         }
111
112         ref_account = emcore_get_account_reference(multi_user_name, account_id, false);
113         if (!ref_account)  {
114                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
115                 err = EMAIL_ERROR_INVALID_ACCOUNT;
116                 goto FINISH_OFF;
117         }
118
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);
121                 goto FINISH_OFF;
122         }
123
124         ret = true;
125
126 FINISH_OFF:
127
128         if (ref_account) {
129                 emcore_free_account(ref_account);
130                 EM_SAFE_FREE(ref_account);
131         }
132
133         if (err_code)
134                 *err_code = err;
135         EM_DEBUG_FUNC_END();
136         return ret;
137 }
138
139
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)
141 {
142         EM_DEBUG_FUNC_BEGIN("mailbox[%p], total[%p], unseen[%p], err_code[%p]", mailbox, total, unseen, err_code);
143
144         /*  default variable */
145         int ret = false;
146         int err = EMAIL_ERROR_NONE;
147         email_account_t* ref_account = NULL;
148
149         if (!mailbox || !total || !unseen)  {
150                 EM_DEBUG_EXCEPTION("mailbox[%p], total[%p], unseen[%p]", mailbox, total, unseen);
151                 err = EMAIL_ERROR_INVALID_PARAM;
152                 goto FINISH_OFF;
153         }
154
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;
159                 goto FINISH_OFF;
160         }
161
162         if (!emcore_get_mail_count(multi_user_name, mailbox, total, unseen, &err))  {
163                 EM_DEBUG_EXCEPTION("emcore_get_mail_count failed [%d]", err);
164                 goto FINISH_OFF;
165         }
166
167
168         ret = true;
169
170 FINISH_OFF:
171
172         if (ref_account) {
173                 emcore_free_account(ref_account);
174                 EM_SAFE_FREE(ref_account);
175         }
176
177         if (err_code)
178                 *err_code = err;
179         EM_DEBUG_FUNC_END();
180         return ret;
181 }
182
183 INTERNAL_FUNC int emdaemon_add_mailbox(char *multi_user_name, email_mailbox_t* new_mailbox, int on_server, int *handle, int* err_code)
184 {
185         EM_DEBUG_FUNC_BEGIN("new_mailbox[%p], err_code[%p]", new_mailbox, err_code);
186
187         int ret = false;
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;
192
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;
197                 goto FINISH_OFF;
198         }
199
200         ref_account = emcore_get_account_reference(multi_user_name, new_mailbox->account_id, false);
201         if (!ref_account)  {
202                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", new_mailbox->account_id);
203                 err = EMAIL_ERROR_INVALID_ACCOUNT;
204                 goto FINISH_OFF;
205         }
206
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 )
209                 on_server = 0;
210
211         if ( on_server ) {      /* async */
212
213                 mailbox = em_malloc(sizeof(email_mailbox_t));
214
215                 if(mailbox == NULL) {
216                         EM_DEBUG_EXCEPTION("em_malloc failed");
217                         err = EMAIL_ERROR_OUT_OF_MEMORY;
218                         goto FINISH_OFF;
219                 }
220
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);
224
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;
230                                 goto FINISH_OFF;
231                         }
232                         memcpy(mailbox->eas_data, new_mailbox->eas_data, new_mailbox->eas_data_length);
233                 }
234
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;
239                         goto FINISH_OFF;
240                 }
241
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);                       
247
248                 if(!emcore_insert_event(event_data, (int*)handle, &err)) {
249                         EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
250                         goto FINISH_OFF;
251                 }
252         } else {        /* sync */
253                 if (!emcore_create_mailbox(multi_user_name, new_mailbox, on_server, -1, -1, &err))  {
254                         EM_DEBUG_EXCEPTION("emcore_create failed [%d]", err);
255                         goto FINISH_OFF;
256                 }
257         }
258
259         ret = true;
260
261 FINISH_OFF:
262
263         if (ret == false) {
264                 if(event_data) {
265                         emcore_free_event(event_data);
266                         EM_SAFE_FREE(event_data);
267                 }
268                 if(mailbox) {
269                         emcore_free_mailbox(mailbox);
270                         EM_SAFE_FREE(mailbox);
271                 }
272         }
273
274         if (ref_account) {
275                 emcore_free_account(ref_account);
276                 EM_SAFE_FREE(ref_account);
277         }
278
279         if (err_code)
280                 *err_code = err;
281         EM_DEBUG_FUNC_END();
282         return ret;
283 }
284
285 INTERNAL_FUNC int emdaemon_set_mailbox_type(char *multi_user_name, int input_mailbox_id, email_mailbox_type_e input_mailbox_type)
286 {
287         EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d], input_mailbox_type [%d]", input_mailbox_id, input_mailbox_type);
288
289         /*  default variable */
290         int err = EMAIL_ERROR_NONE;
291         emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
292
293         if (!input_mailbox_id)  {
294                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
295                 err = EMAIL_ERROR_INVALID_PARAM;
296                 goto FINISH_OFF;
297         }
298
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);
301                 goto FINISH_OFF;
302         }
303
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);
306                 goto FINISH_OFF;
307         }
308
309
310 FINISH_OFF:
311
312         if (mailbox_tbl)
313                 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
314
315         EM_DEBUG_FUNC_END("err [%d]", err);
316         return err;
317 }
318
319 INTERNAL_FUNC int emdaemon_set_local_mailbox(char *multi_user_name, int input_mailbox_id, int input_is_local_mailbox)
320 {
321         EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d], input_mailbox_type [%d]", input_mailbox_id, input_is_local_mailbox);
322
323         /*  default variable */
324         int err = EMAIL_ERROR_NONE;
325
326         if (input_mailbox_id <= 0)  {
327                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
328                 err = EMAIL_ERROR_INVALID_PARAM;
329                 goto FINISH_OFF;
330         }
331
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);
334                 goto FINISH_OFF;
335         }
336
337
338 FINISH_OFF:
339
340         EM_DEBUG_FUNC_END("err [%d]", err);
341         return err;
342 }
343
344 INTERNAL_FUNC int emdaemon_delete_mailbox(char *multi_user_name, int input_mailbox_id, int on_server, int *handle, int* err_code)
345 {
346         EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], err_code[%p]", input_mailbox_id, err_code);
347
348         int ret = false;
349         int err = EMAIL_ERROR_NONE;
350         int recursive = 1;
351         emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
352         email_account_t *ref_account = NULL;
353         email_event_t *event_data = NULL;
354
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);
357                 goto FINISH_OFF;
358         }
359
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;
364                 goto FINISH_OFF;
365         }
366
367         ref_account = emcore_get_account_reference(multi_user_name, mailbox_tbl->account_id, false);
368         if (!ref_account) {
369                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", mailbox_tbl->account_id);
370                 err = EMAIL_ERROR_INVALID_ACCOUNT;
371                 goto FINISH_OFF;
372         }
373
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 ) {
376                 on_server = 0;
377                 recursive = 0;
378         }
379
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;
385                         goto FINISH_OFF;
386                 }
387
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);
394
395                 if(!emcore_insert_event(event_data, (int*)handle, &err)) {
396                         EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
397                         goto FINISH_OFF;
398                 }
399         } else {
400                 if (!emcore_delete_mailbox(multi_user_name, input_mailbox_id, on_server, recursive))  {
401                         EM_DEBUG_EXCEPTION("emcore_delete failed [%d]", err);
402                         goto FINISH_OFF;
403                 }
404         }
405
406         ret = true;
407
408 FINISH_OFF:
409
410         if (ret == false && event_data) {
411                 emcore_free_event(event_data);
412                 EM_SAFE_FREE(event_data);
413         }
414
415         if (ref_account) {
416                 emcore_free_account(ref_account);
417                 EM_SAFE_FREE(ref_account);
418         }
419
420         if (err_code)
421                 *err_code = err;
422         EM_DEBUG_FUNC_END();
423         return ret;
424 }
425
426 INTERNAL_FUNC int emdaemon_delete_mailbox_all(char *multi_user_name, email_mailbox_t* mailbox, int* err_code)
427 {
428         EM_DEBUG_FUNC_BEGIN("malibox[%p], err_code[%p]", mailbox, err_code);
429
430         /*  default variable */
431         int ret = false;
432         int err = EMAIL_ERROR_NONE;
433         email_account_t* ref_account = NULL;
434
435         if (!mailbox)  {
436                 err = EMAIL_ERROR_INVALID_PARAM;
437                 goto FINISH_OFF;
438         }
439
440         if (mailbox->account_id <= 0)  {
441                 EM_DEBUG_EXCEPTION("malibox->account_id[%d]", mailbox->account_id);
442                 err = EMAIL_ERROR_INVALID_PARAM;
443                 goto FINISH_OFF;
444         }
445
446         ref_account = emcore_get_account_reference(multi_user_name, mailbox->account_id, false);
447         if (!ref_account)  {
448                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", mailbox->account_id);
449                 err = EMAIL_ERROR_INVALID_ACCOUNT;
450                 goto FINISH_OFF;
451         }
452
453         if (!emcore_delete_mailbox_all(multi_user_name, mailbox, &err))  {
454                 EM_DEBUG_EXCEPTION("emcore_delete_all failed [%d]", err);
455                 goto FINISH_OFF;
456         }
457
458         ret = true;
459
460 FINISH_OFF:
461
462         if (ref_account) {
463                 emcore_free_account(ref_account);
464                 EM_SAFE_FREE(ref_account);
465         }
466
467         if (err_code)
468                 *err_code = err;
469         EM_DEBUG_FUNC_END();
470         return ret;
471 }
472
473
474 INTERNAL_FUNC int emdaemon_sync_header(char *multi_user_name, int input_account_id, int input_mailbox_id, int *handle, int* err_code)
475 {
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);
477
478         int ret = false;
479         int err = EMAIL_ERROR_NONE;
480         email_event_t *event_data = NULL;
481
482         if (input_mailbox_id < 0 || handle == NULL) {
483                 EM_DEBUG_EXCEPTION("parameter is invalid");
484                 err = EMAIL_ERROR_INVALID_PARAM;
485                 goto FINISH_OFF;
486         }
487
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;
492                 goto FINISH_OFF;
493         }
494
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);               
501
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;
505
506                 if (!emcore_insert_event(event_data, (int*)handle, &err)) {
507                         EM_DEBUG_EXCEPTION("emcore_insert_event falied [%d]", err);
508                         goto FINISH_OFF;
509                 }
510         } else {
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);               
516
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);
521
522                 if (!emcore_insert_event(event_data, (int*)handle, &err)) {
523                         EM_DEBUG_EXCEPTION("emcore_insert_event falied [%d]", err);
524                         goto FINISH_OFF;
525                 }
526         }
527
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 >>>> ");
533
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);
536 */
537
538
539 #ifdef __FEATURE_LOCAL_ACTIVITY__
540         EM_DEBUG_LOG("Setting g_local_activity_run ");
541         g_local_activity_run = 1;
542 #endif
543
544         ret = true;
545
546 FINISH_OFF:
547
548         if (ret == false && event_data) {
549                 emcore_free_event(event_data);
550                 EM_SAFE_FREE(event_data);
551         }
552
553         if (err_code)
554                 *err_code = err;
555         EM_DEBUG_FUNC_END();
556         return ret;
557 }
558
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)
560 {
561         EM_DEBUG_FUNC_BEGIN("account_id [%d], mailbox_id[%d], handle[%p], err_code[%p]", account_id, mailbox_id, handle, err_code);
562
563         int ret = false;
564         int err = EMAIL_ERROR_NONE;
565         email_event_t *event_data = NULL;
566
567         if(handle == NULL) {
568                 EM_DEBUG_EXCEPTION("handle is required");
569                 err = EMAIL_ERROR_INVALID_PARAM;
570                 goto FINISH_OFF;
571         }
572
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;
577                 goto FINISH_OFF;
578         }
579
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);
585
586         if (!emcore_insert_event(event_data, (int*)handle, &err))   {
587                 EM_DEBUG_EXCEPTION("emcore_insert_event falied [%d]", err);
588                 goto FINISH_OFF;
589         }
590
591         ret = true;
592
593 FINISH_OFF:
594
595         if (ret == false && event_data) {
596                 emcore_free_event(event_data);
597                 EM_SAFE_FREE(event_data);
598         }
599
600         if (err_code)
601                 *err_code = err;
602         EM_DEBUG_FUNC_END();
603         return ret;
604 }
605
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)
607 {
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);
609
610         int ret = false;
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;
615
616         if(input_mailbox_id <= 0 || output_handle == NULL) {
617                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
618                 err = EMAIL_ERROR_INVALID_PARAM;
619                 goto FINISH_OFF;
620         }
621
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);
625                         goto FINISH_OFF;
626                 }
627
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);
630                         goto FINISH_OFF;
631                 }
632
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;
638                                 goto FINISH_OFF;
639                         }
640
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);
648
649                         if (!emcore_insert_event(event_data, (int*)output_handle, &err)) {
650                                 EM_DEBUG_EXCEPTION("emcore_insert_event falied [%d]", err);
651                                 goto FINISH_OFF;
652                         }
653                 }
654         } else {
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);
657                         goto FINISH_OFF;
658                 }
659         }
660
661         ret = true;
662
663 FINISH_OFF:
664
665         if (ret == false && event_data) {
666                 emcore_free_event(event_data);
667                 EM_SAFE_FREE(event_data);
668         }
669
670         if (old_mailbox_data)
671                 emstorage_free_mailbox(&old_mailbox_data, 1, NULL);
672
673         if (account_data)
674                 emstorage_free_account(&account_data, 1, NULL);
675
676         EM_DEBUG_FUNC_END("err [%d]", err);
677         return err;
678 }