Tizen 2.0 Release
[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
46 #ifdef __FEATURE_LOCAL_ACTIVITY__
47 extern int g_local_activity_run;
48 #endif
49
50 INTERNAL_FUNC int emdaemon_get_imap_mailbox_list(int account_id, char* mailbox, int *handle, int* err_code)
51 {
52         EM_DEBUG_FUNC_BEGIN("account_id[%d] mailbox[%p] err_code[%p]", account_id, mailbox, err_code);
53
54         /*  default variable */
55         int ret = false;
56         int err = EMAIL_ERROR_NONE;
57         email_account_t* ref_account = NULL;
58
59         if (account_id <= 0 ||!mailbox)  {
60                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox[%p]", account_id, mailbox);
61                 err = EMAIL_ERROR_INVALID_PARAM;
62                 goto FINISH_OFF;
63         }
64
65         ref_account = emcore_get_account_reference(account_id);
66
67         if (!ref_account)  {
68                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
69                 err = EMAIL_ERROR_INVALID_ACCOUNT;
70                 goto FINISH_OFF;
71         }
72
73         email_event_t event_data;
74         memset(&event_data, 0x00, sizeof(email_event_t));
75
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);
79
80         if (!emcore_insert_event(&event_data, (int*)handle, &err))  {
81                 EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
82                 goto FINISH_OFF;
83         }
84
85         ret = true;
86
87 FINISH_OFF:
88
89         if (ref_account) {
90                 emcore_free_account(ref_account);
91                 EM_SAFE_FREE(ref_account);
92         }
93
94         if (err_code)
95                 *err_code = err;
96         EM_DEBUG_FUNC_END();
97         return ret;
98 }
99
100 INTERNAL_FUNC int emdaemon_get_mailbox_list(int account_id, email_mailbox_t** mailbox_list, int* count, int* err_code)
101 {
102         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], count[%p], err_code[%p]", account_id, mailbox_list, count, err_code);
103
104         int ret = false;
105         int err = EMAIL_ERROR_NONE;
106         email_account_t* ref_account = NULL;
107
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;
111                 goto FINISH_OFF;
112         }
113
114         ref_account = emcore_get_account_reference(account_id);
115
116         if (!ref_account)  {
117                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
118                 err = EMAIL_ERROR_INVALID_ACCOUNT;
119                 goto FINISH_OFF;
120         }
121
122         if (!emcore_get_mailbox_list(account_id, mailbox_list, count, &err))  {
123                 EM_DEBUG_EXCEPTION("emcore_get_mailbox_list failed [%d]", err);
124                 goto FINISH_OFF;
125         }
126
127         ret = true;
128
129 FINISH_OFF:
130
131         if (ref_account) {
132                 emcore_free_account(ref_account);
133                 EM_SAFE_FREE(ref_account);
134         }
135
136         if (err_code)
137                 *err_code = err;
138         EM_DEBUG_FUNC_END();
139         return ret;
140 }
141
142
143 INTERNAL_FUNC int emdaemon_get_mail_count_of_mailbox(email_mailbox_t* mailbox, int* total, int* unseen, int* err_code)
144 {
145         EM_DEBUG_FUNC_BEGIN("mailbox[%p], total[%p], unseen[%p], err_code[%p]", mailbox, total, unseen, err_code);
146
147         /*  default variable */
148         int ret = false;
149         int err = EMAIL_ERROR_NONE;
150         email_account_t* ref_account = NULL;
151
152         if (!mailbox || !total || !unseen)  {
153                 EM_DEBUG_EXCEPTION("mailbox[%p], total[%p], unseen[%p]", mailbox, total, unseen);
154                 err = EMAIL_ERROR_INVALID_PARAM;
155                 goto FINISH_OFF;
156         }
157
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;
162                 goto FINISH_OFF;
163         }
164
165         if (!emcore_get_mail_count(mailbox, total, unseen, &err))  {
166                 EM_DEBUG_EXCEPTION("emcore_get_mail_count failed [%d]", err);
167                 goto FINISH_OFF;
168         }
169
170
171         ret = true;
172
173 FINISH_OFF:
174
175         if (ref_account) {
176                 emcore_free_account(ref_account);
177                 EM_SAFE_FREE(ref_account);
178         }
179
180         if (err_code)
181                 *err_code = err;
182         EM_DEBUG_FUNC_END();
183         return ret;
184 }
185
186 INTERNAL_FUNC int emdaemon_add_mailbox(email_mailbox_t* new_mailbox, int on_server, int *handle, int* err_code)
187 {
188         EM_DEBUG_FUNC_BEGIN("new_mailbox[%p], err_code[%p]", new_mailbox, err_code);
189
190         int ret = false;;
191         int err = EMAIL_ERROR_NONE;
192         email_account_t* ref_account = NULL;
193
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;
198                 goto FINISH_OFF;
199         }
200
201         ref_account = emcore_get_account_reference(new_mailbox->account_id);
202
203         if (!ref_account)  {
204                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", new_mailbox->account_id);
205                 err = EMAIL_ERROR_INVALID_ACCOUNT;
206                 goto FINISH_OFF;
207         }
208
209         email_event_t event_data;
210         memset(&event_data, 0x00, sizeof(email_event_t));
211
212
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 )
215                 on_server = 0;
216
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);
226                         goto FINISH_OFF;
227                 }
228         }
229         else {  /*  sync */
230                 if (!emcore_create_mailbox(new_mailbox, on_server, &err))  {
231                         EM_DEBUG_EXCEPTION("emcore_create failed [%d]", err);
232                         goto FINISH_OFF;
233                 }
234         }
235
236         ret = true;
237
238 FINISH_OFF:
239
240         if (ref_account) {
241                 emcore_free_account(ref_account);
242                 EM_SAFE_FREE(ref_account);
243         }
244
245         if (err_code)
246                 *err_code = err;
247         EM_DEBUG_FUNC_END();
248         return ret;
249 }
250
251
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)
253 {
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);
255
256         /*  default variable */
257         int ret = false;;
258         int err = EMAIL_ERROR_NONE;
259         email_account_t* ref_account = NULL;
260
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);
268
269                 err = EMAIL_ERROR_INVALID_PARAM;
270                 goto FINISH_OFF;
271         }
272
273         ref_account = emcore_get_account_reference(old_mailbox->account_id);
274
275         if (!ref_account)  {
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; */
278                 goto FINISH_OFF;
279         }
280
281         email_event_t event_data;
282         memset(&event_data, 0x00, sizeof(email_event_t));
283
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);
287                 goto FINISH_OFF;
288         }
289
290         ret = true;
291
292 FINISH_OFF:
293
294         if (ref_account) {
295                 emcore_free_account(ref_account);
296                 EM_SAFE_FREE(ref_account);
297         }
298
299         if (err_code)
300                 *err_code = err;
301         EM_DEBUG_FUNC_END();
302         return ret;
303 }
304
305 INTERNAL_FUNC int emdaemon_set_mailbox_type(int input_mailbox_id, email_mailbox_type_e input_mailbox_type)
306 {
307         EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d], input_mailbox_type [%d]", input_mailbox_id, input_mailbox_type);
308
309         /*  default variable */
310         int err = EMAIL_ERROR_NONE;
311         emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
312
313         if (!input_mailbox_id)  {
314                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
315                 err = EMAIL_ERROR_INVALID_PARAM;
316                 goto FINISH_OFF;
317         }
318
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);
321                 goto FINISH_OFF;
322         }
323
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);
326                 goto FINISH_OFF;
327         }
328
329
330 FINISH_OFF:
331         if (mailbox_tbl)
332                 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
333
334         EM_DEBUG_FUNC_END("err [%d]", err);
335         return err;
336 }
337
338 INTERNAL_FUNC int emdaemon_set_local_mailbox(int input_mailbox_id, int input_is_local_mailbox)
339 {
340         EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d], input_mailbox_type [%d]", input_mailbox_id, input_is_local_mailbox);
341
342         /*  default variable */
343         int err = EMAIL_ERROR_NONE;
344
345         if (input_mailbox_id <= 0)  {
346                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
347                 err = EMAIL_ERROR_INVALID_PARAM;
348                 goto FINISH_OFF;
349         }
350
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);
353                 goto FINISH_OFF;
354         }
355
356
357 FINISH_OFF:
358
359         EM_DEBUG_FUNC_END("err [%d]", err);
360         return err;
361 }
362
363 INTERNAL_FUNC int emdaemon_delete_mailbox(int input_mailbox_id, int on_server, int *handle, int* err_code)
364 {
365         EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], err_code[%p]", input_mailbox_id, err_code);
366
367         /*  default variable */
368         int ret = false;
369         int err = EMAIL_ERROR_NONE;
370         emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
371         email_account_t* ref_account = NULL;
372
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);
375                 goto FINISH_OFF;
376         }
377
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;
382                 goto FINISH_OFF;
383         }
384
385         ref_account = emcore_get_account_reference(mailbox_tbl->account_id);
386
387         if (!ref_account)  {
388                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", mailbox_tbl->account_id);
389                 err = EMAIL_ERROR_INVALID_ACCOUNT;
390                 goto FINISH_OFF;
391         }
392
393         email_event_t event_data;
394         memset(&event_data, 0x00, sizeof(email_event_t));
395
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 ) {
398                 on_server = 0;
399         }
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);
407                         goto FINISH_OFF;
408                 }
409         }
410         else {
411                 if (!emcore_delete_mailbox(input_mailbox_id, on_server,  &err))  {
412                         EM_DEBUG_EXCEPTION("emcore_delete failed [%d]", err);
413                         goto FINISH_OFF;
414                 }
415         }
416
417         ret = true;
418
419 FINISH_OFF:
420
421         if (ref_account) {
422                 emcore_free_account(ref_account);
423                 EM_SAFE_FREE(ref_account);
424         }
425
426         if (err_code)
427                 *err_code = err;
428         EM_DEBUG_FUNC_END();
429         return ret;
430 }
431
432 INTERNAL_FUNC int emdaemon_delete_mailbox_all(email_mailbox_t* mailbox, int* err_code)
433 {
434         EM_DEBUG_FUNC_BEGIN("malibox[%p], err_code[%p]", mailbox, err_code);
435
436         /*  default variable */
437         int ret = false;
438         int err = EMAIL_ERROR_NONE;
439         email_account_t* ref_account = NULL;
440
441         if (!mailbox)  {
442                 err = EMAIL_ERROR_INVALID_PARAM;
443                 goto FINISH_OFF;
444         }
445
446         if (mailbox->account_id <= 0)  {
447                 EM_DEBUG_EXCEPTION("malibox->account_id[%d]", mailbox->account_id);
448                 err = EMAIL_ERROR_INVALID_PARAM;
449                 goto FINISH_OFF;
450         }
451
452         ref_account = emcore_get_account_reference(mailbox->account_id);
453
454         if (!ref_account)  {
455                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", mailbox->account_id);
456                 err = EMAIL_ERROR_INVALID_ACCOUNT;
457                 goto FINISH_OFF;
458         }
459
460         if (!emcore_delete_mailbox_all(mailbox, &err))  {
461                 EM_DEBUG_EXCEPTION("emcore_delete_all failed [%d]", err);
462                 goto FINISH_OFF;
463         }
464
465         ret = true;
466
467 FINISH_OFF:
468
469         if (ref_account) {
470                 emcore_free_account(ref_account);
471                 EM_SAFE_FREE(ref_account);
472         }
473
474         if (err_code)
475                 *err_code = err;
476         EM_DEBUG_FUNC_END();
477         return ret;
478 }
479
480
481 INTERNAL_FUNC int emdaemon_sync_header(int input_account_id, int input_mailbox_id, int *handle, int* err_code)
482 {
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);
484
485         /*  default variable */
486         int ret = false;
487         int err = EMAIL_ERROR_NONE;
488
489         email_event_t event_data;
490         email_account_t* ref_account = NULL;
491
492         memset(&event_data, 0x00, sizeof(email_event_t));
493
494         if (input_mailbox_id < 0) {
495                 EM_DEBUG_EXCEPTION("parameter is invalid");
496                 err = EMAIL_ERROR_INVALID_ACCOUNT;
497                 goto FINISH_OFF;
498         }
499
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;
508
509                 if (!emcore_insert_event(&event_data, (int*)handle, &err))   {
510                         EM_DEBUG_EXCEPTION("emcore_insert_event falied [%d]", err);
511                         goto FINISH_OFF;
512                 }
513         }
514         else {
515
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;
519                         goto FINISH_OFF;
520                 }
521
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);
530
531                 if (!emcore_insert_event(&event_data, (int*)handle, &err))   {
532                         EM_DEBUG_EXCEPTION("emcore_insert_event falied [%d]", err);
533                         goto FINISH_OFF;
534                 }
535         }
536
537 #ifdef __FEATURE_LOCAL_ACTIVITY__
538         EM_DEBUG_LOG("Setting g_local_activity_run ");
539         g_local_activity_run = 1;
540 #endif
541
542         ret = true;
543
544 FINISH_OFF:
545
546         if (ref_account) {
547                 emcore_free_account(ref_account);
548                 EM_SAFE_FREE(ref_account);
549         }
550
551         if (err_code)
552                 *err_code = err;
553         EM_DEBUG_FUNC_END();
554         return ret;
555 }
556
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)
558 {
559         EM_DEBUG_FUNC_BEGIN("account_id [%d], mailbox_id[%d], handle[%p], err_code[%p]", account_id, mailbox_id, handle, err_code);
560
561         int ret = false;
562         int err = EMAIL_ERROR_NONE;
563         email_event_t event_data;
564
565         if(handle == NULL) {
566                 EM_DEBUG_EXCEPTION("handle is required");
567                 err = EMAIL_ERROR_INVALID_PARAM;
568                 goto FINISH_OFF;
569         }
570
571         memset(&event_data, 0x00, sizeof(email_event_t));
572
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;
577
578         if (!emcore_insert_event(&event_data, (int*)handle, &err))   {
579                 EM_DEBUG_EXCEPTION("emcore_insert_event falied [%d]", err);
580                 goto FINISH_OFF;
581         }
582
583         ret = true;
584
585 FINISH_OFF:
586         if (err_code)
587                 *err_code = err;
588         EM_DEBUG_FUNC_END();
589         return ret;
590 }
591
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)
593 {
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);
595
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;
600
601         if(input_mailbox_id <= 0 || output_handle == NULL) {
602                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
603                 err = EMAIL_ERROR_INVALID_PARAM;
604                 goto FINISH_OFF;
605         }
606
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);
610                         goto FINISH_OFF;
611                 }
612
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);
615                         goto FINISH_OFF;
616                 }
617
618                 if (account_data->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) {
619                         memset(&event_data, 0x00, sizeof(email_event_t));
620
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;
627
628                         if (!emcore_insert_event(&event_data, (int*)output_handle, &err)) {
629                                 EM_DEBUG_EXCEPTION("emcore_insert_event falied [%d]", err);
630                                 goto FINISH_OFF;
631                         }
632                 }
633         }
634         else {
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);
637                         goto FINISH_OFF;
638                 }
639         }
640
641 FINISH_OFF:
642         if (old_mailbox_data)
643                 emstorage_free_mailbox(&old_mailbox_data, 1, NULL);
644
645         if (account_data)
646                 emstorage_free_account(&account_data, 1, NULL);
647
648         EM_DEBUG_FUNC_END("err [%d]", err);
649         return err;
650 }