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