Tizen 2.0 Release
[platform/core/messaging/email-service.git] / email-core / email-core-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  * File :  email-core-mailbox.c
25  * Desc :  Local Mailbox Management
26  *
27  * Auth : 
28  *
29  *****************************************************************************/
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <time.h>
34 #include <sys/types.h>
35 #include "email-types.h"
36 #include "email-utilities.h"
37 #include "email-convert.h"
38 #include "email-debug-log.h"
39 #include "email-core-global.h"
40 #include "email-core-utils.h"
41 #include "email-core-mailbox.h"
42 #include "email-core-event.h"
43 #include "email-network.h"
44 #include "email-core-mail.h"
45 #include "email-core-imap-mailbox.h"   
46 #include "email-storage.h"
47 #include "email-core-account.h" 
48
49 #ifdef __FEATURE_KEEP_CONNECTION__
50 static void *g_receiving_thd_stream = NULL;                     /* Stores the recv thd stream for next time reuse */
51 static int prev_acc_id_recv_thd = 0;                            /* Stores the account id for which recv thd stream is open */
52 extern int recv_thread_run;
53
54 static void *g_partial_body_thd_stream = NULL;          /* Stores the pb thd stream for next time reuse */
55 static int prev_acc_id_pb_thd = 0;                                      /* Stores the account id for which pb thd stream is open */
56
57 __thread email_connection_info_t *g_connection_info_list = NULL;
58
59 static pthread_mutex_t _close_stream_lock = PTHREAD_MUTEX_INITIALIZER;  /* Mutex to protect closing stream */
60 #endif /*  __FEATURE_KEEP_CONNECTION__ */
61
62
63 /*  Binding IMAP mailbox with its function */
64 static email_mailbox_type_item_t  g_mailbox_type[MAX_MAILBOX_TYPE] = {
65                                 {EMAIL_MAILBOX_TYPE_INBOX,   "INBOX" },
66                                 /*  Naver */
67                                 {EMAIL_MAILBOX_TYPE_INBOX,   "Inbox" },
68                                 {EMAIL_MAILBOX_TYPE_SENTBOX, "Sent Messages"} , 
69                                 {EMAIL_MAILBOX_TYPE_SPAMBOX, "&wqTTOLpUx3zVaA-"} , 
70                                 {EMAIL_MAILBOX_TYPE_DRAFT,   "Drafts"} ,
71                                 {EMAIL_MAILBOX_TYPE_TRASH,   "Deleted Messages" } ,
72                                 /*  AOL */
73                                 {EMAIL_MAILBOX_TYPE_SENTBOX, "Sent"} , 
74                                 {EMAIL_MAILBOX_TYPE_SPAMBOX, "Spam" }, 
75                                 {EMAIL_MAILBOX_TYPE_DRAFT,   "Drafts"} ,
76                                 {EMAIL_MAILBOX_TYPE_TRASH,   "Trash"},
77                                 /* DAUM */
78                                 {EMAIL_MAILBOX_TYPE_SPAMBOX, "&wqTTONO4ycDVaA-"},
79                                 /* ETC */
80                                 {EMAIL_MAILBOX_TYPE_SENTBOX, "mail/sent-mail"}, 
81                                 {EMAIL_MAILBOX_TYPE_SPAMBOX, "mail/spam-mail" }, 
82                                 {EMAIL_MAILBOX_TYPE_DRAFT,   "mail/saved-drafts"} ,
83                                 {EMAIL_MAILBOX_TYPE_TRASH,   "mail/mail-trash"},
84 };
85
86 #ifdef __FEATURE_KEEP_CONNECTION__
87 email_connection_info_t* emcore_get_connection_info_by_account_id(int account_id)
88 {
89         EM_DEBUG_FUNC_BEGIN("account_id [%d]", account_id);
90         email_connection_info_t *connection_info = g_connection_info_list;
91
92         while(connection_info) {
93                 if(connection_info->account_id == account_id)
94                         break;
95                 connection_info = connection_info->next;
96         }
97         
98         EM_DEBUG_FUNC_END("connection_info [%p]", connection_info);
99         return connection_info;
100 }
101
102 int emcore_append_connection_info(email_connection_info_t *new_connection_info)
103 {
104         EM_DEBUG_FUNC_BEGIN("new_connection_info [%p]", new_connection_info);
105         email_connection_info_t *connection_info = g_connection_info_list;
106
107         if(!new_connection_info) {
108                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
109                 return EMAIL_ERROR_INVALID_PARAM;
110         }
111
112         if(emcore_get_connection_info_by_account_id(new_connection_info->account_id)) {
113                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_ALREADY_EXISTS");
114                 return EMAIL_ERROR_ALREADY_EXISTS;
115         }
116
117         if(connection_info) {
118                 while(connection_info) {
119                         if(connection_info->next == NULL) {
120                                 connection_info->next = new_connection_info;
121                                 new_connection_info->next = NULL;
122                         }
123                         connection_info = connection_info->next;
124                 }
125         }
126         else {
127                 new_connection_info->next = NULL;
128                 g_connection_info_list = new_connection_info;
129         }
130         
131         EM_DEBUG_FUNC_END("EMAIL_ERROR_NONE");
132         return EMAIL_ERROR_NONE;
133 }
134
135 INTERNAL_FUNC int emcore_remove_connection_info(int account_id)
136 {
137         EM_DEBUG_FUNC_BEGIN("account_id [%d]", account_id);
138         email_connection_info_t *connection_info = g_connection_info_list, *prev_connection_info = NULL;
139
140         while(connection_info) {
141                 if(connection_info->account_id == account_id) {
142                         if(prev_connection_info) {
143                                 prev_connection_info->next = connection_info->next;
144                         }
145                         else {
146                                 g_connection_info_list = connection_info->next;
147                         }
148                         EM_SAFE_FREE(connection_info);
149                         break;
150                 }
151                 prev_connection_info = connection_info;
152                 connection_info = connection_info->next;
153         }
154         
155         EM_DEBUG_FUNC_END("");
156         return EMAIL_ERROR_NONE;
157 }
158
159 #endif /* __FEATURE_KEEP_CONNECTION__ */
160
161
162 /* description
163  *    get local mailbox list
164  */
165 INTERNAL_FUNC int emcore_get_mailbox_list(int account_id, email_mailbox_t **mailbox_list,       int *p_count, int *err_code)
166 {
167         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], p_count[%p], err_code[%p]", account_id, mailbox_list, p_count, err_code);
168         
169         if (account_id <= 0 || !mailbox_list || !p_count)  {
170                 EM_DEBUG_EXCEPTION("PARAM Failed account_id[%d], mailbox_list[%p], p_count[%p]", account_id, mailbox_list, p_count);
171                 if (err_code != NULL)
172                         *err_code = EMAIL_ERROR_INVALID_PARAM;
173                 return false;
174         }
175         
176         int ret = false;
177         int error = EMAIL_ERROR_NONE;
178         emstorage_mailbox_tbl_t *local_mailbox_list = NULL;
179         email_account_t *ref_account = NULL;
180         int i, count = 512;
181         
182         /* get mailbox list from mailbox table */
183         
184         if (!(ref_account = emcore_get_account_reference(account_id)))  {
185                 EM_DEBUG_EXCEPTION(" emcore_get_account_reference failed - %d", account_id);
186                 error = EMAIL_ERROR_INVALID_ACCOUNT;
187                 goto FINISH_OFF;
188         }
189         
190         if (!emstorage_get_mailbox_list(ref_account->account_id, EMAIL_MAILBOX_ALL, EMAIL_MAILBOX_SORT_BY_NAME_ASC, &count, &local_mailbox_list, true, &error))  {      
191                 EM_DEBUG_EXCEPTION(" emstorage_get_mailbox failed - %d", error);
192         
193                 goto FINISH_OFF;
194         }
195         
196         if (count > 0)  {
197                 if (!(*mailbox_list = em_malloc(sizeof(email_mailbox_t) * count)))  {
198                         EM_DEBUG_EXCEPTION(" mailloc failed...");
199                         error = EMAIL_ERROR_OUT_OF_MEMORY;
200                         goto FINISH_OFF;
201                 }
202                 
203                 memset(*mailbox_list, 0x00, (sizeof(email_mailbox_t) * count));
204                 
205                 for (i = 0; i < count; i++)  {
206                         em_convert_mailbox_tbl_to_mailbox(local_mailbox_list + i, (*mailbox_list) + i);
207                         /*
208                         (*mailbox_list)[i].mailbox_id = local_mailbox_list[i].mailbox_id;
209                         (*mailbox_list)[i].account_id = account_id;
210                         (*mailbox_list)[i].mailbox_name = local_mailbox_list[i].mailbox_name; local_mailbox_list[i].mailbox_name = NULL;
211                         (*mailbox_list)[i].alias = local_mailbox_list[i].alias; local_mailbox_list[i].alias = NULL;
212                         (*mailbox_list)[i].local = local_mailbox_list[i].local_yn;              
213                         (*mailbox_list)[i].mailbox_type = local_mailbox_list[i].mailbox_type;   
214                         (*mailbox_list)[i].unread_count = local_mailbox_list[i].unread_count;
215                         (*mailbox_list)[i].total_mail_count_on_local = local_mailbox_list[i].total_mail_count_on_local;
216                         (*mailbox_list)[i].total_mail_count_on_server = local_mailbox_list[i].total_mail_count_on_server;
217                         (*mailbox_list)[i].mail_slot_size = local_mailbox_list[i].mail_slot_size;
218                         (*mailbox_list)[i].no_select = local_mailbox_list[i].no_select;
219                         (*mailbox_list)[i].last_sync_time = local_mailbox_list[i].last_sync_time;
220                         */
221                 }
222         }
223         else
224                 mailbox_list = NULL;
225
226         * p_count = count;
227
228         ret = true;
229
230 FINISH_OFF: 
231         if (local_mailbox_list != NULL)
232                 emstorage_free_mailbox(&local_mailbox_list, count, NULL);
233
234         if (ref_account) {
235                 emcore_free_account(ref_account);
236                 EM_SAFE_FREE(ref_account);
237         }
238         
239         if (err_code != NULL)
240                 *err_code = error;
241
242         EM_DEBUG_FUNC_END();
243         return ret;
244 }
245
246 /* description
247  *    get imap sync mailbox list
248  */
249 int emcore_get_mailbox_list_to_be_sync(int account_id, email_mailbox_t **mailbox_list, int *p_count, int *err_code)
250 {
251         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], p_count[%p], err_code[%p]", account_id, mailbox_list, p_count, err_code);
252         
253         if (account_id <= 0 || !mailbox_list || !p_count)  {
254                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_list[%p], p_count[%p]", account_id, mailbox_list, p_count);
255                 if (err_code != NULL)
256                         *err_code = EMAIL_ERROR_INVALID_PARAM;
257                 return false;
258         }
259
260         int ret = false;
261         int error = EMAIL_ERROR_NONE;
262         email_mailbox_t *tmp_mailbox_list = NULL;
263         emstorage_mailbox_tbl_t *mailbox_tbl_list = NULL;
264         email_account_t *ref_account = NULL;
265         int i, count = 512;
266         
267         /* get mailbox list from mailbox table */
268         if (!(ref_account = emcore_get_account_reference(account_id)))  {
269                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed - %d", account_id);
270                 error = EMAIL_ERROR_INVALID_ACCOUNT;
271                 goto FINISH_OFF;
272         }
273         
274         if (!emstorage_get_mailbox_list(ref_account->account_id, 0, EMAIL_MAILBOX_SORT_BY_TYPE_ASC, &count, &mailbox_tbl_list, true, &error))  {        
275                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox failed - %d", error);
276         
277                 goto FINISH_OFF;
278         }
279         
280         if (count > 0)  {
281                 if (!(tmp_mailbox_list = em_malloc(sizeof(email_mailbox_t) * count)))  {
282                         EM_DEBUG_EXCEPTION("malloc failed...");
283                         error = EMAIL_ERROR_OUT_OF_MEMORY;
284                         goto FINISH_OFF;
285                 }
286                 
287                 memset(tmp_mailbox_list, 0x00, (sizeof(email_mailbox_t) * count));
288                 
289                 for (i = 0; i < count; i++)  {
290                         em_convert_mailbox_tbl_to_mailbox(mailbox_tbl_list + i, tmp_mailbox_list + i);
291                         /*
292                         tmp_mailbox_list[i].mailbox_id = mailbox_tbl_list[i].mailbox_id;
293                         tmp_mailbox_list[i].account_id = account_id;
294                         tmp_mailbox_list[i].mailbox_name = mailbox_tbl_list[i].mailbox_name; mailbox_tbl_list[i].mailbox_name = NULL;
295                         tmp_mailbox_list[i].mailbox_type = mailbox_tbl_list[i].mailbox_type; 
296                         tmp_mailbox_list[i].alias = mailbox_tbl_list[i].alias; mailbox_tbl_list[i].alias = NULL;
297                         tmp_mailbox_list[i].local = mailbox_tbl_list[i].local_yn;
298                         tmp_mailbox_list[i].unread_count = mailbox_tbl_list[i].unread_count;
299                         tmp_mailbox_list[i].total_mail_count_on_local = mailbox_tbl_list[i].total_mail_count_on_local;
300                         tmp_mailbox_list[i].total_mail_count_on_server = mailbox_tbl_list[i].total_mail_count_on_server;
301                         tmp_mailbox_list[i].mail_slot_size = mailbox_tbl_list[i].mail_slot_size;
302                         */
303                 }
304         }
305         else
306                 tmp_mailbox_list = NULL;
307         *p_count = count;
308         ret = true;
309         
310 FINISH_OFF: 
311         
312         *mailbox_list = tmp_mailbox_list;
313         
314         if (ref_account) {
315                 emcore_free_account(ref_account);
316                 EM_SAFE_FREE(ref_account);
317         }
318         
319         if (mailbox_tbl_list != NULL)
320                 emstorage_free_mailbox(&mailbox_tbl_list, count, NULL);
321         
322         if (err_code != NULL)
323                 *err_code = error;
324         EM_DEBUG_FUNC_END("error [%d]", error);
325         return ret;
326 }
327
328 INTERNAL_FUNC int emcore_get_mail_count(email_mailbox_t *mailbox, int *total, int *unseen, int *err_code)
329 {
330         EM_DEBUG_FUNC_BEGIN("mailbox[%p], total[%p], unseen[%p], err_code[%p]", mailbox, total, unseen, err_code);
331         
332         int ret = false;
333         int err = EMAIL_ERROR_NONE;
334         
335         if (!mailbox)  {
336                 EM_DEBUG_EXCEPTION(" mailbox[%p], total[%p], unseen[%p]", mailbox, total, unseen);
337                 err = EMAIL_ERROR_INVALID_PARAM;
338                 goto FINISH_OFF;
339         }
340         
341         if (!emstorage_get_mail_count(mailbox->account_id, mailbox->mailbox_name, total, unseen, true, &err))  {
342                 EM_DEBUG_EXCEPTION(" emstorage_get_mail_count failed - %d", err);
343
344                 goto FINISH_OFF;
345         }
346
347         
348         ret = true;
349         
350 FINISH_OFF: 
351         if (err_code != NULL)
352                 *err_code = err;
353         
354         return ret;
355 }
356
357 INTERNAL_FUNC int emcore_create_mailbox(email_mailbox_t *new_mailbox, int on_server, int *err_code)
358 {
359         EM_DEBUG_FUNC_BEGIN("new_mailbox[%p], err_code[%p]", new_mailbox, err_code);
360         int ret = false;
361         int err = EMAIL_ERROR_NONE;
362         emstorage_mailbox_tbl_t local_mailbox;
363         
364         if (new_mailbox == NULL || new_mailbox->mailbox_name == NULL)  {
365                 err = EMAIL_ERROR_INVALID_PARAM;
366                 goto FINISH_OFF;
367         }
368
369         if  (on_server) {
370                 /* Create a mailbox from Sever */
371                 if (!emcore_create_imap_mailbox(new_mailbox, &err)) {
372                         EM_DEBUG_EXCEPTION(">>>>> mailbox Creation in Server FAILED >>> ");
373                         goto FINISH_OFF;
374                 }
375                 else
376                         EM_DEBUG_LOG(">>>>> mailbox Creation in Server SUCCESS >>> ");  
377         }
378
379         memset(&local_mailbox, 0x00, sizeof(emstorage_mailbox_tbl_t));
380         EM_DEBUG_LOG("box name[%s] local yn[%d] mailbox_type[%d]", new_mailbox->mailbox_name, local_mailbox.local_yn, new_mailbox->mailbox_type);
381
382         /* add local mailbox into local mailbox table */
383         local_mailbox.mailbox_id = new_mailbox->mailbox_id;
384         local_mailbox.account_id = new_mailbox->account_id;
385         local_mailbox.local_yn = new_mailbox->local;
386         local_mailbox.mailbox_name = new_mailbox->mailbox_name;
387         local_mailbox.alias = new_mailbox->alias;
388         local_mailbox.mailbox_type = new_mailbox->mailbox_type;
389         local_mailbox.unread_count = 0;
390         local_mailbox.total_mail_count_on_local = 0;
391         local_mailbox.total_mail_count_on_server = 0;
392         emcore_get_default_mail_slot_count(local_mailbox.account_id, &local_mailbox.mail_slot_size);
393
394         if (strncmp(new_mailbox->mailbox_name, EMAIL_INBOX_NAME, EM_SAFE_STRLEN(EMAIL_INBOX_NAME))    == 0 || 
395                 strncmp(new_mailbox->mailbox_name, EMAIL_DRAFTBOX_NAME, EM_SAFE_STRLEN(EMAIL_DRAFTBOX_NAME)) == 0 ||
396                 strncmp(new_mailbox->mailbox_name, EMAIL_OUTBOX_NAME, EM_SAFE_STRLEN(EMAIL_OUTBOX_NAME)) == 0 || 
397                 strncmp(new_mailbox->mailbox_name, EMAIL_SENTBOX_NAME, EM_SAFE_STRLEN(EMAIL_SENTBOX_NAME))  == 0)
398                 local_mailbox.modifiable_yn = 0;                        /*  can be deleted/modified */
399         else
400                 local_mailbox.modifiable_yn = 1;
401
402
403         if (!emstorage_add_mailbox(&local_mailbox, true, &err))  {
404                 EM_DEBUG_EXCEPTION("emstorage_add_mailbox failed [%d]", err);
405                 goto FINISH_OFF;
406         }
407
408         new_mailbox->mailbox_id = local_mailbox.mailbox_id;
409         ret = true;
410         
411 FINISH_OFF: 
412         if (err_code)
413                 *err_code = err;
414         
415         return ret;
416 }
417
418 INTERNAL_FUNC int emcore_delete_mailbox(int input_mailbox_id, int on_server, int *err_code)
419 {
420         EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], err_code[%p]", input_mailbox_id, err_code);
421         
422         int ret = false;
423         int err = EMAIL_ERROR_NONE;
424         emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
425         
426         if (input_mailbox_id == 0)  {
427                 EM_DEBUG_EXCEPTION(" input_mailbox_id == 0");
428                 err = EMAIL_ERROR_INVALID_PARAM;
429                 goto FINISH_OFF;
430         }
431         
432         if ((err = emstorage_get_mailbox_by_id(input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE || !mailbox_tbl) {
433                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
434                 goto FINISH_OFF;
435         }
436
437         if (on_server) {
438                 EM_DEBUG_LOG("Delete the mailbox in Sever >>> ");
439                 if  (!emcore_delete_imap_mailbox(input_mailbox_id, &err))
440                         EM_DEBUG_EXCEPTION("Delete the mailbox in server : failed [%d]", err);
441                 else
442                         EM_DEBUG_LOG("Delete the mailbox in server : success");
443         }
444
445         if (!emcore_delete_all_mails_of_mailbox(mailbox_tbl->account_id, input_mailbox_id, false, &err))  {
446                 EM_DEBUG_EXCEPTION("emcore_delete_all_mails_of_mailbox failed [%d]", err);
447                 goto FINISH_OFF;
448         }
449
450         if (!emstorage_delete_mailbox(mailbox_tbl->account_id, -1, input_mailbox_id, true, &err))  {
451                 EM_DEBUG_EXCEPTION(" emstorage_delete_mailbox failed - %d", err);
452
453                 goto FINISH_OFF;
454         }
455         
456         ret = true;
457         
458 FINISH_OFF:
459         if (mailbox_tbl)
460                 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
461
462         if (err_code != NULL)
463                 *err_code = err;
464         EM_DEBUG_FUNC_END("err[%d]", err);
465         return ret;
466 }
467
468
469 INTERNAL_FUNC int emcore_delete_mailbox_ex(int input_account_id, int *input_mailbox_id_array, int input_mailbox_id_count, int input_on_server)
470 {
471         EM_DEBUG_FUNC_BEGIN("input_account_id [%d] input_mailbox_id_array[%p] input_mailbox_id_count[%d] input_on_server[%d]", input_mailbox_id_array, input_mailbox_id_array, input_mailbox_id_count, input_on_server);
472         int err = EMAIL_ERROR_NONE;
473         int i = 0;
474
475         if(input_account_id == 0 || input_mailbox_id_count <= 0 || input_mailbox_id_array == NULL) {
476                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
477                 err = EMAIL_ERROR_INVALID_PARAM;
478                 goto FINISH_OFF;
479         }
480
481         if((err = emstorage_set_field_of_mailbox_with_integer_value(input_account_id, input_mailbox_id_array, input_mailbox_id_count, "deleted_flag", 1, true)) != EMAIL_ERROR_NONE) {
482                 EM_DEBUG_EXCEPTION("emstorage_set_field_of_mailbox_with_integer_value failed[%d]", err);
483                 goto FINISH_OFF;
484         }
485
486         for(i = 0; i < input_mailbox_id_count; i++) {
487                 if(!emcore_delete_mailbox(input_mailbox_id_array[i] , input_on_server, &err)) {
488                         EM_DEBUG_EXCEPTION("emcore_delete_mailbox failed [%d]", err);
489                         goto FINISH_OFF;
490                 }
491         }
492
493 FINISH_OFF:
494         EM_DEBUG_FUNC_END("err[%d]", err);
495         return err;
496 }
497
498 INTERNAL_FUNC int emcore_delete_mailbox_all(email_mailbox_t *mailbox, int *err_code)
499 {
500         EM_DEBUG_FUNC_BEGIN(" mailbox[%p], err_code[%p]", mailbox, err_code);
501         
502         int ret = false;
503         int err = EMAIL_ERROR_NONE;
504
505         if (mailbox == NULL) {
506                 EM_DEBUG_EXCEPTION(" mailbox[%p]", mailbox);
507                 err = EMAIL_ERROR_INVALID_PARAM;
508                 goto FINISH_OFF;
509         }
510         
511         if (!emcore_delete_all_mails_of_mailbox(mailbox->account_id, mailbox->mailbox_id, 0, /*NULL, */ &err)) {
512                 EM_DEBUG_EXCEPTION(" emcore_delete_all_mails_of_mailbox failed - %d", err);
513                 
514                 goto FINISH_OFF;
515         }
516         
517         if (!emstorage_delete_mailbox(mailbox->account_id, -1, mailbox->mailbox_id, true, &err)) {
518                 EM_DEBUG_EXCEPTION(" emstorage_delete_mailbox failed - %d", err);
519                 
520
521                 goto FINISH_OFF;
522         }
523         
524         ret = true;
525         
526 FINISH_OFF: 
527         if (err_code != NULL)
528                 *err_code = err;
529         EM_DEBUG_FUNC_END("err[%d]", err);
530         return ret;
531 }
532
533 INTERNAL_FUNC int emcore_update_mailbox(email_mailbox_t *old_mailbox, email_mailbox_t *new_mailbox, int *err_code)
534 {
535         EM_DEBUG_FUNC_BEGIN("old_mailbox[%p], new_mailbox[%p], err_code[%p]", old_mailbox, new_mailbox, err_code);
536         
537         int ret = false;
538         int err = EMAIL_ERROR_NONE;
539         
540         if (old_mailbox == NULL || new_mailbox == NULL)  {
541                 EM_DEBUG_EXCEPTION("old_mailbox[%p], new_mailbox[%p]", old_mailbox, new_mailbox);
542                 
543                 err = EMAIL_ERROR_INVALID_PARAM;
544                 goto FINISH_OFF;
545         }
546         
547         emstorage_mailbox_tbl_t new_mailbox_tbl;
548         memset(&new_mailbox_tbl, 0x00, sizeof(emstorage_mailbox_tbl_t));
549         
550         /*  Support only updating mailbox_type */
551         new_mailbox_tbl.mailbox_type = new_mailbox->mailbox_type;
552
553         if (old_mailbox->mailbox_type != new_mailbox_tbl.mailbox_type) {
554                 if (!emstorage_update_mailbox_type(old_mailbox->account_id, -1, old_mailbox->mailbox_name, new_mailbox_tbl.mailbox_type, true, &err))  {
555                         EM_DEBUG_EXCEPTION("emstorage_update_mailbox failed - %d", err);
556                         goto FINISH_OFF;
557                 }
558         }
559
560         new_mailbox_tbl.mailbox_id                      = old_mailbox->mailbox_id;
561         new_mailbox_tbl.account_id                      = old_mailbox->account_id;
562         new_mailbox_tbl.mailbox_name            = new_mailbox->mailbox_name;
563         new_mailbox_tbl.mailbox_type            = new_mailbox->mailbox_type;
564         new_mailbox_tbl.alias                           = new_mailbox->alias;
565         new_mailbox_tbl.mail_slot_size          = new_mailbox->mail_slot_size;
566         new_mailbox_tbl.total_mail_count_on_server = new_mailbox->total_mail_count_on_server;
567         
568         if (!emstorage_update_mailbox(old_mailbox->account_id, -1, old_mailbox->mailbox_id, &new_mailbox_tbl, true, &err))  {
569                 EM_DEBUG_EXCEPTION("emstorage_update_mailbox failed - %d", err);
570
571                 goto FINISH_OFF;
572         }
573         
574         if (EM_SAFE_STRCMP(old_mailbox->mailbox_name, new_mailbox_tbl.mailbox_name) != 0) {
575                 if ( (err = emstorage_rename_mailbox(old_mailbox->mailbox_id, new_mailbox_tbl.mailbox_name, new_mailbox_tbl.alias, true)) != EMAIL_ERROR_NONE) {
576                         EM_DEBUG_EXCEPTION("emstorage_rename_mailbox failed [%d]", err);
577                         goto FINISH_OFF;
578                 }
579         }
580         
581         ret = true;
582         
583 FINISH_OFF: 
584         if (err_code)
585                 *err_code = err;
586         
587         return ret;
588 }
589
590 extern int try_auth;
591 extern int try_auth_smtp;
592
593 #ifdef __FEATURE_KEEP_CONNECTION__
594 extern long smtp_send(SENDSTREAM *stream, char *command, char *args);
595 #endif /* __FEATURE_KEEP_CONNECTION__ */
596
597 INTERNAL_FUNC int emcore_connect_to_remote_mailbox_with_account_info(email_account_t *account, int input_mailbox_id, void **result_stream, int *err_code)
598 {
599         EM_PROFILE_BEGIN(emCoreMailboxOpen);
600         EM_DEBUG_FUNC_BEGIN("account[%p], input_mailbox_id[%d], mail_stream[%p], err_code[%p]", account, input_mailbox_id, result_stream, err_code);
601         
602         int ret = false;
603         int error = EMAIL_ERROR_NONE;
604         email_session_t *session = NULL;
605         char *mbox_path = NULL;
606         void *reusable_stream = NULL;
607         int is_connection_for = _SERVICE_THREAD_TYPE_NONE;
608         emstorage_mailbox_tbl_t* mailbox = NULL;
609         char *mailbox_name = NULL;
610
611         if (account == NULL) {
612                 EM_DEBUG_EXCEPTION("Invalid Parameter.");
613                 error = EMAIL_ERROR_INVALID_PARAM;
614                 goto FINISH_OFF;
615         }
616         
617         if (!emcore_get_current_session(&session)) {
618                 EM_DEBUG_EXCEPTION("emcore_get_current_session failed...");
619                 error = EMAIL_ERROR_SESSION_NOT_FOUND;
620                 goto FINISH_OFF;
621         }
622
623         if (input_mailbox_id == 0 || input_mailbox_id != EMAIL_CONNECT_FOR_SENDING)
624                 is_connection_for = _SERVICE_THREAD_TYPE_RECEIVING;
625         else 
626                 is_connection_for = _SERVICE_THREAD_TYPE_SENDING;
627
628 #ifdef __FEATURE_KEEP_CONNECTION__
629         email_connection_info_t *connection_info = emcore_get_connection_info_by_account_id(account->account_id);
630
631         if(connection_info) {
632                 if (is_connection_for == _SERVICE_THREAD_TYPE_RECEIVING) {
633                         if(connection_info->receiving_server_stream_status == EMAIL_STREAM_STATUS_CONNECTED)
634                                 reusable_stream = connection_info->receiving_server_stream;
635                 }
636                 else {
637                         if(connection_info->sending_server_stream_status == EMAIL_STREAM_STATUS_CONNECTED)
638                                 reusable_stream = connection_info->sending_server_stream;
639                 }
640         }
641         
642         if (reusable_stream != NULL)
643                 EM_DEBUG_LOG("Stream reuse desired");
644 #else
645         reusable_stream = *result_stream;
646 #endif
647
648         session->error = EMAIL_ERROR_NONE;
649         emcore_set_network_error(EMAIL_ERROR_NONE);             /*  set current network error as EMAIL_ERROR_NONE before network operation */
650         
651         if (input_mailbox_id == EMAIL_CONNECT_FOR_SENDING) {
652                 mailbox_name = EM_SAFE_STRDUP(ENCODED_PATH_SMTP);
653         }
654         else if (input_mailbox_id == 0) {
655                 mailbox_name = NULL;
656         }else {
657                 if ( (error = emstorage_get_mailbox_by_id(input_mailbox_id, &mailbox)) != EMAIL_ERROR_NONE || !mailbox) {
658                         EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", error);
659                         goto FINISH_OFF;
660                 }
661                 mailbox_name = EM_SAFE_STRDUP(mailbox->mailbox_name);
662         }
663
664         if (is_connection_for == _SERVICE_THREAD_TYPE_RECEIVING) {      
665                 /*  open pop3/imap server */
666                 MAILSTREAM *mail_stream = NULL;
667                 
668                 if (!emcore_get_long_encoded_path_with_account_info(account, mailbox_name, '/', &mbox_path, &error)) {
669                         EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path failed - %d", error);
670                         session->error = error;
671                         goto FINISH_OFF;
672                 }
673                 
674                 EM_DEBUG_LOG("open mail connection to mbox_path [%s]", mbox_path);
675                 
676                 try_auth = 0;           /*  ref_account->receiving_auth ? 1  :  0  */
677                 session->auth = 0; /*  ref_account->receiving_auth ? 1  :  0 */
678                 
679                 if (!(mail_stream = mail_open(reusable_stream, mbox_path, IMAP_2004_LOG))) {    
680                         EM_DEBUG_EXCEPTION("mail_open failed. session->error[%d], session->network[%d]", session->error, session->network);
681                         
682                         if ((session->error == EMAIL_ERROR_UNKNOWN) || (session->error == EMAIL_ERROR_NONE))
683                                 session->error = EMAIL_ERROR_CONNECTION_FAILURE;
684                         
685                         error = session->error;
686
687 #ifdef __FEATURE_KEEP_CONNECTION__
688                         /* Since mail_open failed Reset the global stream pointer as it is a dangling pointer now */
689 #endif /*  __FEATURE_KEEP_CONNECTION__ */
690                         goto FINISH_OFF;
691                 }
692                 *result_stream = mail_stream;
693         }
694         else {  
695                 /*  open smtp server */
696                 SENDSTREAM *send_stream = NULL;
697                 char *host_list[2] = {NULL, NULL};
698
699 #ifdef __FEATURE_KEEP_CONNECTION__
700                 if (reusable_stream != NULL) {
701                         int send_ret = 0;
702                         /* Check whether connection is avaiable */
703                         send_stream     = reusable_stream;
704                         /*
705                         send_ret = smtp_send(send_stream, "RSET", 0);
706                         
707                         if (send_ret != SMTP_RESPONSE_OK) {
708                                 EM_DEBUG_EXCEPTION("[SMTP] RSET --> [%s]", send_stream->reply);
709                                 send_stream = NULL;
710                         }
711                         */
712                 }
713 #endif
714                 if(!send_stream) {
715                         if (!emcore_get_long_encoded_path_with_account_info(account, mailbox_name, 0, &mbox_path, &error)) {
716                                 EM_DEBUG_EXCEPTION(" emcore_get_long_encoded_path failed - %d", error);
717                                 session->error = error;
718                                 goto FINISH_OFF;
719                         }
720                                 
721                         EM_DEBUG_LOG("open SMTP connection to mbox_path [%s]", mbox_path);
722                         
723                         try_auth_smtp = account->outgoing_server_need_authentication ? 1  :  0;
724                         session->auth = account->outgoing_server_need_authentication ? 1  :  0;
725                         
726                         host_list[0] = mbox_path;
727                         
728                         if (!(send_stream = smtp_open(host_list, 1))) {
729                                 EM_DEBUG_EXCEPTION("smtp_open failed... : current outgoing_server_secure_connection[%d] session->error[%d] session->network[%d]",
730                                         account->outgoing_server_secure_connection, session->error, session->network);
731                                 if (session->network != EMAIL_ERROR_NONE)
732                                         session->error = session->network;
733                                 if ((session->error == EMAIL_ERROR_UNKNOWN) || (session->error == EMAIL_ERROR_NONE))
734                                         session->error = EMAIL_ERROR_CONNECTION_FAILURE;
735                                 
736                                 error = session->error;
737                                 goto FINISH_OFF;
738                         }
739                 }
740                 *result_stream = send_stream;
741         }
742         
743         ret = true;
744         
745 FINISH_OFF: 
746
747 #ifdef __FEATURE_KEEP_CONNECTION__
748         if (ret == true) {
749                 if(!connection_info) {
750                         connection_info = em_malloc(sizeof(email_connection_info_t));
751                         connection_info->account_id = account->account_id;
752                         if(!connection_info) 
753                                 EM_DEBUG_EXCEPTION("em_malloc for connection_info failed.");
754                         else
755                                 emcore_append_connection_info(connection_info);
756                 }
757
758                 if(connection_info) {
759                         /* connection_info->account_id = account->account_id; */
760                         if (is_connection_for == _SERVICE_THREAD_TYPE_RECEIVING) {
761                                 connection_info->receiving_server_stream      = *result_stream;
762                                 connection_info->receiving_server_stream_status = EMAIL_STREAM_STATUS_CONNECTED;
763                         }
764                         else {
765                                 connection_info->sending_server_stream        = *result_stream;
766                                 connection_info->sending_server_stream_status = EMAIL_STREAM_STATUS_CONNECTED;
767                         }
768                 }
769         }
770 #endif
771
772         EM_SAFE_FREE(mbox_path);
773
774         EM_SAFE_FREE(mailbox_name);
775
776         if (mailbox) {
777                 emstorage_free_mailbox(&mailbox, 1, NULL);
778         }
779
780         if (err_code != NULL)
781                 *err_code = error;
782         EM_PROFILE_END(emCoreMailboxOpen);
783         EM_DEBUG_FUNC_END("ret [%d]", ret);     
784         return ret;
785 }
786
787 #ifdef __FEATURE_KEEP_CONNECTION__
788
789 /*h.gahlaut@samsung.com :  20-oct-2010*/
790 /*Precaution :  When Reuse Stream feature is enabled then stream should only be closed using emcore_close_mailbox from email-service code.
791 mail_close should not be used directly from anywhere in code.
792 emcore_close_mailbox uses mail_close inside it.
793
794 mail_close is only used in emcore_connect_to_remote_mailbox and emcore_reset_streams as an exception to above rule*/
795
796 INTERNAL_FUNC int emcore_connect_to_remote_mailbox(int account_id, char *mailbox, void **mail_stream, int *err_code)
797 {
798         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], mail_stream[%p], err_code[%p]", account_id, mailbox, mail_stream, err_code);
799         
800         int ret = false;
801         int error = EMAIL_ERROR_NONE;
802         email_account_t *ref_account = NULL;
803
804         ref_account = emcore_get_account_reference(account_id);
805
806         if (!ref_account) {             
807                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed - account id[%d]", account_id);
808                 error = EMAIL_ERROR_INVALID_ACCOUNT;            
809                 goto FINISH_OFF;
810         }
811         
812         ret = emcore_connect_to_remote_mailbox_with_account_info(ref_account, mailbox, mail_stream, &error);
813
814 FINISH_OFF:
815
816         if (ref_account) {
817                 emcore_free_account(ref_account);
818                 EM_SAFE_FREE(ref_account);
819         }
820
821         if (err_code)
822                 *err_code = error;
823         EM_DEBUG_FUNC_END("ret [%d]", ret);
824         return ret;
825 }
826
827 INTERNAL_FUNC void emcore_close_mailbox_receiving_stream()
828 {
829         EM_DEBUG_FUNC_BEGIN("recv_thread_run [%d]", recv_thread_run);
830         if (!recv_thread_run) {
831                 ENTER_CRITICAL_SECTION(_close_stream_lock);
832                 mail_close(g_receiving_thd_stream);
833                 g_receiving_thd_stream = NULL;
834                 prev_acc_id_recv_thd = 0;
835                 LEAVE_CRITICAL_SECTION(_close_stream_lock);
836         }
837         EM_DEBUG_FUNC_END();
838 }
839
840 INTERNAL_FUNC void emcore_close_mailbox_partial_body_stream()
841 {
842         EM_DEBUG_FUNC_BEGIN();
843         if (false == emcore_get_pbd_thd_state()) {
844                 EM_DEBUG_LOG("emcore_get_pbd_thd_state returned false");
845                 mail_close(g_partial_body_thd_stream);
846                 g_partial_body_thd_stream = NULL;
847                 prev_acc_id_pb_thd = 0;
848         }
849         EM_DEBUG_FUNC_END();
850 }
851
852 /* h.gahlaut@samsung.com :  21-10-2010 - 
853 emcore_reset_stream() function is used to reset globally stored partial body thread and receiving thread streams 
854 on account deletion and pdp deactivation */
855
856 INTERNAL_FUNC void emcore_reset_streams()
857 {
858         EM_DEBUG_FUNC_BEGIN();
859
860         emcore_close_mailbox_receiving_stream();
861         emcore_close_mailbox_partial_body_stream();
862         
863         EM_DEBUG_FUNC_END();
864         return;
865 }
866
867 #else /*  __FEATURE_KEEP_CONNECTION__ */
868
869 INTERNAL_FUNC int emcore_connect_to_remote_mailbox(int account_id, int input_mailbox_id, void **mail_stream, int *err_code)
870 {
871         EM_DEBUG_FUNC_BEGIN("account_id[%d], input_mailbox_id[%d], mail_stream[%p], err_code[%p]", account_id, input_mailbox_id, mail_stream, err_code);
872         
873         int ret = false;
874         int error = EMAIL_ERROR_NONE;
875         email_session_t *session = NULL;
876         email_account_t *ref_account = NULL;
877
878         ref_account = emcore_get_account_reference(account_id);
879
880         if (!ref_account)  {            
881                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed - account id[%d]", account_id);
882                 error = EMAIL_ERROR_INVALID_ACCOUNT;            
883                 goto FINISH_OFF;
884         }
885
886         if (!emcore_check_thread_status()) {
887                 error = EMAIL_ERROR_CANCELLED;
888                 goto FINISH_OFF;
889         }
890
891         if (!emnetwork_check_network_status(&error)) {
892                 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", error);
893                 goto FINISH_OFF;
894         }
895
896         if (!emcore_get_empty_session(&session)) {
897                 EM_DEBUG_EXCEPTION("emcore_get_empty_session failed...");
898                 error = EMAIL_ERROR_SESSION_NOT_FOUND;
899                 goto FINISH_OFF;
900         }
901
902         ret = emcore_connect_to_remote_mailbox_with_account_info(ref_account, input_mailbox_id, mail_stream, &error);
903
904 FINISH_OFF: 
905
906         if (ref_account) {
907                 emcore_free_account(ref_account);
908                 EM_SAFE_FREE(ref_account);
909         }
910
911         emcore_clear_session(session);
912         
913         if (err_code)
914                 *err_code = error;
915         
916         return ret;
917 }
918 #endif  /*  __FEATURE_KEEP_CONNECTION__ */
919
920 INTERNAL_FUNC int emcore_close_mailbox(int account_id, void *mail_stream)
921 {
922         EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_stream[%p]", account_id, mail_stream);
923         
924         if (!mail_stream)  {
925                 EM_DEBUG_EXCEPTION("Invalid parameter");
926                 return false;
927         }
928         
929 #ifdef __FEATURE_KEEP_CONNECTION__
930         thread_t thread_id = THREAD_SELF();
931
932         if (thread_id == (thread_t)emcore_get_receiving_thd_id()) {
933                 /*  Receiving thread - Dont' Free Reuse feature enabled  */
934         }
935         else if (thread_id == (thread_t)emcore_get_partial_body_thd_id()) {
936                 /*  Partial Body Download thread - Dont' Free Reuse feature enabled  */
937         }
938         else {
939                 /*  Some other thread so free stream */
940                 if (g_receiving_thd_stream != mail_stream && g_partial_body_thd_stream != mail_stream)
941                         mail_close((MAILSTREAM *)mail_stream);
942         }
943 #else
944         mail_close((MAILSTREAM *)mail_stream);
945 #endif  /*  __FEATURE_KEEP_CONNECTION__ */
946         EM_DEBUG_FUNC_END();
947         return true;
948 }
949
950 INTERNAL_FUNC void emcore_free_mailbox_list(email_mailbox_t **mailbox_list, int count)
951 {
952         EM_DEBUG_FUNC_BEGIN("mailbox_list[%p], count[%d]", mailbox_list, count);
953         
954         if (count <= 0 || !mailbox_list || !*mailbox_list)  {
955                 EM_DEBUG_EXCEPTION("INVALID_PARAM: mailbox_list[%p], count[%d]", mailbox_list, count);
956                 return;
957         }
958                 
959         email_mailbox_t *p = *mailbox_list;
960         int i;
961                 
962         for (i = 0; i < count; i++)
963                 emcore_free_mailbox(p+i);
964
965         EM_SAFE_FREE(p);
966         *mailbox_list = NULL;
967
968         EM_DEBUG_FUNC_END();
969 }
970
971
972 INTERNAL_FUNC void emcore_free_mailbox(email_mailbox_t *mailbox)
973 {
974         EM_DEBUG_FUNC_BEGIN();
975
976         if (!mailbox)  {
977                 EM_DEBUG_EXCEPTION("INVALID_PARAM");
978                 return;
979         }
980         
981         EM_SAFE_FREE(mailbox->mailbox_name);
982         EM_SAFE_FREE(mailbox->alias);
983         
984         EM_DEBUG_FUNC_END();
985 }
986
987
988 INTERNAL_FUNC int emcore_free_internal_mailbox(email_internal_mailbox_t **mailbox_list, int count, int *err_code)
989 {
990         EM_DEBUG_FUNC_BEGIN("mailbox_list[%p], count[%d], err_code[%p]", mailbox_list, count, err_code);
991
992         /*  default variable */
993         int ret = false;
994         int err = EMAIL_ERROR_NONE;
995
996         if (count > 0)  {
997                 if (!mailbox_list || !*mailbox_list)  {
998                         EM_DEBUG_EXCEPTION(" mailbox_list[%p], count[%d]", mailbox_list, count);
999
1000                         err = EMAIL_ERROR_INVALID_PARAM;
1001                         goto FINISH_OFF;
1002                 }
1003
1004                 email_internal_mailbox_t *p = *mailbox_list;
1005                 int i;
1006
1007                 /* EM_DEBUG_LOG("before loop"); */
1008                 for (i = 0; i < count; i++)  {
1009                         EM_SAFE_FREE(p[i].mailbox_name);
1010                         EM_SAFE_FREE(p[i].alias);
1011                 }
1012                 /* EM_DEBUG_LOG("p [%p]", p); */
1013                 free(p);
1014                 *mailbox_list = NULL;
1015         }
1016
1017         ret = true;
1018
1019 FINISH_OFF:
1020         if (err_code)
1021                 *err_code = err;
1022         EM_DEBUG_FUNC_END();
1023         return ret;
1024 }
1025
1026 INTERNAL_FUNC void emcore_bind_mailbox_type(email_internal_mailbox_t *mailbox_list)
1027 {
1028         EM_DEBUG_FUNC_BEGIN("mailbox_list[%p]", mailbox_list);
1029
1030         int i = 0;
1031         int bIsNotUserMailbox = false;
1032         email_mailbox_type_item_t   *pMailboxType1 = NULL ;
1033         
1034         for (i = 0 ; i < MAX_MAILBOX_TYPE ; i++) {
1035                 pMailboxType1 = g_mailbox_type + i;
1036                 if (0 == EM_SAFE_STRCMP(pMailboxType1->mailbox_name, mailbox_list->mailbox_name)) { /*prevent 24662*/
1037                         mailbox_list->mailbox_type = pMailboxType1->mailbox_type;
1038                         EM_DEBUG_LOG("mailbox_list->mailbox_type[%d]", mailbox_list->mailbox_type);
1039                         bIsNotUserMailbox = true;
1040                         break;
1041                 }
1042         }
1043
1044         if (false == bIsNotUserMailbox)
1045                 mailbox_list->mailbox_type = EMAIL_MAILBOX_TYPE_USER_DEFINED;
1046
1047         EM_DEBUG_FUNC_END();
1048 }
1049
1050 INTERNAL_FUNC int  emcore_send_mail_event(email_mailbox_t *mailbox, int mail_id , int *err_code)
1051 {
1052         EM_DEBUG_FUNC_BEGIN();
1053         
1054         int ret = false;
1055         int err = EMAIL_ERROR_NONE;
1056         int handle; 
1057         email_event_t event_data;
1058
1059         if (!mailbox || mailbox->account_id <= 0) {
1060                 EM_DEBUG_LOG(" mailbox[%p]", mailbox);
1061                 
1062                 err = EMAIL_ERROR_INVALID_PARAM;
1063                 goto FINISH_OFF;
1064         }
1065         memset(&event_data, 0x00, sizeof(email_event_t));
1066
1067         event_data.type = EMAIL_EVENT_SEND_MAIL;
1068         event_data.account_id = mailbox->account_id;
1069         event_data.event_param_data_4 = mail_id;
1070         event_data.event_param_data_1 = NULL;
1071         event_data.event_param_data_5 = mailbox->mailbox_id;
1072                         
1073         if (!emcore_insert_event_for_sending_mails(&event_data, &handle, &err))  {
1074                 EM_DEBUG_LOG(" emcore_insert_event failed - %d", err);
1075                 goto FINISH_OFF;
1076         }
1077         emcore_add_transaction_info(mail_id , handle , &err);
1078
1079         ret = true;
1080 FINISH_OFF: 
1081         if (err_code)
1082                 *err_code = err;
1083         
1084         return ret;
1085 }
1086
1087 INTERNAL_FUNC int emcore_partial_body_thd_local_activity_sync(int *is_event_inserted, int *err_code)
1088 {
1089         EM_DEBUG_FUNC_BEGIN();
1090         int activity_count = 0;
1091         int ret = false;
1092         int error = EMAIL_ERROR_NONE;
1093
1094         if (false == emstorage_get_pbd_activity_count(&activity_count, false, &error)) {
1095                 EM_DEBUG_LOG("emstorage_get_pbd_activity_count failed [%d]", error);
1096                 goto FINISH_OFF;
1097         }
1098
1099         if (activity_count > 0) {
1100
1101                 email_event_partial_body_thd pbd_event;
1102
1103                 /* Carefully initialise the event */ 
1104                 memset(&pbd_event, 0x00, sizeof(email_event_partial_body_thd));
1105
1106                 pbd_event.event_type = EMAIL_EVENT_LOCAL_ACTIVITY_SYNC_BULK_PBD;
1107                 pbd_event.activity_type = EMAIL_EVENT_LOCAL_ACTIVITY_SYNC_BULK_PBD;
1108
1109                 if (false == emcore_insert_partial_body_thread_event(&pbd_event, &error)) {
1110                         EM_DEBUG_LOG(" emcore_insert_partial_body_thread_event failed [%d]", error);
1111                         goto FINISH_OFF;
1112                 }
1113                 else {
1114                         /*Not checking for NULL here because is_event_inserted is never NULL. */
1115                         *is_event_inserted = true;
1116                 }
1117                 
1118         }
1119         else {
1120                 *is_event_inserted = false;     
1121         }
1122
1123         ret = true;
1124         
1125         FINISH_OFF: 
1126
1127         if (NULL != err_code) {
1128                 *err_code = error;
1129         }
1130
1131         return ret;
1132 }
1133
1134 INTERNAL_FUNC int emcore_get_mailbox_by_type(int account_id, email_mailbox_type_e mailbox_type, email_mailbox_t *result_mailbox, int *err_code)
1135 {
1136         EM_DEBUG_FUNC_BEGIN("account_id [%d], result_mailbox [%p], err_code [%p]", account_id, result_mailbox, err_code);
1137         int ret = false, err = EMAIL_ERROR_NONE;
1138         emstorage_mailbox_tbl_t *mail_box_tbl_spam = NULL;
1139
1140         if (result_mailbox == NULL)     {       
1141                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1142                 err = EMAIL_ERROR_INVALID_PARAM;
1143                 goto FINISH_OFF;
1144         }
1145
1146         if (!emstorage_get_mailbox_by_mailbox_type(account_id, mailbox_type, &mail_box_tbl_spam, false, &err)) {
1147
1148                 EM_DEBUG_LOG("emstorage_get_mailbox_by_mailbox_type failed - %d", err);
1149         }
1150         else {  
1151                 if (mail_box_tbl_spam) {
1152                         result_mailbox->mailbox_type = mail_box_tbl_spam->mailbox_type;
1153                         result_mailbox->mailbox_name = EM_SAFE_STRDUP(mail_box_tbl_spam->mailbox_name);
1154                         result_mailbox->account_id = mail_box_tbl_spam->account_id;
1155                         result_mailbox->mail_slot_size = mail_box_tbl_spam->mail_slot_size;
1156                         if (!emstorage_free_mailbox(&mail_box_tbl_spam, 1, &err))
1157                                 EM_DEBUG_EXCEPTION(" emstorage_free_mailbox Failed [%d]", err);
1158                         ret = true;     
1159                 }
1160         }
1161
1162 FINISH_OFF: 
1163         if (err_code)
1164                 *err_code = err;
1165         EM_DEBUG_FUNC_END();
1166         return ret;
1167 }
1168
1169 #ifdef __FEATURE_LOCAL_ACTIVITY__
1170 INTERNAL_FUNC int emcore_local_activity_sync(int account_id, int *err_code)
1171 {
1172         EM_DEBUG_FUNC_BEGIN();
1173
1174         EM_DEBUG_LOG(">> account_id [%d], err_code [%p] ", account_id, err_code);
1175         
1176         int *activity_id_list = NULL;
1177         int activity_count = 0;
1178         int err         = 0;
1179         int handle = 0;
1180         int ret = false;
1181         email_event_t event_data;
1182
1183
1184         memset(&event_data, 0x00, sizeof(email_event_t));
1185         
1186
1187         EM_IF_NULL_RETURN_VALUE(err_code, false);
1188
1189         if (account_id <= 0) {
1190                 EM_DEBUG_EXCEPTION(" Invalid Account ID [%d] ", account_id);
1191                 return false;
1192         }
1193                 EM_DEBUG_LOG(">>> emdaemon_sync_local_activity 3 ");
1194
1195         if (!emstorage_get_activity_id_list(account_id, &activity_id_list, &activity_count, ACTIVITY_DELETEMAIL, ACTIVITY_COPYMAIL, true,  &err)) {
1196                         EM_DEBUG_LOG(">>> emdaemon_sync_local_activity 4 ");
1197                 EM_DEBUG_EXCEPTION(" emstorage_get_activity_id_list failed [ %d] ", err);
1198                 goto FINISH_OFF;
1199         }
1200         EM_DEBUG_LOG(">>> emdaemon_sync_local_activity 5 ");
1201
1202         if (activity_count > 0) {
1203                 event_data.type = EMAIL_EVENT_LOCAL_ACTIVITY;
1204                 event_data.account_id  = account_id;
1205                 if (!emcore_insert_event(&event_data, &handle, &err))  {
1206                         EM_DEBUG_LOG(" emcore_insert_event failed - %d", err);
1207                         goto FINISH_OFF;
1208                 }
1209                 
1210                 ret = true;
1211         }
1212
1213         FINISH_OFF: 
1214                 
1215         if (activity_id_list)
1216                 emstorage_free_activity_id_list(activity_id_list, &err); 
1217         
1218         if (err_code != NULL)
1219                 *err_code = err;
1220         
1221         return ret;
1222
1223 }
1224
1225 INTERNAL_FUNC int emcore_save_local_activity_sync(int account_id, int *err_code)
1226 {
1227         EM_DEBUG_FUNC_BEGIN();
1228
1229         EM_DEBUG_LOG(">> account_id [%d], err_code [%p] ", account_id, err_code);
1230         
1231         emstorage_activity_tbl_t *local_activity = NULL;
1232         int *activity_id_list = NULL;
1233         int activity_count = 0;
1234         int err         = 0;
1235         int ret = false;
1236         int handle = 0;
1237         email_event_t event_data;
1238
1239         memset(&event_data, 0x00, sizeof(email_event_t));
1240
1241         EM_IF_NULL_RETURN_VALUE(err_code, false);
1242
1243         if (account_id <= 0) {
1244                 EM_DEBUG_EXCEPTION(" Invalid Account ID [%d] ", account_id);
1245                 return false;
1246         }
1247                 EM_DEBUG_LOG(">>> emdaemon_sync_local_activity 3 ");
1248
1249         if (!emstorage_get_activity_id_list(account_id, &activity_id_list, &activity_count, ACTIVITY_SAVEMAIL, ACTIVITY_DELETEMAIL_SEND, true,  &err)) {
1250                 EM_DEBUG_EXCEPTION(" emstorage_get_activity_id_list [ %d] ", err);
1251                 goto FINISH_OFF;
1252         }
1253
1254         
1255         if (activity_count > 0) {
1256                 event_data.type = EMAIL_EVENT_LOCAL_ACTIVITY;
1257                 event_data.account_id  = account_id;
1258                 if (!emcore_insert_event_for_sending_mails(&event_data, &handle, &err))  {
1259                         EM_DEBUG_LOG(" emcore_insert_event failed - %d", err);
1260                         goto FINISH_OFF;
1261                 }       
1262                 
1263                 ret = true;
1264         }
1265
1266
1267         FINISH_OFF: 
1268
1269         if (local_activity)
1270                 emstorage_free_local_activity(&local_activity, activity_count, NULL); 
1271
1272         if (activity_id_list)
1273                 emstorage_free_activity_id_list(activity_id_list, &err);        
1274
1275         if (err_code != NULL)
1276                 *err_code = err;
1277         
1278         return ret;
1279
1280 }
1281
1282 #endif
1283