Tizen 2.0 Release
[platform/core/messaging/email-service.git] / email-core / email-core-mail.c
1 /*
2 *  email-service
3 *
4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5 *
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
7
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 */
21
22
23
24 /******************************************************************************
25  * File :  email-core-mail.c
26  * Desc :  Mail Operation
27  *
28  * Auth : 
29  *
30  * History : 
31  *    2006.08.16  :  created
32  *****************************************************************************/
33 #undef close
34 #include <time.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <ctype.h>
39 #include <fcntl.h>
40
41 #include <sys/time.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <unistd.h>
45
46 #include <vconf.h> 
47 #include <contacts.h>
48
49 #include "email-internal-types.h"
50 #include "c-client.h"
51 #include "lnx_inc.h"
52 #include "email-utilities.h"
53 #include "email-core-global.h"
54 #include "email-core-utils.h"
55 #include "email-core-mail.h"
56 #include "email-core-mime.h"
57 #include "email-core-mailbox.h"
58 #include "email-storage.h"
59 #include "email-network.h"
60 #include "email-core-mailbox-sync.h"
61 #include "email-core-event.h"
62 #include "email-core-account.h" 
63 #include "email-core-signal.h"
64 #include "email-core-smtp.h"
65
66 #include "email-convert.h"
67 #include "email-debug-log.h"
68
69 #ifdef __FEATURE_DRM__
70 #include <drm_client.h>
71 #endif /* __FEATURE_DRM__ */
72
73 #define ST_SILENT   (long) 0x2  /* don't return results */
74 #define ST_SET      (long) 0x4  /* set vs. clear */
75
76 static char g_new_server_uid[129];
77
78 bool only_body_download = false;
79
80 int multi_part_body_size = 0;
81 int is_multi_part_body_download_all = 0;
82 int _pop3_receiving_mail_id = 0;
83 int _pop3_received_body_size = 0;
84 int _pop3_last_notified_body_size = 0;
85 int _pop3_total_body_size = 0;
86
87 int _imap4_received_body_size = 0;
88 int _imap4_last_notified_body_size = 0;
89 int _imap4_total_body_size = 0;
90 int _imap4_download_noti_interval_value = 0;
91
92 BODY **g_inline_list = NULL ;
93 int g_inline_count = 0;
94
95
96 static int emcore_delete_mails_from_pop3_server(email_account_t *input_account, int input_mail_ids[], int input_mail_id_count);
97 static int emcore_mail_cmd_read_mail_pop3(void *stream, int msgno, int limited_size, int *downloded_size, int *total_body_size, int *err_code);
98
99 extern long pop3_send (MAILSTREAM *stream, char *command, char *args);
100
101
102 #ifdef FEATURE_CORE_DEBUG
103 static char *_getType(int type)
104 {
105         switch (type) {
106                 case 0:  return "TYPETEXT";
107                 case 1:  return "TYPEMULTIPART";
108                 case 2:  return "TYPEMESSAGE";
109                 case 3:  return "TYPEAPPLICATION";
110                 case 4:  return "TYPEAUDIO";
111                 case 5:  return "TYPEVIDEO";
112                 case 6:  return "TYPEMODEL";
113                 case 7:  return "TYPEOTHER";
114         }
115         return g_strdup_printf("%d", type);
116 }
117
118 static char *_getEncoding(int encoding)
119 {
120         switch (encoding) {
121                 case 0:  return "ENC7BIT";
122                 case 1:  return "ENC8BIT";
123                 case 2:  return "ENCBINARY";
124                 case 3:  return "ENCBASE64";
125                 case 4:  return "ENCQUOTEDPRINTABLE";
126                 case 5:  return "ENCOTHER";
127         }
128         return g_strdup_printf("%d", encoding);
129 }
130
131 static void _print_parameter(PARAMETER *param)
132 {
133         while (param != NULL) {
134                 EM_DEBUG_EXCEPTION("param->attribute[%s]", param->attribute);
135                 EM_DEBUG_EXCEPTION("param->value[%s]", param->value);
136                 
137                 param = param->next;
138         }
139 }
140
141 static void _print_stringlist(STRINGLIST *stringlist)
142 {
143         while (stringlist != NULL) {
144                 EM_DEBUG_LOG("stringlist->text.data[%s]", stringlist->text.data);
145                 EM_DEBUG_LOG("stringlist->text.size[%ld]", stringlist->text.size);
146                 
147                 stringlist = stringlist->next;
148         }
149 }
150
151 static void _print_body(BODY *body, int recursive)
152 {
153         EM_DEBUG_LOG(" ========================================================== ");
154         
155         if (body != NULL) {
156                 EM_DEBUG_LOG("body->type[%s]", _getType(body->type));
157                 EM_DEBUG_LOG("body->encoding[%s]", _getEncoding(body->encoding));
158                 EM_DEBUG_LOG("body->subtype[%s]", body->subtype);
159                 
160                 EM_DEBUG_LOG("body->parameter[%p]", body->parameter);
161                 
162                 _print_parameter(body->parameter);
163                 
164                 EM_DEBUG_LOG("body->id[%s]", body->id);
165                 EM_DEBUG_LOG("body->description[%s]", body->description);
166                 
167                 EM_DEBUG_LOG("body->disposition.type[%s]", body->disposition.type);
168                 EM_DEBUG_LOG("body->disposition.parameter[%p]", body->disposition.parameter);
169                 
170                 _print_parameter(body->disposition.parameter);
171                 
172                 EM_DEBUG_LOG("body->language[%p]", body->language);
173                 
174                 _print_stringlist(body->language);
175                 
176                 EM_DEBUG_LOG("body->location[%s]", body->location);
177         
178                 EM_DEBUG_LOG("body->mime.offset[%ld]", body->mime.offset);
179                 EM_DEBUG_LOG("body->mime.text.data[%s]", body->mime.text.data);
180                 EM_DEBUG_LOG("body->mime.text.size[%ld]", body->mime.text.size);
181                 
182                 EM_DEBUG_LOG("body->contents.offset[%ld]", body->contents.offset);
183                 EM_DEBUG_LOG("body->contents.text.data[%p]", body->contents.text.data);
184                 EM_DEBUG_LOG("body->contents.text.size[%ld]", body->contents.text.size);
185         
186                 EM_DEBUG_LOG("body->nested.part[%p]", body->nested.part);
187                 
188                 EM_DEBUG_LOG("body->size.lines[%ld]", body->size.lines);
189                 EM_DEBUG_LOG("body->size.bytes[%ld]", body->size.bytes);
190                 
191                 EM_DEBUG_LOG("body->md5[%s]", body->md5);
192                 EM_DEBUG_LOG("body->sparep[%p]", body->sparep);
193                 
194                 if (recursive)  {
195                         PART *part = body->nested.part;
196                         
197                         while (part != NULL)  {
198                                 _print_body(&(part->body), recursive);
199                                 part = part->next;
200                         }
201                 }
202         }
203         
204         EM_DEBUG_LOG(" ========================================================== ");
205 }
206 #endif /*  FEATURE_CORE_DEBUG */
207
208 static int convert_contact_err_to_email_err(int contact_err)
209 {
210         int err = EMAIL_ERROR_NONE;
211
212         switch (contact_err) {
213         case CONTACTS_ERROR_NONE :
214                 err = EMAIL_ERROR_NONE;
215                 break;
216         case CONTACTS_ERROR_OUT_OF_MEMORY :
217                 err = EMAIL_ERROR_OUT_OF_MEMORY;
218                 break;
219         case CONTACTS_ERROR_INVALID_PARAMETER :
220                 err = EMAIL_ERROR_INVALID_PARAM;
221                 break;
222         case CONTACTS_ERROR_NO_DATA :
223                 err = EMAIL_ERROR_DATA_NOT_FOUND;
224                 break;
225         case CONTACTS_ERROR_DB :
226                 err = EMAIL_ERROR_DB_FAILURE;
227                 break;
228         case CONTACTS_ERROR_IPC :
229                 err = EMAIL_ERROR_IPC_CONNECTION_FAILURE;
230                 break;
231         case CONTACTS_ERROR_SYSTEM:
232                 err = EMAIL_ERROR_SYSTEM_FAILURE;
233                 break;
234         case CONTACTS_ERROR_INTERNAL:
235         default:
236                 err = EMAIL_ERROR_UNKNOWN;
237                 break;
238         }
239         return err;
240 }
241
242 static int pop3_mail_delete(MAILSTREAM *stream, int msgno, int *err_code)
243 {
244         EM_DEBUG_FUNC_BEGIN("stream[%p], msgno[%d], err_code[%p]", stream, msgno, err_code);
245         
246         int ret = false;
247         int err = EMAIL_ERROR_NONE;
248         
249         POP3LOCAL *pop3local = NULL;
250         char cmd[64];
251         char *p = NULL;
252         
253         if (!stream)  {
254                 EM_DEBUG_EXCEPTION("stream[%p]", stream);
255                 err = EMAIL_ERROR_INVALID_PARAM;
256                 goto FINISH_OFF;
257         }
258         
259         if (!(pop3local = stream->local) || !pop3local->netstream) {
260                 EM_DEBUG_EXCEPTION("invalid POP3 stream detected...");
261                 err = EMAIL_ERROR_UNKNOWN;
262                 goto FINISH_OFF;
263         }
264         
265         memset(cmd, 0x00, sizeof(cmd));
266         
267         SNPRINTF(cmd, sizeof(cmd), "DELE %d\015\012", msgno);
268         
269 #ifdef FEATURE_CORE_DEBUG
270         EM_DEBUG_LOG("[POP3] >>> %s", cmd);
271 #endif
272         
273         /*  send command  :  delete specified mail */
274         if (!net_sout(pop3local->netstream, cmd, (int)EM_SAFE_STRLEN(cmd))) {
275                 EM_DEBUG_EXCEPTION("net_sout failed...");
276                 err = EMAIL_ERROR_CONNECTION_BROKEN;            /* EMAIL_ERROR_UNKNOWN; */
277                 goto FINISH_OFF;
278         }
279         
280         /*  receive response */
281         if (!(p = net_getline(pop3local->netstream))) {
282                 EM_DEBUG_EXCEPTION("net_getline failed...");
283                 err = EMAIL_ERROR_INVALID_RESPONSE;
284                 goto FINISH_OFF;
285         }
286         
287 #ifdef FEATURE_CORE_DEBUG
288         EM_DEBUG_LOG("[POP3] <<< %s", p);
289 #endif
290
291         if (*p == '-') {                /*  '-ERR' */
292                 err = EMAIL_ERROR_POP3_DELE_FAILURE;            /* EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER; */
293                 goto FINISH_OFF;
294         }
295         
296         if (*p != '+') {                /*  '+OK' ... */
297                 err = EMAIL_ERROR_INVALID_RESPONSE;
298                 goto FINISH_OFF;
299         }
300         
301         ret = true;
302
303 FINISH_OFF: 
304         if (p)
305                 free(p);
306         
307         if (err_code)
308                 *err_code = err;
309
310         return ret;
311 }
312
313 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
314
315 static void emcore_mail_copyuid_ex(MAILSTREAM *stream, char *mailbox, unsigned long uidvalidity, SEARCHSET *sourceset, SEARCHSET *destset)
316 {
317         
318         EM_DEBUG_FUNC_BEGIN();
319
320         int index = -1;
321         int i = 0;
322         int err = EMAIL_ERROR_NONE;
323         
324         unsigned long  first_uid = 0;
325         unsigned long last_uid = 0;
326         
327         unsigned long *old_server_uid = NULL;
328         unsigned long *new_server_uid = NULL;
329         int count = 0;
330         SEARCHSET *temp = sourceset;
331
332         char old_server_uid_char[129];
333         char new_server_uid_char[129];
334
335         if (NULL == sourceset || NULL == destset) {
336                 /* 
337                 sourceset will be NULL when the sequence of UIDs sent to server for mail move operation has all invalid old server uids  
338                 if sourceset is NULL then corresponding dest set will be NULL
339                 */
340                 
341                 EM_DEBUG_LOG("emcore_mail_copyuid_ex failed :  Invalid Parameters--> sourceset[%p] , destset[%p]", sourceset, destset);
342                 return;
343         }
344
345         /* To get count of mails actually moved */
346
347         while (temp) {
348                 if (temp->first > 0) {
349                         first_uid = temp->first;
350
351                         count++;
352
353                         if (temp->last > 0) {
354                                 last_uid = temp->last;
355
356                                 while (first_uid < last_uid) {
357                                         first_uid++;
358                                         count++;
359                                 }
360                         }
361                 }
362                 
363                 temp = temp->next;
364         }
365
366
367         
368
369         EM_DEBUG_LOG("Count of mails copied [%d]", count);
370         old_server_uid = em_malloc(count * sizeof(unsigned long));
371         new_server_uid = em_malloc(count * sizeof(unsigned long));
372
373         /* While loop below will collect all old server uid from sourceset into old_server_uid array */
374         
375         while (sourceset) {
376                 if (sourceset->first > 0) {
377                         first_uid = sourceset->first;
378
379                         index++;
380                         old_server_uid[index] = first_uid;
381
382                         if (sourceset->last > 0) {
383                                 last_uid = sourceset->last;
384
385                                 while (first_uid < last_uid) {
386                                         first_uid++;
387                                         index++;
388
389                                         old_server_uid[index] = first_uid;
390                                 }
391                         }
392                 }
393                 
394                 sourceset = sourceset->next;
395         }
396
397         /* While loop below will collect all new server uid from destset into new_server_uid array */
398
399         index = -1;
400         first_uid = last_uid = 0;
401         
402         while (destset) {
403                 if (destset->first > 0) {
404                         first_uid = destset->first;
405
406                         index++;
407                         new_server_uid[index] = first_uid;
408
409                         if (destset->last > 0) {
410                                 last_uid = destset->last;
411
412                                 while (first_uid < last_uid) {
413                                         first_uid++;
414                                         index++;
415
416                                         new_server_uid[index] = first_uid;
417                                 }
418                         }
419                 }
420                 
421                 destset = destset->next;
422         }
423
424         /* For loop below updates mail_tbl and mail_read_mail_uid_tbl with new server uids*/
425         
426         for (i = 0; i <= index; ++i) {
427
428                 memset(old_server_uid_char, 0x00, sizeof(old_server_uid_char));
429                 sprintf(old_server_uid_char, "%ld", old_server_uid[i]);
430
431                 EM_DEBUG_LOG("Old Server Uid Char[%s]", old_server_uid_char);
432                 
433                 memset(new_server_uid_char, 0x00, sizeof(new_server_uid_char));
434                 sprintf(new_server_uid_char, "%ld", new_server_uid[i]);
435
436                 EM_DEBUG_LOG("New Server Uid Char[%s]", new_server_uid_char);
437         
438                 if (!emstorage_update_server_uid(old_server_uid_char, new_server_uid_char, &err)) {
439                         EM_DEBUG_EXCEPTION("emstorage_update_server_uid failed...[%d]", err);
440                 }
441                 
442                 if (!emstorage_update_read_mail_uid_by_server_uid(old_server_uid_char, new_server_uid_char, mailbox, &err)) {
443                         EM_DEBUG_EXCEPTION("emstorage_update_read_mail_uid_by_server_uid failed... [%d]", err);
444                 }
445                 
446 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
447
448                 if (false == emstorage_update_pbd_activity(old_server_uid_char, new_server_uid_char, mailbox, &err)) {
449                         EM_DEBUG_EXCEPTION("emstorage_update_pbd_activity failed... [%d]", err);
450                 }
451                 
452 #endif
453         }
454
455         EM_SAFE_FREE(old_server_uid);
456         EM_SAFE_FREE(new_server_uid);
457         
458 }
459
460 INTERNAL_FUNC int emcore_move_mail_on_server_ex(int account_id, int src_mailbox_id,  int mail_ids[], int num, int dest_mailbox_id, int *error_code)
461 {
462         EM_DEBUG_FUNC_BEGIN();
463         MAILSTREAM *stream = NULL;
464         int err_code = EMAIL_ERROR_NONE;
465         email_account_t *ref_account = NULL;
466         int ret = false;
467         int i = 0;
468         email_id_set_t *id_set = NULL;
469         int id_set_count = 0;
470
471         email_uid_range_set *uid_range_set = NULL;
472         int len_of_each_range = 0;
473
474         email_uid_range_set *uid_range_node = NULL;
475
476         char **string_list = NULL;      
477         int string_count = 0;
478         emstorage_mailbox_tbl_t* dest_mailbox = NULL;
479         
480         if (num <= 0  || account_id <= 0 || src_mailbox_id <= 0 || dest_mailbox_id <= 0 || NULL == mail_ids) {
481                 if (error_code != NULL) {
482                         *error_code = EMAIL_ERROR_INVALID_PARAM;
483                 }
484                 EM_DEBUG_LOG("Invalid Parameters- num[%d], account_id[%d], src_mailbox_id[%d], dest_mailbox_id[%d], mail_ids[%p]", num, account_id, src_mailbox_id, dest_mailbox_id, mail_ids);
485                 return false;
486         }
487
488         ref_account = emcore_get_account_reference(account_id);
489         
490         if (NULL == ref_account) {
491                 EM_DEBUG_EXCEPTION(" emcore_get_account_reference failed[%d]", account_id);
492                 
493                 *error_code = EMAIL_ERROR_INVALID_ACCOUNT;
494                 goto FINISH_OFF;
495         }
496
497         if (ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
498                 *error_code = EMAIL_ERROR_INVALID_PARAM;
499                 goto FINISH_OFF;
500         }
501
502
503         if (!emcore_connect_to_remote_mailbox(account_id, src_mailbox_id, (void **)&stream, &err_code)) {
504                 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed[%d]", err_code);
505                 
506                 goto FINISH_OFF;
507         }
508
509         if (NULL != stream) {
510                 mail_parameters(stream, SET_COPYUID, emcore_mail_copyuid_ex);
511                 EM_DEBUG_LOG("calling mail_copy_full FODLER MAIL COPY ");
512                 /*  [h.gahlaut] Break the set of mail_ids into comma separated strings of given length  */
513                 /*  Length is decided on the basis of remaining keywords in the Query to be formed later on in emstorage_get_id_set_from_mail_ids  */
514                 /*  Here about 90 bytes are required for fixed keywords in the query-> SELECT local_uid, s_uid from mail_read_mail_uid_tbl where local_uid in (....) ORDER by s_uid  */
515                 /*  So length of comma separated strings which will be filled in (.....) in above query can be maximum QUERY_SIZE - 90  */
516                 
517                 if (false == emcore_form_comma_separated_strings(mail_ids, num, QUERY_SIZE - 90, &string_list, &string_count, &err_code))   {
518                         EM_DEBUG_EXCEPTION("emcore_form_comma_separated_strings failed [%d]", err_code);
519                         goto FINISH_OFF;
520                 }
521                 
522                 if ( (err_code = emstorage_get_mailbox_by_id(dest_mailbox_id, &dest_mailbox)) != EMAIL_ERROR_NONE || !dest_mailbox) {
523                         EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err_code);
524                         goto FINISH_OFF;
525                 }
526
527                 /*  Now execute one by one each comma separated string of mail_ids */
528
529                 for (i = 0; i < string_count; ++i) {
530                         /*  Get the set of mail_ds and corresponding server_mail_ids sorted by server mail ids in ascending order */
531                 
532                         if (false == emstorage_get_id_set_from_mail_ids(string_list[i], &id_set, &id_set_count, &err_code)) {
533                                 EM_DEBUG_EXCEPTION("emstorage_get_id_set_from_mail_ids failed [%d]", err_code);
534                                 goto FINISH_OFF;
535                         }
536                         
537                         /*  Convert the sorted sequence of server mail ids to range sequences of given length. A range sequence will be like A : B, C, D: E, H */
538                         
539                         len_of_each_range = MAX_IMAP_COMMAND_LENGTH - 40; /*  1000 is the maximum length allowed RFC 2683. 40 is left for keywords and tag. */
540                         
541                         if (false == emcore_convert_to_uid_range_set(id_set, id_set_count, &uid_range_set, len_of_each_range, &err_code)) {
542                                 EM_DEBUG_EXCEPTION("emcore_convert_to_uid_range_set failed [%d]", err_code);
543                                 goto FINISH_OFF;
544                         }
545                 
546                         uid_range_node = uid_range_set;
547                 
548                         while (uid_range_node != NULL) {
549                                 /* Remove comma from end of uid_range */
550                                 uid_range_node->uid_range[EM_SAFE_STRLEN(uid_range_node->uid_range) - 1] = '\0';
551                                 EM_DEBUG_LOG("uid_range_node->uid_range - %s", uid_range_node->uid_range);
552                                 if (!mail_copy_full(stream, uid_range_node->uid_range, dest_mailbox->mailbox_name, CP_UID | CP_MOVE)) {
553                                         EM_DEBUG_EXCEPTION("emcore_move_mail_on_server_ex :   Mail cannot be moved failed");
554                                         EM_DEBUG_EXCEPTION("Mail MOVE failed ");
555                                         goto FINISH_OFF;
556                                 }
557                                 else if (!mail_expunge_full(stream, uid_range_node->uid_range, EX_UID)) {
558                                         EM_DEBUG_EXCEPTION("emcore_move_mail_on_server_ex :   Mail cannot be expunged after move. failed!");
559                                                 EM_DEBUG_EXCEPTION("Mail Expunge after move failed ");
560                                                   goto FINISH_OFF;
561                                 }
562                                 else {
563                                         EM_DEBUG_LOG("Mail MOVE SUCCESS ");
564                                 }
565                                 
566                                 uid_range_node = uid_range_node->next;
567                         }       
568
569                         emcore_free_uid_range_set(&uid_range_set);
570
571                         EM_SAFE_FREE(id_set);
572                         
573                         id_set_count = 0;
574                 }
575         
576         }
577         else {
578                 EM_DEBUG_EXCEPTION(">>>> STREAM DATA IS NULL >>> ");
579                 goto FINISH_OFF;
580         }
581         
582
583         ret = true;
584
585 FINISH_OFF: 
586         emcore_close_mailbox(0, stream);
587         stream = NULL;
588
589         if (ref_account) {
590                 emcore_free_account(ref_account);
591                 EM_SAFE_FREE(ref_account);
592         }
593
594 #ifdef __FEATURE_LOCAL_ACTIVITY__
595         if (ret || ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) /* Delete local activity for POP3 mails and successful move operation in IMAP */ {
596                 emstorage_activity_tbl_t new_activity;
597                 for (i = 0; i<num ; i++) {              
598                         memset(&new_activity, 0x00, sizeof(emstorage_activity_tbl_t));
599                         new_activity.activity_type = ACTIVITY_MOVEMAIL;
600                         new_activity.account_id    = account_id;
601                         new_activity.mail_id       = mail_ids[i];
602                         new_activity.src_mbox      = src_mailbox;
603                         new_activity.dest_mbox     = dest_mailbox;
604                 
605                         if (!emcore_delete_activity(&new_activity, &err_code)) {
606                                 EM_DEBUG_EXCEPTION(">>>>>>Local Activity ACTIVITY_MOVEMAIL [%d] ", err_code);
607                         }
608                 }
609
610         }
611 #endif
612         if (dest_mailbox) {
613                 emstorage_free_mailbox(&dest_mailbox, 1, &err_code);
614         }
615
616         if (error_code != NULL) {
617                 *error_code = err_code;
618         }
619
620         return ret;
621 }
622
623 int emcore_delete_mails_from_imap4_server(int mail_ids[], int num, int from_server, int *err_code)
624 {
625         EM_DEBUG_FUNC_BEGIN();
626         
627         int ret = false;
628         int err = EMAIL_ERROR_NONE;
629         IMAPLOCAL *imaplocal = NULL;
630         char *p = NULL;
631         MAILSTREAM *stream = NULL;
632         char tag[MAX_TAG_SIZE];
633         char cmd[MAX_IMAP_COMMAND_LENGTH];
634         email_id_set_t *id_set = NULL;
635         int id_set_count = 0;
636         int i = 0;
637         email_uid_range_set *uid_range_set = NULL;
638         int len_of_each_range = 0;
639         email_uid_range_set *uid_range_node = NULL;
640         char **string_list = NULL;      
641         int string_count = 0;
642         int delete_success = false;
643         emstorage_mail_tbl_t *mail_tbl_data = NULL;
644                 
645         if (num <= 0 || !mail_ids) {
646                 EM_DEBUG_EXCEPTION(" Invalid parameter ");
647                 err = EMAIL_ERROR_INVALID_PARAM;
648                 goto FINISH_OFF;
649         }
650
651         for(i = 0; i < num; i++) {
652                 if (!emstorage_get_downloaded_mail(mail_ids[i], &mail_tbl_data, false, &err)) {
653                         EM_DEBUG_EXCEPTION("emstorage_get_downloaded_mail failed [%d]", err);
654
655                         if (err == EMAIL_ERROR_MAIL_NOT_FOUND) {        /* not server mail */
656                                 continue;
657                         }
658                         else
659                                 break;
660                 }
661         }
662
663         if (!mail_tbl_data || mail_tbl_data->account_id <= 0 || mail_tbl_data->mailbox_id <= 0) {
664                 EM_DEBUG_EXCEPTION("mail_tbl_data [%p]", mail_tbl_data);
665                 goto FINISH_OFF;
666         }
667
668         if (!emcore_connect_to_remote_mailbox(mail_tbl_data->account_id, mail_tbl_data->mailbox_id , (void **)&stream, &err)) {
669                 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
670                 goto FINISH_OFF;
671         }
672
673         /*  [h.gahlaut] Break the set of mail_ids into comma separated strings of given length  */
674         /*  Length is decided on the basis of remaining keywords in the Query to be formed later on in emstorage_get_id_set_from_mail_ids  */
675         /*  Here about 90 bytes are required for fixed keywords in the query-> SELECT local_uid, s_uid from mail_read_mail_uid_tbl where local_uid in (....) ORDER by s_uid  */
676         /*  So length of comma separated strings which will be filled in (.....) in above query can be maximum QUERY_SIZE - 90  */
677
678         if (false == emcore_form_comma_separated_strings(mail_ids, num, QUERY_SIZE - 90, &string_list, &string_count, &err)) {
679                 EM_DEBUG_EXCEPTION("emcore_form_comma_separated_strings failed [%d]", err);
680                 goto FINISH_OFF;
681         }
682
683         /*  Now execute one by one each comma separated string of mail_ids  */
684         
685         for (i = 0; i < string_count; ++i) {
686                 /*  Get the set of mail_ds and corresponding server_mail_ids sorted by server mail ids in ascending order  */
687
688                 if (false == emstorage_get_id_set_from_mail_ids(string_list[i], &id_set, &id_set_count, &err)) {
689                         EM_DEBUG_EXCEPTION("emstorage_get_id_set_from_mail_ids failed [%d]", err);
690                         goto FINISH_OFF;
691                 }
692                 
693                 /*  Convert the sorted sequence of server mail ids to range sequences of given length. A range sequence will be like A : B, C, D: E, H  */
694                 
695                 len_of_each_range = MAX_IMAP_COMMAND_LENGTH - 40;               /*   1000 is the maximum length allowed RFC 2683. 40 is left for keywords and tag.  */
696                 
697                 if (false == emcore_convert_to_uid_range_set(id_set, id_set_count, &uid_range_set, len_of_each_range, &err)) {
698                         EM_DEBUG_EXCEPTION("emcore_convert_to_uid_range_set failed [%d]", err);
699                         goto FINISH_OFF;
700                 }
701
702                 uid_range_node = uid_range_set;
703
704                 while (uid_range_node != NULL) {
705                         /*  Remove comma from end of uid_range  */
706
707                         uid_range_node->uid_range[EM_SAFE_STRLEN(uid_range_node->uid_range) - 1] = '\0';
708
709                         if (!(imaplocal = stream->local) || !imaplocal->netstream)  {
710                                 EM_DEBUG_EXCEPTION("invalid IMAP4 stream detected...");
711                 
712                                 err = EMAIL_ERROR_CONNECTION_BROKEN;
713                                 goto FINISH_OFF;
714                         }
715
716                         memset(tag, 0x00, sizeof(tag));
717                         memset(cmd, 0x00, sizeof(cmd));
718                         
719                         SNPRINTF(tag, sizeof(tag), "%08lx", 0xffffffff & (stream->gensym++));
720                         SNPRINTF(cmd, sizeof(cmd), "%s UID STORE %s +FLAGS (\\Deleted)\015\012", tag, uid_range_node->uid_range);
721                         
722                         EM_DEBUG_LOG("[IMAP4] >>> %s", cmd);
723
724                         
725                         /* send command  :  set deleted flag */
726                         if (!net_sout(imaplocal->netstream, cmd, (int)EM_SAFE_STRLEN(cmd)))  {
727                                 EM_DEBUG_EXCEPTION("net_sout failed...");
728                                 
729                                 err = EMAIL_ERROR_CONNECTION_BROKEN;            /* EMAIL_ERROR_UNKNOWN */
730                                 goto FINISH_OFF;
731                         }
732
733                         
734                         while (imaplocal->netstream)  {
735                                 /* receive response */
736                                 if (!(p = net_getline(imaplocal->netstream))) {
737                                         EM_DEBUG_EXCEPTION("net_getline failed...");
738                                         
739                                         err = EMAIL_ERROR_INVALID_RESPONSE;             /* EMAIL_ERROR_UNKNOWN; */
740                                         goto FINISH_OFF;
741                                 }
742                                 
743
744                                 EM_DEBUG_LOG("[IMAP4] <<< %s", p);
745
746                                 /* To confirm - Commented out as FETCH response does not contain the tag - may be a problem for common stream in email-service*/
747                                 /* Success case - delete all local activity and entry from mail_read_mail_uid_tbl 
748                                 if (strstr(p, "FETCH") != NULL)  {
749                                         EM_DEBUG_LOG(" FETCH Response recieved ");
750                                         delete_success = true;
751                                         EM_SAFE_FREE(p);
752                                         break;
753                                 }
754                                 */
755                 
756                         
757                                 if (!strncmp(p, tag, EM_SAFE_STRLEN(tag)))  {
758                                         if (!strncmp(p + EM_SAFE_STRLEN(tag) + 1, "OK", 2))  {
759                                                 /*Error scenario delete all local activity and entry from mail_read_mail_uid_tbl */
760                                                 EM_DEBUG_LOG(" OK Response recieved ");
761                                                 delete_success = true;
762                                                 EM_SAFE_FREE(p);
763                                                 break;
764                                         }                                       
765                                         else  {
766                                                 /*  'NO' or 'BAD' */
767                                                 delete_success = false;
768                                                 err = EMAIL_ERROR_IMAP4_STORE_FAILURE;          /* EMAIL_ERROR_INVALID_RESPONSE; */
769                                                 goto FINISH_OFF;
770                                         }
771                                 }
772                         
773                                 EM_SAFE_FREE(p);
774                                 }
775                                 
776                                 memset(tag, 0x00, sizeof(tag));
777                                 memset(cmd, 0x00, sizeof(cmd));
778
779                                 EM_DEBUG_LOG("Calling Expunge");
780                                 
781                                 SNPRINTF(tag, sizeof(tag), "%08lx", 0xffffffff & (stream->gensym++));
782                                 SNPRINTF(cmd, sizeof(cmd), "%s EXPUNGE\015\012", tag);
783                                 
784                                 EM_DEBUG_LOG("[IMAP4] >>> %s", cmd);
785                                 
786                                 /* send command  :   EXPUNGE */
787                                 if (!net_sout(imaplocal->netstream, cmd, (int)EM_SAFE_STRLEN(cmd)))  {
788                                         EM_DEBUG_EXCEPTION("net_sout failed...");
789                                         
790                                         err = EMAIL_ERROR_CONNECTION_BROKEN;
791                                         goto FINISH_OFF;
792                                 }
793
794                                 while (imaplocal->netstream)  {
795                                         /* receive response */
796                                         if (!(p = net_getline(imaplocal->netstream))) {
797                                                 EM_DEBUG_EXCEPTION("net_getline failed...");
798                                                 
799                                                 err = EMAIL_ERROR_INVALID_RESPONSE;             /* EMAIL_ERROR_UNKNOWN; */
800                                                 goto FINISH_OFF;
801                                         }
802                                         
803                                         EM_DEBUG_LOG("[IMAP4] <<< %s", p);
804                                         
805                                         if (!strncmp(p, tag, EM_SAFE_STRLEN(tag)))  {
806                                                 if (!strncmp(p + EM_SAFE_STRLEN(tag) + 1, "OK", 2))  {
807 #ifdef __FEATURE_LOCAL_ACTIVITY__
808                                                         int index = 0;
809                                                         emstorage_mail_tbl_t **mail = NULL;
810                                                         
811                                                         mail = (emstorage_mail_tbl_t **) em_malloc(num * sizeof(emstorage_mail_tbl_t *));
812                                                         if (!mail) {
813                                                                 EM_DEBUG_EXCEPTION("em_malloc failed");
814                                                                 err = EMAIL_ERROR_UNKNOWN;
815                                                                 goto FINISH_OFF;
816                                                         }
817
818                                                         if (delete_success) {
819                                                                 for (index = 0 ; index < num; index++) {
820                                                                         if (!emstorage_get_downloaded_mail(mail_ids[index], &mail[index], false, &err))  {
821                                                                                 EM_DEBUG_LOG("emstorage_get_uid_by_mail_id failed [%d]", err);
822                                                                                 
823                                                                                 if (err == EMAIL_ERROR_MAIL_NOT_FOUND)  {               
824                                                                                         EM_DEBUG_LOG("EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER  : ");
825                                                                                         continue;
826                                                                                 }
827                                                                         }               
828                                                                 
829                                                                         if (mail[index]) {                              
830                                                                                 /* Clear entry from mail_read_mail_uid_tbl */
831                                                                                 if (mail[index]->server_mail_id != NULL) {
832                                                                                         if (!emstorage_remove_downloaded_mail(mail[index]->account_id, mail[index]->mailbox_name, mail[index]->server_mail_id, true, &err))  {
833                                                                                                 EM_DEBUG_LOG("emstorage_remove_downloaded_mail falied [%d]", err);
834                                                                                         }
835                                                                                 }
836                                                                                 
837                                                                                 /* Delete local activity */
838                                                                                 emstorage_activity_tbl_t new_activity;
839                                                                                 memset(&new_activity, 0x00, sizeof(emstorage_activity_tbl_t));
840                                                                                 if (from_server == EMAIL_DELETE_FOR_SEND_THREAD) {
841                                                                                         new_activity.activity_type = ACTIVITY_DELETEMAIL_SEND;
842                                                                                         EM_DEBUG_LOG("from_server == EMAIL_DELETE_FOR_SEND_THREAD ");
843                                                                                 }
844                                                                                 else {
845                                                                                         new_activity.activity_type      = ACTIVITY_DELETEMAIL;
846                                                                                 }
847
848                                                                                 new_activity.mail_id            = mail[index]->mail_id;
849                                                                                 new_activity.server_mailid      = NULL;
850                                                                                 new_activity.src_mbox           = NULL;
851                                                                                 new_activity.dest_mbox          = NULL;
852                                                                                                                 
853                                                                                 if (!emcore_delete_activity(&new_activity, &err)) {
854                                                                                         EM_DEBUG_EXCEPTION(" emcore_delete_activity  failed  - %d ", err);
855                                                                                 }
856                                                                         }
857                                                                         else {
858                                                                                 /* Fix for crash seen while deleting Outbox mails which are moved to Trash. Outbox mails do not have server mail id and are not updated in mail_read_mail_uid_tbl.
859                                                                                   * So there is no need of deleting entry in mail_read_mail_uid_tbl. However local activity has to be deleted. 
860                                                                                 */
861                                                                                 /* Delete local activity */
862                                                                                 emstorage_activity_tbl_t new_activity;
863                                                                                 memset(&new_activity, 0x00, sizeof(emstorage_activity_tbl_t));
864                                                                                 new_activity.activity_type      = ACTIVITY_DELETEMAIL;
865                                                                                 new_activity.mail_id            = mail_ids[index]; /* valid mail id passed for outbox mails*/
866                                                                                 new_activity.server_mailid      = NULL;
867                                                                                 new_activity.src_mbox           = NULL;
868                                                                                 new_activity.dest_mbox          = NULL;
869                                                                                                                 
870                                                                                 if (!emcore_delete_activity(&new_activity, &err)) {
871                                                                                         EM_DEBUG_EXCEPTION(" emcore_delete_activity  failed  - %d ", err);
872                                                                                 }
873                                                                         }
874                                                                 }
875
876                                                                 for (index = 0; index < num; index++) {
877                                                                         if (!emstorage_free_mail(&mail[index], 1, &err)) {
878                                                                                 EM_DEBUG_EXCEPTION(" emstorage_free_mail [%d]", err);
879                                                                         }
880                                                                 }
881
882                                                                 EM_SAFE_FREE(mail);
883                                                         }
884
885 #endif
886         EM_SAFE_FREE(p);
887                                                         break;
888                                                 }
889                                                 else  {         /*  'NO' or 'BAD' */
890                                                         err = EMAIL_ERROR_IMAP4_EXPUNGE_FAILURE;                /* EMAIL_ERROR_INVALID_RESPONSE; */
891                                                         goto FINISH_OFF;
892                                                 }
893                                         }
894                                         
895                                         EM_SAFE_FREE(p);
896                                 }
897                                 uid_range_node = uid_range_node->next;
898                 }       
899
900                 emcore_free_uid_range_set(&uid_range_set);
901
902                 EM_SAFE_FREE(id_set);
903                 
904                 id_set_count = 0;
905         }
906         
907         ret = true;
908
909 FINISH_OFF: 
910         EM_SAFE_FREE(p);
911         
912         emcore_free_comma_separated_strings(&string_list, &string_count);
913
914         if (false == ret) {
915                 emcore_free_uid_range_set(&uid_range_set);
916         }
917         
918         if (err_code) {
919                 *err_code = err;
920         }
921
922         return ret;
923
924 }
925
926 #endif
927
928 typedef enum {
929         IMAP4_CMD_EXPUNGE
930 } imap4_cmd_t;
931
932 static int imap4_send_command(MAILSTREAM *stream, imap4_cmd_t cmd_type, int *err_code)
933 {
934         EM_DEBUG_FUNC_BEGIN("stream[%p], cmd_type[%d], err_code[%p]", stream, cmd_type, err_code);
935         
936         int ret = false;
937         int err = EMAIL_ERROR_NONE;
938         
939         IMAPLOCAL *imaplocal = NULL;
940         char tag[16], cmd[64];
941         char *p = NULL;
942         
943         if (!(imaplocal = stream->local) || !imaplocal->netstream) {
944                 EM_DEBUG_EXCEPTION("invalid IMAP4 stream detected...");
945                 
946                 err = EMAIL_ERROR_INVALID_PARAM;                /* EMAIL_ERROR_UNKNOWN */
947                 goto FINISH_OFF;
948         }
949         
950         memset(tag, 0x00, sizeof(tag));
951         memset(cmd, 0x00, sizeof(cmd));
952         
953         SNPRINTF(tag, sizeof(tag), "%08lx", 0xffffffff & (stream->gensym++));
954         SNPRINTF(cmd, sizeof(cmd), "%s EXPUNGE\015\012", tag);
955
956 #ifdef FEATURE_CORE_DEBUG
957         EM_DEBUG_LOG("[IMAP4] >>> %s", cmd);
958 #endif
959         
960         /*  send command  :  delete flaged mail */
961         if (!net_sout(imaplocal->netstream, cmd, (int)EM_SAFE_STRLEN(cmd))) {
962                 EM_DEBUG_EXCEPTION("net_sout failed...");
963                 
964                 err = EMAIL_ERROR_CONNECTION_BROKEN;
965                 goto FINISH_OFF;
966         }
967         
968         while (imaplocal->netstream) {
969                 /*  receive response */
970                 if (!(p = net_getline(imaplocal->netstream))) {
971                         EM_DEBUG_EXCEPTION("net_getline failed...");
972                         
973                         err = EMAIL_ERROR_INVALID_RESPONSE;             /* EMAIL_ERROR_UNKNOWN; */
974                         goto FINISH_OFF;
975                 }
976                 
977 #ifdef FEATURE_CORE_DEBUG
978                 EM_DEBUG_LOG("[IMAP4] <<< %s", p);
979 #endif
980                 
981                 if (!strncmp(p, tag, EM_SAFE_STRLEN(tag))) {
982                         if (!strncmp(p + EM_SAFE_STRLEN(tag) + 1, "OK", 2)) {
983                                 EM_SAFE_FREE(p);
984                                 break;
985                         }
986                         else {          /*  'NO' or 'BAD' */
987                                 err = EMAIL_ERROR_IMAP4_EXPUNGE_FAILURE;                /* EMAIL_ERROR_INVALID_RESPONSE; */
988                                 goto FINISH_OFF;
989                         }
990                 }
991                 
992                 EM_SAFE_FREE(p);
993         }
994         
995         ret = true;
996
997 FINISH_OFF: 
998         EM_SAFE_FREE(p);
999         
1000         if (err_code)
1001                 *err_code = err;
1002
1003         return ret;
1004 }
1005
1006 int emcore_get_mail_contact_info(email_mail_contact_info_t *contact_info, char *full_address, int *err_code)
1007 {
1008         EM_DEBUG_FUNC_BEGIN("contact_info[%p], full_address[%s], err_code[%p]", contact_info, full_address, err_code);
1009         
1010         int ret = false;
1011         int err = EMAIL_ERROR_NONE;
1012         
1013         if (!emcore_get_mail_contact_info_with_update(contact_info, full_address, 0, &err))
1014                 EM_DEBUG_EXCEPTION("emcore_get_mail_contact_info_with_update failed [%d]", err);
1015         else
1016                 ret = true;
1017         
1018         if (err_code != NULL)
1019                 *err_code = err;
1020         
1021         return ret;
1022 }
1023
1024 int emcore_search_contact_info_by_address(const char *contact_uri, int property_id, char *address, int limit, contacts_record_h *contacts_record)
1025 {
1026         EM_DEBUG_FUNC_BEGIN();
1027         int contact_err = CONTACTS_ERROR_NONE;
1028         
1029         contacts_query_h query = NULL;
1030         contacts_filter_h filter = NULL;
1031         contacts_list_h list = NULL;
1032
1033         if ((contact_err = contacts_query_create(contact_uri, &query)) != CONTACTS_ERROR_NONE) {
1034                 EM_DEBUG_EXCEPTION("contacts_query_create failed");
1035                 goto FINISH_OFF;
1036         }
1037         
1038         if ((contact_err = contacts_filter_create(contact_uri, &filter)) != CONTACTS_ERROR_NONE) {
1039                 EM_DEBUG_EXCEPTION("contacts_filter_create failed");
1040                 goto FINISH_OFF;
1041         }
1042
1043         if ((contact_err = contacts_filter_add_str(filter, property_id, CONTACTS_MATCH_EXACTLY, address)) != CONTACTS_ERROR_NONE) {
1044                 EM_DEBUG_EXCEPTION("contacts_filter_add_str failed");
1045                 goto FINISH_OFF;
1046         }
1047         
1048         if ((contact_err = contacts_query_set_filter(query, filter)) != CONTACTS_ERROR_NONE) {
1049                 EM_DEBUG_EXCEPTION("contacts_query_set_filter failed");
1050                 goto FINISH_OFF;
1051         }
1052
1053         if ((contact_err = contacts_db_get_records_with_query(query, 0, limit, &list)) != CONTACTS_ERROR_NONE) {
1054                 EM_DEBUG_EXCEPTION("contacts_db_get_record_with_query failed");
1055                 goto FINISH_OFF;
1056         }
1057
1058         if ((contact_err = contacts_list_get_current_record_p(list, contacts_record)) != CONTACTS_ERROR_NONE) {
1059                 EM_DEBUG_EXCEPTION("contacts_list_get_current_record_p failed");
1060                 goto FINISH_OFF;
1061         }
1062
1063 FINISH_OFF:
1064
1065         if (query != NULL)
1066                 contacts_query_destroy(query);
1067
1068         if (filter != NULL)
1069                 contacts_filter_destroy(filter);
1070
1071         if (list != NULL)
1072                 contacts_list_destroy(list, false);
1073
1074         return contact_err;
1075 }
1076
1077 int emcore_set_contacts_log(int account_id, char *email_address, char *subject, time_t date_time, email_action_t action)
1078 {
1079         EM_DEBUG_FUNC_BEGIN("account_id : [%d], address : [%p], subject : [%s], action : [%d], date_time : [%d]", account_id, email_address, subject, action, (int)date_time);
1080         
1081         int err = EMAIL_ERROR_NONE;
1082         int contacts_error = CONTACTS_ERROR_NONE;
1083         int person_id = 0;
1084         int action_type = 0;
1085         
1086         contacts_record_h phone_record = NULL;
1087         contacts_record_h person_record = NULL; 
1088
1089         if ((contacts_error = contacts_connect2()) != CONTACTS_ERROR_NONE) {
1090                 EM_DEBUG_EXCEPTION("Open connect service failed [%d]", contacts_error);
1091                 goto FINISH_OFF;
1092         }
1093
1094         if ((contacts_error = contacts_record_create(_contacts_phone_log._uri, &phone_record)) != CONTACTS_ERROR_NONE) {
1095                 EM_DEBUG_EXCEPTION("contacts_query_create failed [%d]", contacts_error);
1096                 goto FINISH_OFF;
1097         }
1098
1099         /* Set email address */
1100         if ((contacts_error = contacts_record_set_str(phone_record, _contacts_phone_log.address, email_address)) != CONTACTS_ERROR_NONE) {
1101                 EM_DEBUG_EXCEPTION("contacts_record_set_str [address] failed [%d]", contacts_error);
1102                 goto FINISH_OFF;
1103         }
1104
1105         /* Search contact person info */
1106         if ((contacts_error = emcore_search_contact_info_by_address(_contacts_person_email._uri, _contacts_person_email.email, email_address, 1, &person_record)) != CONTACTS_ERROR_NONE) {
1107                 EM_DEBUG_EXCEPTION("emcore_search_contact_info_by_address failed [%d]", contacts_error);
1108                 EM_DEBUG_LOG("Not match person");
1109         } else {
1110                 /* Get person_id in contacts_person_email record  */
1111                 if (person_record  && (contacts_error = contacts_record_get_int(person_record, _contacts_person_email.person_id, &person_id)) != CONTACTS_ERROR_NONE) {
1112                         EM_DEBUG_EXCEPTION("contacts_record_get_str failed [%d]", contacts_error);
1113                         goto FINISH_OFF;
1114                 }
1115
1116                 /* Set the person id */
1117                 if ((contacts_error = contacts_record_set_int(phone_record, _contacts_phone_log.person_id, person_id)) != CONTACTS_ERROR_NONE) {
1118                         EM_DEBUG_EXCEPTION("contacts_record_set_int [person id] failed [%d]", contacts_error);
1119                         goto FINISH_OFF;
1120                 }
1121         }
1122
1123         switch (action) {
1124         case EMAIL_ACTION_SEND_MAIL :
1125                 action_type = CONTACTS_PLOG_TYPE_EMAIL_SENT;
1126                 break;
1127         case EMAIL_ACTION_SYNC_HEADER :
1128                 action_type = CONTACTS_PLOG_TYPE_EMAIL_RECEIVED;
1129                 break;
1130         default :
1131                 EM_DEBUG_EXCEPTION("Unknown action type");
1132                 goto FINISH_OFF;
1133         }
1134
1135         /* Set log type */
1136         if ((contacts_error = contacts_record_set_int(phone_record, _contacts_phone_log.log_type, action_type)) != CONTACTS_ERROR_NONE) {
1137                 EM_DEBUG_EXCEPTION("contacts_record_set_int [log_type] failed [%d]", contacts_error);
1138                 goto FINISH_OFF;
1139         }
1140
1141         /* Set log time */
1142         if ((contacts_error = contacts_record_set_int(phone_record, _contacts_phone_log.log_time, (int)date_time)) != CONTACTS_ERROR_NONE) {
1143                 EM_DEBUG_EXCEPTION("contacts_record_set_str [address] failed [%d]", contacts_error);
1144                 goto FINISH_OFF;
1145         }
1146
1147         /* Set subject */
1148         if ((contacts_error = contacts_record_set_str(phone_record, _contacts_phone_log.extra_data2, subject)) != CONTACTS_ERROR_NONE) {
1149                 EM_DEBUG_EXCEPTION("contacts_record_set_str [subject] failed [%d]", contacts_error);
1150                 goto FINISH_OFF;
1151         }
1152
1153         /* Set Mail id */
1154         if ((contacts_error = contacts_record_set_int(phone_record, _contacts_phone_log.extra_data1, account_id)) != CONTACTS_ERROR_NONE) {
1155                 EM_DEBUG_EXCEPTION("contacts_record_set_int [mail id] failed [%d]", contacts_error);
1156                 goto FINISH_OFF;
1157         }
1158
1159         /* Insert the record in DB */
1160         if ((contacts_error = contacts_db_insert_record(phone_record, NULL)) != CONTACTS_ERROR_NONE) {
1161                 EM_DEBUG_EXCEPTION("contacts_db_insert_record failed [%d]",contacts_error );
1162                 goto FINISH_OFF;
1163         }
1164
1165 FINISH_OFF:
1166
1167         if (phone_record != NULL)
1168                 contacts_record_destroy(phone_record, false);
1169
1170         if (person_record != NULL)
1171                 contacts_record_destroy(person_record, false);
1172
1173         err = convert_contact_err_to_email_err(contacts_error);
1174
1175         if ((contacts_error = contacts_disconnect2()) != CONTACTS_ERROR_NONE) {
1176                 EM_DEBUG_EXCEPTION("Open connect service failed [%d]", contacts_error);
1177                 err = convert_contact_err_to_email_err(contacts_error);
1178         }
1179
1180         EM_DEBUG_FUNC_END("err [%d]", err);
1181         return err;
1182 }
1183
1184 INTERNAL_FUNC int emcore_set_sent_contacts_log(emstorage_mail_tbl_t *input_mail_data)
1185 {
1186         EM_DEBUG_FUNC_BEGIN("input_mail_data : [%p]", input_mail_data);
1187         
1188         int i = 0;
1189         int err = EMAIL_ERROR_NONE;
1190         char email_address[MAX_EMAIL_ADDRESS_LENGTH];
1191         char *address_array[3] = {input_mail_data->full_address_to, input_mail_data->full_address_cc, input_mail_data->full_address_bcc};
1192         ADDRESS *addr = NULL;
1193         ADDRESS *p_addr = NULL;
1194
1195         for (i = 0; i < 3; i++) {
1196                 if (address_array[i] && address_array[i][0] != 0) {
1197                         rfc822_parse_adrlist(&addr, address_array[i], NULL);
1198                         for (p_addr = addr ; p_addr ;p_addr = p_addr->next) {
1199                                 SNPRINTF(email_address, MAX_EMAIL_ADDRESS_LENGTH, "%s@%s", addr->mailbox, addr->host);
1200                                 if ((err = emcore_set_contacts_log(input_mail_data->account_id, email_address, input_mail_data->subject, input_mail_data->date_time, EMAIL_ACTION_SEND_MAIL)) != EMAIL_ERROR_NONE) {
1201                                         EM_DEBUG_EXCEPTION("emcore_set_contacts_log failed : [%d]", err);
1202                                         goto FINISH_OFF;
1203                                 }
1204                         }
1205                 
1206                         if (addr) {     
1207                                 mail_free_address(&addr);
1208                                 addr = NULL;
1209                         }
1210                 }
1211         }
1212
1213 FINISH_OFF:
1214
1215         if (addr) 
1216                 mail_free_address(&addr);
1217
1218         EM_DEBUG_FUNC_END("err [%d]", err);
1219         return err;
1220 }
1221
1222 INTERNAL_FUNC int emcore_set_received_contacts_log(emstorage_mail_tbl_t *input_mail_data)
1223 {
1224         EM_DEBUG_FUNC_BEGIN("input_mail_data : [%p]", input_mail_data);
1225         int err = EMAIL_ERROR_NONE;
1226
1227         if ((err = emcore_set_contacts_log(input_mail_data->account_id, input_mail_data->email_address_sender, input_mail_data->subject, input_mail_data->date_time, EMAIL_ACTION_SYNC_HEADER)) != EMAIL_ERROR_NONE) {
1228                 EM_DEBUG_EXCEPTION("emcore_set_contacts_log failed [%d]", err);
1229         }
1230
1231         EM_DEBUG_FUNC_END("err [%d]", err);
1232         return err;
1233 }
1234
1235 INTERNAL_FUNC int emcore_delete_contacts_log(int account_id)
1236 {
1237         EM_DEBUG_FUNC_BEGIN("account_id [%d]", account_id);
1238
1239         int err = EMAIL_ERROR_NONE;
1240         int contacts_error = CONTACTS_ERROR_NONE;
1241
1242         if ((contacts_error = contacts_connect2()) != CONTACTS_ERROR_NONE) {
1243                 EM_DEBUG_EXCEPTION("Open connect service failed [%d]", contacts_error);
1244                 goto FINISH_OFF;
1245         }
1246         
1247         /* Delete record of the account id */
1248         if ((contacts_error = contacts_phone_log_delete(CONTACTS_PHONE_LOG_DELETE_BY_EMAIL_EXTRA_DATA1, account_id)) != CONTACTS_ERROR_NONE) {
1249                 EM_DEBUG_EXCEPTION("contacts_phone_log_delete failed [%d]", contacts_error);
1250         }
1251         
1252
1253 FINISH_OFF:
1254         err = convert_contact_err_to_email_err(contacts_error);
1255
1256         if ((contacts_error = contacts_disconnect2()) != CONTACTS_ERROR_NONE) {
1257                 EM_DEBUG_EXCEPTION("Open connect service failed [%d]", contacts_error);
1258                 err = convert_contact_err_to_email_err(contacts_error);
1259         }
1260
1261         EM_DEBUG_FUNC_END("err [%d]", err);
1262         return err;
1263 }
1264
1265 INTERNAL_FUNC int emcore_get_mail_display_name(char *email_address, char **contact_display_name, int *err_code)
1266 {
1267         EM_DEBUG_FUNC_BEGIN("contact_name_value[%s], contact_display_name[%p]", email_address, contact_display_name);
1268
1269         int contact_err = 0;
1270         int ret = false;
1271         char *display = NULL;
1272         /* Contact variable */
1273         contacts_record_h record = NULL;
1274
1275         if ((contact_err = emcore_search_contact_info_by_address(_contacts_contact_email._uri, _contacts_contact_email.email, email_address, 1, &record)) != CONTACTS_ERROR_NONE) {
1276                 EM_DEBUG_EXCEPTION("emcore_search_contact_info_by_address failed");
1277                 goto FINISH_OFF;
1278         }
1279 /*
1280         if ((contact_err = contacts_list_get_count(list, &list_count)) != CONTACTS_ERROR_NONE) {
1281                 EM_DEBUG_EXCEPTION("contacts_list_get_count failed");
1282                 goto FINISH_OFF;
1283         }
1284
1285         if (list_count > 1) {
1286                 EM_DEBUG_EXCEPTION("Duplicated contacts info");
1287                 contact_err = CONTACTS_ERROR_INTERNAL;
1288                 goto FINISH_OFF;
1289         } else if (list_count == 0) {
1290                 EM_DEBUG_EXCEPTION("Not found contact info");
1291                 contact_err = CONTACTS_ERROR_NO_DATA;
1292                 goto FINISH_OFF;
1293         }
1294 */
1295
1296         if (contacts_record_get_str(record, _contacts_contact_email.display_name, &display) != CONTACTS_ERROR_NONE) {
1297                 EM_DEBUG_EXCEPTION("contacts_record_get_str failed");
1298                 goto FINISH_OFF;
1299         }
1300
1301         ret = true;
1302
1303 FINISH_OFF:
1304
1305         if (record != NULL)
1306                 contacts_record_destroy(record, false);
1307
1308         if (ret) {
1309                 *contact_display_name = display;
1310         } else {
1311                 *contact_display_name = NULL;
1312                 EM_SAFE_FREE(display);
1313         }
1314
1315         if (err_code != NULL)
1316                 *err_code = convert_contact_err_to_email_err(contact_err);
1317
1318         return ret;
1319 }
1320
1321 int emcore_get_mail_contact_info_with_update(email_mail_contact_info_t *contact_info, char *full_address, int mail_id, int *err_code)
1322 {
1323         EM_DEBUG_FUNC_BEGIN("contact_info[%p], full_address[%s], mail_id[%d], err_code[%p]", contact_info, full_address, mail_id, err_code);
1324         
1325         int ret = false;
1326         int err = EMAIL_ERROR_NONE;
1327         ADDRESS *addr = NULL;
1328         char *address = NULL;
1329         char *temp_emailaddr = NULL;
1330         char *alias = NULL;
1331         int is_searched = false;
1332         int address_length = 0;
1333         char *email_address = NULL;
1334         int contact_name_len = 0;
1335         char temp_string[1024] = { 0 , };
1336         int is_saved = 0;
1337         char *contact_display_name = NULL;
1338         char *contact_display_name_from_contact_info = NULL;
1339         int contact_display_name_len = 0;
1340         int i = 0;
1341         int contact_name_buffer_size = 0;
1342         char *contact_name = NULL;
1343
1344         if (!contact_info)  {
1345                 EM_DEBUG_EXCEPTION("contact_info[%p]", contact_info);
1346                 err = EMAIL_ERROR_INVALID_PARAM;
1347                 goto FINISH_OFF;
1348         }
1349
1350         if (!full_address) {
1351                 full_address = "";
1352                 address_length = 0;
1353                 temp_emailaddr = NULL;
1354         }
1355         else {
1356                 address_length = 2 * EM_SAFE_STRLEN(full_address);
1357                 temp_emailaddr = (char  *)calloc(1, address_length); 
1358         }
1359
1360         em_skip_whitespace(full_address , &address);
1361         EM_DEBUG_LOG("address[address][%s]", address);  
1362
1363
1364         /*  ',' -> "%2C" */
1365         gchar **tokens = g_strsplit(address, ", ", -1);
1366         char *p = g_strjoinv("%2C", tokens);
1367         
1368         g_strfreev(tokens);
1369         
1370         /*  ';' -> ',' */
1371         while (p && p[i] != '\0') 
1372         {
1373                 if (p[i] == ';') 
1374                         p[i] = ',';
1375                 i++;
1376         }
1377         EM_DEBUG_LOG("  2  converted address %s ", p);
1378
1379         rfc822_parse_adrlist(&addr, p, NULL);
1380
1381         EM_SAFE_FREE(p);
1382         EM_DEBUG_LOG("  3  full_address  %s ", full_address);
1383
1384         if (!addr) {
1385                 EM_DEBUG_EXCEPTION("rfc822_parse_adrlist failed...");
1386                 err = EMAIL_ERROR_INVALID_ADDRESS;
1387                 goto FINISH_OFF;
1388         }
1389
1390         contact_name_buffer_size = address_length;
1391         contact_name = (char*)em_malloc(contact_name_buffer_size);
1392
1393         if (!contact_name) {
1394                 EM_DEBUG_EXCEPTION("Memory allocation error!");
1395                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1396                 goto FINISH_OFF;
1397         }
1398         
1399         while (addr != NULL)  {
1400                 if (addr->mailbox && addr->host) {      
1401                         if (!strncmp(addr->mailbox , "UNEXPECTED_DATA_AFTER_ADDRESS", strlen("UNEXPECTED_DATA_AFTER_ADDRESS")) || !strncmp(addr->mailbox , "INVALID_ADDRESS", strlen("INVALID_ADDRESS")) || !strncmp(addr->host , ".SYNTAX-ERROR.", strlen(".SYNTAX-ERROR.")))
1402                 {
1403                                 EM_DEBUG_LOG("Invalid address ");
1404                                 addr = addr->next;
1405                                 continue;
1406                         }
1407                 }
1408                 else  {
1409                         EM_DEBUG_LOG("Error in parsing..! ");
1410                         addr = addr->next;
1411                         continue;
1412                 }
1413
1414                 EM_SAFE_FREE(email_address);
1415                 email_address = g_strdup_printf("%s@%s", addr->mailbox ? addr->mailbox  :  "", addr->host ? addr->host  :  "");
1416                 
1417                 EM_DEBUG_LOG(" addr->personal[%s]", addr->personal);
1418                 EM_DEBUG_LOG(" email_address[%s]", email_address);
1419         
1420                 is_searched = false;
1421                 EM_DEBUG_LOG(" >>>>> emcore_get_mail_contact_info - 10");
1422         
1423                 if (emcore_get_mail_display_name(email_address, &contact_display_name_from_contact_info, &err) && err == EMAIL_ERROR_NONE) {
1424                         contact_display_name = contact_display_name_from_contact_info;
1425
1426                         EM_DEBUG_LOG(">>> contact_name[%s]", contact_display_name);
1427                         /*  Make display name string */
1428                         is_searched = true;
1429
1430                         if (mail_id == 0 || (contact_name_len == 0)) {  /*  save only the first address information - 09-SEP-2010 */
1431                                 contact_display_name_len = EM_SAFE_STRLEN(contact_display_name);
1432                                 if (contact_name_len + contact_display_name_len >= contact_name_buffer_size) {  /*  re-alloc memory */
1433                                         char *temp = contact_name;
1434                                         contact_name_buffer_size += contact_name_buffer_size;
1435                                         contact_name = (char  *)calloc(1, contact_name_buffer_size); 
1436                                         if (contact_name == NULL) {
1437                                                 EM_DEBUG_EXCEPTION("Memory allocation failed.");
1438                                                 EM_SAFE_FREE(temp);
1439                                                 goto FINISH_OFF;
1440                                         }
1441                                         snprintf(contact_name, contact_name_buffer_size, "%s", temp);
1442                                         EM_SAFE_FREE(temp);
1443                                 }
1444
1445                                 /* snprintf(temp_string, sizeof(temp_string), "%c%d%c%s <%s>%c", start_text_ascii, contact_index, start_text_ascii, contact_display_name, email_address, end_text_ascii); */
1446                                 if (addr->next == NULL) {
1447                                         snprintf(temp_string, sizeof(temp_string), "\"%s\" <%s>", contact_display_name, email_address);
1448                                 }
1449                                 else {
1450                                         snprintf(temp_string, sizeof(temp_string), "\"%s\" <%s>, ", contact_display_name, email_address);
1451                                 }                                       
1452
1453                                 contact_display_name_len = EM_SAFE_STRLEN(temp_string);
1454                                 if (contact_name_len + contact_display_name_len >= contact_name_buffer_size) {  /*  re-alloc memory */
1455                                         char *temp = contact_name;
1456                                         contact_name_buffer_size += contact_name_buffer_size;
1457                                         contact_name = (char  *)calloc(1, contact_name_buffer_size); 
1458                                         if (contact_name == NULL) {
1459                                                 EM_DEBUG_EXCEPTION("Memory allocation failed.");
1460                                                 EM_SAFE_FREE(temp);
1461                                                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1462                                                 goto FINISH_OFF;
1463                                         }
1464                                         snprintf(contact_name, contact_name_buffer_size, "%s", temp);
1465                                         EM_SAFE_FREE(temp);
1466                                 }
1467                                 snprintf(contact_name + contact_name_len, contact_name_buffer_size - contact_name_len, "%s", temp_string);
1468                                 contact_name_len += contact_display_name_len;
1469                                 EM_DEBUG_LOG("new contact_name >>>>> %s ", contact_name);
1470                         }
1471                 }
1472                 else {
1473                         EM_DEBUG_LOG("emcore_get_mail_display_name - Not found contact record(if err is 203) or error [%d]", err);
1474                 }
1475
1476                 /*  if contact doesn't exist, use alias or email address as display name */
1477                 if (addr->personal != NULL) {
1478                         /*  "%2C" -> ',' */
1479                         tokens = g_strsplit(addr->personal, "%2C", -1);
1480                         
1481                         EM_SAFE_FREE(addr->personal);
1482                         
1483                         addr->personal = g_strjoinv(", ", tokens);
1484                         
1485                         g_strfreev(tokens);
1486                         /* contact_info->contact_name = EM_SAFE_STRDUP(addr->personal); */
1487                         alias = addr->personal;
1488                 }
1489                 else {
1490                         /* alias = addr->mailbox ? addr->mailbox  :  ""; */
1491                         alias = email_address;
1492                 }
1493                 contact_info->alias = EM_SAFE_STRDUP(alias);
1494
1495                 if (!is_searched) {
1496                         contact_display_name = alias;
1497                         contact_info->contact_id = -1;          /*  NOTE :  This is valid only if there is only one address. */
1498                         contact_info->storage_type = -1;
1499
1500                         /*  Make display name string */
1501                         EM_DEBUG_LOG("contact_display_name : [%s]", contact_display_name);
1502                         EM_DEBUG_LOG("email_address : [%s]", email_address);
1503
1504                         /*  if mail_id is 0, return only contact info without saving contact info to DB */
1505                         if (mail_id == 0 || (contact_name_len == 0))             {      /*  save only the first address information - 09-SEP-2010 */
1506                                 /* snprintf(temp_string, sizeof(temp_string), "%c%d%c%s <%s>%c", start_text_ascii, contact_index, start_text_ascii, contact_display_name, email_address, end_text_ascii); */
1507                                 if (addr->next == NULL) {
1508                                         snprintf(temp_string, sizeof(temp_string), "\"%s\" <%s>", contact_display_name, email_address);
1509                                 }
1510                                 else {
1511                                         snprintf(temp_string, sizeof(temp_string), "\"%s\" <%s>, ", contact_display_name, email_address);
1512                                 }
1513                                 EM_DEBUG_LOG("temp_string[%s]", temp_string);
1514
1515                                 contact_display_name_len = EM_SAFE_STRLEN(temp_string);
1516                                 if (contact_name_len + contact_display_name_len >= contact_name_buffer_size) {  /*  re-alloc memory */
1517                                         char *temp = contact_name;
1518                                         contact_name_buffer_size += contact_name_buffer_size;
1519                                         contact_name = (char  *)calloc(1, contact_name_buffer_size); 
1520                                         if (contact_name == NULL) {
1521                                                 EM_DEBUG_EXCEPTION("Memory allocation failed.");
1522                                                 EM_SAFE_FREE(temp);
1523                                                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1524                                                 goto FINISH_OFF;
1525                                         }
1526                                         snprintf(contact_name, contact_name_buffer_size, "%s", temp);
1527                                         EM_SAFE_FREE(temp);
1528                                 }
1529                                 
1530                                 snprintf(contact_name + contact_name_len, contact_name_buffer_size - contact_name_len, "%s", temp_string);
1531                                 contact_name_len += contact_display_name_len;
1532                                 EM_DEBUG_LOG("new contact_name >>>>> %s ", contact_name);
1533                         }
1534                 }
1535
1536                 if (temp_emailaddr && email_address) {  
1537                         if (mail_id == 0) {     /*  if mail_id is 0, return only contact info without saving contact info to DB */
1538                                 /* snprintf(temp_emailaddr, 400, "%s", contact_info->email_address); */
1539                                 EM_SAFE_STRCAT(temp_emailaddr, email_address);
1540                                 if (addr->next != NULL)
1541                                         EM_SAFE_STRCAT(temp_emailaddr, ", ");
1542                                 EM_DEBUG_LOG(">>>> TEMP EMail Address [ %s ] ", temp_emailaddr);
1543                         }
1544                         else {  /*  save only the first address information - 09-SEP-2010 */
1545                                 if (is_saved == 0) {
1546                                         is_saved = 1;
1547                                         /* snprintf(temp_emailaddr, 400, "%s", contact_info->email_address); */
1548                                         EM_SAFE_STRCAT(temp_emailaddr, email_address);
1549                                         /*
1550                                         if (addr->next != NULL)
1551                                                 EM_SAFE_STRCAT(temp_emailaddr, ", ");
1552                                         */
1553                                         EM_DEBUG_LOG(">>>> TEMP EMail Address [ %s ] ", temp_emailaddr);
1554                                 }
1555                         }
1556                 }
1557
1558                 EM_SAFE_FREE(contact_display_name_from_contact_info);
1559                 /*  next address */
1560                 addr = addr->next;
1561         } /*  while (addr != NULL) */
1562
1563         if (temp_emailaddr) {
1564                 EM_DEBUG_LOG(">>>> TEMPEMAIL ADDR [ %s ] ", temp_emailaddr);
1565                 contact_info->email_address = temp_emailaddr;
1566                 temp_emailaddr = NULL;
1567         }
1568
1569
1570         contact_info->contact_name = g_strdup(contact_name); /*prevent 40020*/
1571
1572         ret = true;
1573         
1574 FINISH_OFF: 
1575
1576         EM_SAFE_FREE(email_address);
1577         EM_SAFE_FREE(address);
1578         EM_SAFE_FREE(temp_emailaddr);
1579         EM_SAFE_FREE(contact_name);             
1580         EM_SAFE_FREE(contact_display_name_from_contact_info);
1581         
1582         if (err_code != NULL)
1583                 *err_code = err;
1584         
1585         return ret;
1586 }
1587
1588 int emcore_free_contact_info(email_mail_contact_info_t *contact_info, int *err_code)
1589 {
1590         EM_DEBUG_FUNC_BEGIN("contact_info[%p], err_code[%p]", contact_info, err_code);
1591         
1592         int ret = false;
1593         int err = EMAIL_ERROR_NONE;
1594         
1595         if (!contact_info)  {
1596                 EM_DEBUG_EXCEPTION("contact_info[%p]", contact_info);
1597                 err = EMAIL_ERROR_INVALID_PARAM;
1598                 goto FINISH_OFF;
1599         }
1600         
1601         EM_SAFE_FREE(contact_info->contact_name);
1602         EM_SAFE_FREE(contact_info->email_address);
1603         EM_SAFE_FREE(contact_info->alias);
1604         
1605         contact_info->storage_type = -1;
1606         contact_info->contact_id = -1;
1607         
1608         ret = true;
1609         
1610 FINISH_OFF: 
1611         if (err_code != NULL)
1612                 *err_code = err;
1613         EM_DEBUG_FUNC_END();
1614         return ret;
1615 }
1616
1617 int emcore_sync_contact_info(int mail_id, int *err_code)
1618 {
1619         EM_PROFILE_BEGIN(emCoreMailContactSync);
1620         EM_DEBUG_FUNC_BEGIN();
1621
1622         int ret = false;
1623         int err = EMAIL_ERROR_NONE;
1624
1625         emstorage_mail_tbl_t *mail = NULL;
1626
1627         email_mail_contact_info_t contact_info_from;
1628         email_mail_contact_info_t contact_info_to;
1629         email_mail_contact_info_t contact_info_cc;
1630         email_mail_contact_info_t contact_info_bcc;
1631         
1632         EM_DEBUG_LOG("mail_id[%d], err_code[%p]", mail_id, err_code);
1633         
1634         memset(&contact_info_from, 0x00, sizeof(email_mail_contact_info_t));    
1635         memset(&contact_info_to, 0x00, sizeof(email_mail_contact_info_t));
1636         memset(&contact_info_cc, 0x00, sizeof(email_mail_contact_info_t));
1637         memset(&contact_info_bcc, 0x00, sizeof(email_mail_contact_info_t));     
1638
1639         if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail)  {
1640                 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
1641
1642                 goto FINISH_OFF;
1643         }
1644
1645         if (mail->full_address_from != NULL) {
1646                 if (!emcore_get_mail_contact_info_with_update(&contact_info_from, mail->full_address_from, mail_id, &err))  {
1647                         EM_DEBUG_EXCEPTION("emcore_get_mail_contact_info failed [%d]", err);
1648                 }
1649         }
1650
1651         if (mail->full_address_to != NULL)  {
1652                 if (!emcore_get_mail_contact_info_with_update(&contact_info_to, mail->full_address_to, mail_id, &err))  {
1653                         EM_DEBUG_EXCEPTION("emcore_get_mail_contact_info failed [%d]", err);
1654                 }
1655         }
1656
1657         if (mail->full_address_cc != NULL)  {
1658                 if (!emcore_get_mail_contact_info_with_update(&contact_info_cc, mail->full_address_cc, mail_id, &err))  {
1659                         EM_DEBUG_EXCEPTION("emcore_get_mail_contact_info failed [%d]", err);
1660                 }
1661         }
1662
1663         if (mail->full_address_bcc != NULL)  {
1664                 if (!emcore_get_mail_contact_info_with_update(&contact_info_bcc, mail->full_address_bcc, mail_id, &err))  {
1665                         EM_DEBUG_EXCEPTION("emcore_get_mail_contact_info failed [%d]", err);
1666                 }
1667         }       
1668
1669         EM_SAFE_FREE(mail->email_address_sender);
1670         mail->email_address_sender = contact_info_from.email_address;
1671         contact_info_from.contact_name = NULL;
1672         contact_info_from.email_address = NULL;
1673         EM_SAFE_FREE(mail->email_address_recipient);
1674         if (mail->full_address_to != NULL) {
1675                 mail->email_address_recipient = contact_info_to.email_address;
1676                 contact_info_to.contact_name = NULL;
1677                 contact_info_to.email_address = NULL;
1678         }
1679         else if (mail->full_address_cc != NULL) {
1680                 mail->email_address_recipient = contact_info_cc.email_address;
1681                 contact_info_cc.contact_name = NULL;
1682                 contact_info_cc.email_address = NULL;
1683         }
1684         else if (mail->full_address_bcc != NULL) {
1685                 mail->email_address_recipient = contact_info_bcc.email_address;
1686                 contact_info_bcc.contact_name  = NULL;
1687                 contact_info_bcc.email_address = NULL;
1688         }
1689         
1690         /*  Update DB */
1691         if (!emstorage_change_mail_field(mail_id, UPDATE_ALL_CONTACT_INFO, mail, false, &err))  {                                       
1692                 EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed [%d]", err);
1693
1694                 goto FINISH_OFF;
1695         }       
1696
1697         ret = true;
1698
1699 FINISH_OFF: 
1700         if (mail != NULL)
1701                 emstorage_free_mail(&mail, 1, NULL);
1702
1703         emcore_free_contact_info(&contact_info_from, NULL);
1704         emcore_free_contact_info(&contact_info_to, NULL);
1705         emcore_free_contact_info(&contact_info_cc, NULL);
1706         emcore_free_contact_info(&contact_info_bcc, NULL);      
1707
1708         if (err_code != NULL)
1709                 *err_code = err;
1710
1711         EM_PROFILE_END(emCoreMailContactSync);
1712         return ret;
1713 }
1714
1715 /*  1. parsing  :  alias and address */
1716 /*  2. sync with contact */
1717 /*  3. make glist of address info */
1718 static int emcore_sync_address_info(email_address_type_t address_type, char *full_address, GList **address_info_list, int *err_code)
1719 {
1720         EM_DEBUG_FUNC_BEGIN("address type[%d], address_info_list[%p], full_address[%p]", address_type, address_info_list, full_address);
1721
1722         int ret = false;
1723         int error = EMAIL_ERROR_NONE;
1724         int contact_index = -1;
1725         int is_search = false;
1726         char *alias = NULL;
1727         char *address = NULL;
1728         char *contact_display_name_from_contact_info = NULL;
1729         char email_address[MAX_EMAIL_ADDRESS_LENGTH];
1730         email_address_info_t *p_address_info = NULL;
1731         ADDRESS *addr = NULL;
1732         
1733         if (address_info_list == NULL) {
1734                 EM_DEBUG_EXCEPTION("Invalid param : address_info_list is NULL");
1735                 error = EMAIL_ERROR_INVALID_PARAM;
1736                 goto FINISH_OFF;
1737         }
1738
1739         /*  Parsing */
1740         address = EM_SAFE_STRDUP(full_address);
1741
1742         /*  ',' -> "%2C" */
1743         gchar **tokens = g_strsplit(address, ", ", -1);
1744         char *p = g_strjoinv("%2C", tokens);
1745         int i = 0;
1746         
1747         g_strfreev(tokens);
1748         
1749         /*  ';' -> ',' */
1750         while (p && p[i] != '\0')  {
1751                 if (p[i] == ';') 
1752                         p[i] = ',';
1753                 i++;
1754         }
1755
1756         rfc822_parse_adrlist(&addr, p, NULL);
1757
1758         EM_SAFE_FREE(p);        
1759
1760         if (!addr)  {
1761                 EM_DEBUG_EXCEPTION("rfc822_parse_adrlist failed...");
1762                 error = EMAIL_ERROR_INVALID_PARAM;              
1763                 goto FINISH_OFF;
1764         }
1765
1766         /*  Get a contact name */
1767         while (addr != NULL)  {
1768                 if (addr->mailbox && addr->host) {      
1769                         if (!strcmp(addr->mailbox , "UNEXPECTED_DATA_AFTER_ADDRESS") || !strcmp(addr->mailbox , "INVALID_ADDRESS") || !strcmp(addr->host , ".SYNTAX-ERROR.")) {
1770                                 EM_DEBUG_LOG("Invalid address ");
1771                                 addr = addr->next;
1772                                 continue;
1773                         }
1774                 }
1775                 else  {
1776                         EM_DEBUG_LOG("Error in parsing..! ");
1777                         addr = addr->next;
1778                         continue;
1779                 }
1780
1781                 /*   set display name  */
1782                 /*    1) contact name */
1783                 /*    2) alias (if a alias in an original mail doesn't exist, this field is set with email address */
1784                 /*    3) email address   */
1785
1786                 if (!(p_address_info = (email_address_info_t *)malloc(sizeof(email_address_info_t)))) {
1787                         EM_DEBUG_EXCEPTION("malloc failed...");
1788                         error = EMAIL_ERROR_OUT_OF_MEMORY;
1789                         goto FINISH_OFF;
1790                 }       
1791                 memset(p_address_info, 0x00, sizeof(email_address_info_t)); 
1792
1793                 SNPRINTF(email_address, MAX_EMAIL_ADDRESS_LENGTH, "%s@%s", addr->mailbox ? addr->mailbox  :  "", addr->host ? addr->host  :  "");
1794  
1795                 EM_DEBUG_LOG("Search a contact  :  address[%s]", email_address);
1796
1797                 is_search = false;
1798
1799                 if (emcore_get_mail_display_name(email_address, &contact_display_name_from_contact_info, &error) && error == EMAIL_ERROR_NONE) {
1800                         EM_DEBUG_LOG(">>> contact display name[%s]", contact_display_name_from_contact_info);
1801
1802                         is_search = true;
1803                 }
1804                 else
1805                         EM_DEBUG_EXCEPTION("emcore_get_mail_display_name - Not found contact record(if err is -203) or error [%d]", error);
1806
1807                 if (is_search == true) {
1808                         p_address_info->contact_id = contact_index;             
1809                         p_address_info->storage_type = -1;                              
1810                         p_address_info->display_name = contact_display_name_from_contact_info;
1811                         EM_DEBUG_LOG("display_name from contact[%s]", p_address_info->display_name);
1812                 }
1813                 else {
1814                         /*  if contact doesn't exist, use alias or email address as display name */
1815                         if (addr->personal != NULL) {
1816                                 /*  "%2C" -> ',' */
1817                                 tokens = g_strsplit(addr->personal, "%2C", -1);
1818                                 
1819                                 EM_SAFE_FREE(addr->personal);
1820                                 
1821                                 addr->personal = g_strjoinv(", ", tokens);
1822                                 
1823                                 g_strfreev(tokens);
1824                                 alias = addr->personal;
1825                         }
1826                         else {
1827                                 alias = NULL;
1828                         }               
1829                         p_address_info->contact_id = -1;
1830                         p_address_info->storage_type = -1;      
1831                         /*  Use an alias or an email address as a display name */
1832                         if (alias == NULL)
1833                                 p_address_info->display_name = EM_SAFE_STRDUP(email_address);
1834                         else
1835                                 p_address_info->display_name = EM_SAFE_STRDUP(alias);
1836
1837                         EM_DEBUG_LOG("display_name from email [%s]", p_address_info->display_name);
1838                 }
1839                 
1840                 p_address_info->address = EM_SAFE_STRDUP(email_address);
1841                 p_address_info->address_type = address_type;
1842
1843                 EM_DEBUG_LOG("email address[%s]", p_address_info->address);
1844
1845                 *address_info_list = g_list_append(*address_info_list, p_address_info);
1846                 p_address_info = NULL;
1847
1848                 EM_DEBUG_LOG("after append");
1849
1850                 alias = NULL;
1851
1852                 EM_DEBUG_LOG("next address[%p]", addr->next);
1853
1854                 /*  next address */
1855                 addr = addr->next;
1856         }
1857
1858         ret = true;
1859         
1860 FINISH_OFF: 
1861
1862         EM_SAFE_FREE(address);
1863         
1864         if (err_code != NULL)
1865                 *err_code = error;      
1866         EM_DEBUG_FUNC_END();
1867         return ret;
1868 }
1869
1870 static gint address_compare(gconstpointer a, gconstpointer b)
1871 {
1872         EM_DEBUG_FUNC_BEGIN();
1873         email_sender_list_t *recipients_list1 = (email_sender_list_t *)a;
1874         email_sender_list_t *recipients_list2 = (email_sender_list_t *)b;       
1875
1876         EM_DEBUG_FUNC_END();    
1877         return strcmp(recipients_list1->address, recipients_list2->address);
1878 }
1879
1880 INTERNAL_FUNC GList *emcore_get_recipients_list(GList *old_recipients_list, char *full_address, int *err_code)
1881 {
1882         EM_DEBUG_FUNC_BEGIN();
1883
1884         int i = 0, err = EMAIL_ERROR_NONE;
1885         int is_search = false;
1886         char *address = NULL;
1887         char email_address[MAX_EMAIL_ADDRESS_LENGTH];
1888         char *display_name = NULL;
1889         char *alias = NULL;
1890         ADDRESS *addr = NULL;
1891         GList *new_recipients_list = old_recipients_list;
1892         GList *recipients_list;
1893
1894         email_sender_list_t *temp_recipients_list = NULL;
1895         email_sender_list_t *old_recipients_list_t = NULL;
1896         
1897         if (full_address == NULL || EM_SAFE_STRLEN(full_address) == 0) {
1898                 EM_DEBUG_EXCEPTION("Invalid param : full_address NULL or empty");
1899                 err = EMAIL_ERROR_INVALID_PARAM;
1900                 goto FINISH_OFF;
1901         }
1902
1903         address = EM_SAFE_STRDUP(full_address);
1904
1905         gchar **tokens = g_strsplit(address, ", ", -1);
1906         char *p = g_strjoinv("%2C", tokens);
1907
1908         g_strfreev(tokens);
1909
1910         while (p && p[i] != '\0') {
1911                 if (p[i] == ';') 
1912                         p[i] = ',';
1913                 i++;
1914         }
1915
1916         rfc822_parse_adrlist(&addr, p, NULL);
1917
1918         EM_SAFE_FREE(p);
1919
1920         if (!addr) {
1921                 EM_DEBUG_EXCEPTION("rfc822_parse_adrlist failed...");
1922                 err = EMAIL_ERROR_INVALID_PARAM;
1923                 goto FINISH_OFF;
1924         }
1925
1926         while (addr != NULL) {
1927                 if (addr->mailbox && addr->host) {
1928                         if (!strcmp(addr->mailbox , "UNEXPECTED_DATA_AFTER_ADDRESS") || !strcmp(addr->mailbox , "INVALID_ADDRESS") || !strcmp(addr->host , ".SYNTAX-ERROR.")) {
1929                                 EM_DEBUG_LOG("Invalid address ");
1930                                 addr = addr->next;
1931                                 continue;
1932                         } 
1933                 } else {
1934                         EM_DEBUG_LOG("Error in parsing..! ");
1935                         addr = addr->next;
1936                         continue;
1937                 }                       
1938         
1939                 if ((temp_recipients_list = g_new0(email_sender_list_t, 1)) == NULL) {
1940                         EM_DEBUG_EXCEPTION("EMAIL_ERROR_OUT_OF_MEMORY");
1941                         err = EMAIL_ERROR_OUT_OF_MEMORY;
1942                         goto FINISH_OFF;
1943                 }
1944
1945                 
1946                 SNPRINTF(email_address, MAX_EMAIL_ADDRESS_LENGTH, "%s@%s", addr->mailbox ? addr->mailbox : "", addr->host ? addr->host : "");
1947
1948                 EM_DEBUG_LOG("Search a contact : address[%s]", email_address);
1949
1950                 if (emcore_get_mail_display_name(email_address, &display_name, &err) && err == EMAIL_ERROR_NONE) {
1951                         EM_DEBUG_LOG(">>> contact display name[%s]", display_name);
1952                         is_search = true;
1953                 } else {
1954                         EM_DEBUG_LOG("emcore_get_mail_display_name - Not found contact record(if err is -203) or error [%d]", err);
1955                 }
1956
1957                 if (is_search) {
1958                         temp_recipients_list->display_name = display_name;
1959                         EM_DEBUG_LOG("display_name from contact[%s]", temp_recipients_list->display_name);
1960                 } else {
1961                         if (addr->personal != NULL) {
1962                                 tokens = g_strsplit(addr->personal, "%2C", -1);
1963                                 EM_SAFE_FREE(addr->personal);
1964                                 addr->personal = g_strjoinv(", ", tokens);
1965                                 g_strfreev(tokens);
1966                                 alias = addr->personal;
1967                         } else {
1968                                 alias = NULL;
1969                         }
1970
1971                         if (alias == NULL)
1972                                 temp_recipients_list->display_name = EM_SAFE_STRDUP(email_address);
1973                         else
1974                                 temp_recipients_list->display_name = EM_SAFE_STRDUP(alias);
1975
1976                         EM_DEBUG_LOG("display_name from contact[%s]", temp_recipients_list->display_name);
1977                 }
1978
1979                 temp_recipients_list->address = EM_SAFE_STRDUP(email_address);
1980                 EM_DEBUG_LOG("email address[%s]", temp_recipients_list->address);
1981
1982                 EM_SAFE_FREE(display_name);
1983                 EM_DEBUG_LOG("next address[%p]", addr->next);
1984
1985                 recipients_list = g_list_first(new_recipients_list);
1986                 while (recipients_list != NULL) {
1987                         old_recipients_list_t = (email_sender_list_t *)recipients_list->data;
1988                         if (!strcmp(old_recipients_list_t->address, temp_recipients_list->address)) {
1989                                 old_recipients_list_t->total_count = old_recipients_list_t->total_count + 1;
1990                                 g_free(temp_recipients_list);
1991                                 goto FINISH_OFF;
1992                         }
1993                         recipients_list = g_list_next(recipients_list);
1994                 }
1995
1996                 new_recipients_list = g_list_insert_sorted(new_recipients_list, temp_recipients_list, address_compare);
1997
1998                 g_free(temp_recipients_list);
1999                 temp_recipients_list = NULL;
2000
2001                 alias = NULL;
2002                 addr = addr->next;
2003         }
2004
2005 FINISH_OFF:
2006
2007         EM_SAFE_FREE(address);
2008
2009         if (err_code != NULL)
2010                 *err_code = err;
2011
2012         EM_DEBUG_FUNC_END();
2013         return new_recipients_list;                     
2014 }
2015
2016 INTERNAL_FUNC int emcore_get_mail_address_info_list(int mail_id, email_address_info_list_t **address_info_list, int *err_code)
2017 {
2018         EM_DEBUG_FUNC_BEGIN("mail_id[%d], address_info_list[%p]", mail_id, address_info_list);
2019
2020         int ret = false, err = EMAIL_ERROR_NONE;
2021         int failed = true;
2022         int contact_error;
2023
2024         emstorage_mail_tbl_t *mail = NULL;
2025         email_address_info_list_t *p_address_info_list = NULL;
2026
2027         if (mail_id <= 0 || !address_info_list) {
2028                 EM_DEBUG_EXCEPTION("mail_id[%d], address_info_list[%p]", mail_id, address_info_list);
2029                 err = EMAIL_ERROR_INVALID_PARAM;
2030                 goto FINISH_OFF;
2031         }
2032         
2033         /* get mail from mail table */
2034         if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail) {
2035                 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
2036                 
2037
2038                 goto FINISH_OFF;
2039         }
2040
2041         if (!(p_address_info_list = (email_address_info_list_t *)malloc(sizeof(email_address_info_list_t)))) {
2042                 EM_DEBUG_EXCEPTION("malloc failed...");
2043                 err = EMAIL_ERROR_OUT_OF_MEMORY;
2044                 goto FINISH_OFF;
2045         }       
2046         memset(p_address_info_list, 0x00, sizeof(email_address_info_list_t));   
2047
2048         if ((contact_error = contacts_connect2()) == CONTACTS_ERROR_NONE)        {              
2049                 EM_DEBUG_LOG("Open Contact Service Success");   
2050         }       
2051         else     {              
2052                 EM_DEBUG_EXCEPTION("contact_db_service_connect failed [%d]", contact_error);            
2053                 err = EMAIL_ERROR_DB_FAILURE;
2054                 goto FINISH_OFF;
2055         }
2056
2057         if (mail->full_address_from && emcore_sync_address_info(EMAIL_ADDRESS_TYPE_FROM, mail->full_address_from, &p_address_info_list->from, &err))
2058                 failed = false;
2059         if (mail->full_address_to && emcore_sync_address_info(EMAIL_ADDRESS_TYPE_TO, mail->full_address_to, &p_address_info_list->to, &err))
2060                 failed = false;
2061         if (mail->full_address_cc && emcore_sync_address_info(EMAIL_ADDRESS_TYPE_CC, mail->full_address_cc, &p_address_info_list->cc, &err))
2062                 failed = false;
2063         if (mail->full_address_bcc && emcore_sync_address_info(EMAIL_ADDRESS_TYPE_BCC, mail->full_address_bcc, &p_address_info_list->bcc, &err))
2064                 failed = false;
2065
2066         if ((contact_error = contacts_disconnect2()) == CONTACTS_ERROR_NONE)
2067                 EM_DEBUG_LOG("Close Contact Service Success");
2068         else
2069                 EM_DEBUG_EXCEPTION("Close Contact Service Fail [%d]", contact_error);
2070
2071         if (failed == false)
2072                 ret = true;
2073
2074 FINISH_OFF: 
2075         if (ret == true) 
2076                 *address_info_list = p_address_info_list;
2077         else if (p_address_info_list != NULL)
2078                 emstorage_free_address_info_list(&p_address_info_list);
2079
2080         emstorage_free_mail(&mail, 1, NULL);
2081
2082         if (err_code != NULL)
2083                 *err_code = err;
2084
2085         EM_DEBUG_FUNC_END();
2086         return ret;
2087 }
2088
2089 /* description
2090  *    get a mail data
2091  * arguments  
2092  *    input_mail_id     : [in]  mail id
2093  *    output_mail_data  : [out] double pointer to hold mail data.
2094  * return  
2095  *    succeed  :  EMAIL_ERROR_NONE
2096  *    fail     :  error code
2097  */
2098 INTERNAL_FUNC int emcore_get_mail_data(int input_mail_id, email_mail_data_t **output_mail_data)
2099 {
2100         EM_DEBUG_FUNC_BEGIN("input_mail_id[%d], output_mail_data[%p]", input_mail_id, output_mail_data);
2101         
2102         int             error = EMAIL_ERROR_NONE;
2103         int             result_mail_count = 0;
2104         char            conditional_clause_string[QUERY_SIZE] = { 0, };
2105         emstorage_mail_tbl_t *result_mail_tbl = NULL;
2106         
2107         if (input_mail_id == 0 || !output_mail_data)  {
2108                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2109                 error = EMAIL_ERROR_INVALID_PARAM;
2110                 goto FINISH_OFF;
2111         }
2112
2113         SNPRINTF(conditional_clause_string, QUERY_SIZE, "WHERE mail_id = %d", input_mail_id);
2114         
2115         if(!emstorage_query_mail_tbl(conditional_clause_string, true, &result_mail_tbl, &result_mail_count, &error)) {
2116                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl falied [%d]", error);
2117                 goto FINISH_OFF;
2118         }
2119
2120         if(!em_convert_mail_tbl_to_mail_data(result_mail_tbl, 1, output_mail_data, &error)) {
2121                 EM_DEBUG_EXCEPTION("em_convert_mail_tbl_to_mail_data falied [%d]", error);
2122                 goto FINISH_OFF;
2123         }
2124         
2125 FINISH_OFF: 
2126         if (result_mail_tbl)
2127                 emstorage_free_mail(&result_mail_tbl, result_mail_count, NULL);
2128
2129         EM_DEBUG_FUNC_END("error [%d]", error);
2130         return error;
2131 }
2132
2133
2134 /* internal function */
2135 void emcore_free_body_sharep(void **p)
2136 {
2137         EM_DEBUG_FUNC_BEGIN();
2138         EM_SAFE_FREE(*p);
2139         EM_DEBUG_FUNC_END();
2140 }
2141
2142 int emcore_check_drm(emstorage_attachment_tbl_t *input_attachment_tb_data)
2143 {
2144         EM_DEBUG_FUNC_BEGIN();
2145         int ret = 0;
2146 #ifdef __FEATURE_DRM__
2147         drm_bool_type_e drm_file = DRM_UNKNOWN;
2148         drm_file_info_s drm_file_info; 
2149
2150         if (input_attachment_tb_data == NULL)
2151                 return ret;
2152
2153         ret = drm_is_drm_file(input_attachment_tb_data->attachment_path, &drm_file);
2154
2155         if (ret == DRM_RETURN_SUCCESS && drm_file == DRM_TRUE) {
2156                 if (drm_get_file_info (input_attachment_tb_data->attachment_path, &drm_file_info) == DRM_RETURN_SUCCESS) {
2157                         input_attachment_tb_data->attachment_drm_type = 0;
2158                         EM_DEBUG_LOG ("fileInfo is [%d]", drm_file_info.oma_info.method);
2159                         if (drm_file_info.oma_info.method != DRM_METHOD_TYPE_UNDEFINED) {
2160                                 input_attachment_tb_data->attachment_drm_type = drm_file_info.oma_info.method;
2161                                 ret = 1;
2162                         }
2163                 }
2164         }
2165         else {
2166                 EM_DEBUG_LOG("not DRM file %s", input_attachment_tb_data->attachment_path);
2167                 input_attachment_tb_data->attachment_drm_type = 0;
2168                 ret = 0;
2169         }
2170 #endif
2171         EM_DEBUG_FUNC_END();
2172         return ret;
2173 }
2174
2175
2176 /* description
2177  *    get mail attachment from local mailbox
2178  * arguments  
2179  *    mailbox  :  server mailbox
2180  *    mail_id  :  mai id to own attachment
2181  *    attachment  :  the number string to be downloaded
2182  *    callback  :  function callback. if NULL, ignored.
2183  * return  
2184  *     succeed  :  1
2185  *     fail  :  0
2186  */
2187 INTERNAL_FUNC int emcore_get_attachment_info(int attachment_id, email_attachment_data_t **attachment, int *err_code)
2188 {
2189         EM_DEBUG_FUNC_BEGIN("attachment_id[%d], attachment[%p], err_code[%p]", attachment_id, attachment, err_code);
2190
2191         if (attachment == NULL || attachment_id == 0)  {
2192                 EM_DEBUG_EXCEPTION("attachment_id[%d], attachment[%p]", attachment_id, attachment);
2193                 if (err_code != NULL)
2194                         *err_code = EMAIL_ERROR_INVALID_PARAM;
2195                 return false;
2196         }
2197         
2198         int ret = false;
2199         int err = EMAIL_ERROR_NONE;
2200         emstorage_attachment_tbl_t *attachment_tbl = NULL;
2201         
2202         /* get attachment from attachment tbl */
2203         if (!emstorage_get_attachment(attachment_id, &attachment_tbl, true, &err) || !attachment_tbl)  {
2204                 EM_DEBUG_EXCEPTION("emstorage_get_attachment failed [%d]", err);
2205
2206                 goto FINISH_OFF;
2207         }
2208         
2209         *attachment = em_malloc(sizeof(email_attachment_data_t));
2210         if (!*attachment)  {
2211                 EM_DEBUG_EXCEPTION("malloc failed...");
2212                 err = EMAIL_ERROR_OUT_OF_MEMORY;
2213                 goto FINISH_OFF;
2214         }
2215         
2216         (*attachment)->attachment_id = attachment_id;
2217         (*attachment)->attachment_name = attachment_tbl->attachment_name; attachment_tbl->attachment_name = NULL;
2218         (*attachment)->attachment_size = attachment_tbl->attachment_size;
2219         (*attachment)->save_status = attachment_tbl->attachment_save_status;
2220         (*attachment)->attachment_path = attachment_tbl->attachment_path; attachment_tbl->attachment_path = NULL;
2221         (*attachment)->drm_status = attachment_tbl->attachment_drm_type;
2222         (*attachment)->inline_content_status = attachment_tbl->attachment_inline_content_status;
2223
2224         ret = true;
2225
2226 FINISH_OFF: 
2227         if (attachment_tbl)
2228                 emstorage_free_attachment(&attachment_tbl, 1, NULL);
2229
2230         if (err_code)
2231                 *err_code = err;
2232         
2233         return ret;
2234 }
2235
2236 /* description
2237  *    get mail attachment
2238  * arguments  
2239  *    input_mail_id           :  mail id to own attachment
2240  *    output_attachment_data  :  result attahchment data
2241  *    output_attachment_count :  result attahchment count
2242  * return  
2243  *     succeed : EMAIL_ERROR_NONE
2244  *     fail    : error code
2245  */
2246 INTERNAL_FUNC int emcore_get_attachment_data_list(int input_mail_id, email_attachment_data_t **output_attachment_data, int *output_attachment_count)
2247 {
2248         EM_DEBUG_FUNC_BEGIN("input_mail_id[%d], output_attachment_data[%p], output_attachment_count[%p]", input_mail_id, output_attachment_data, output_attachment_count);
2249         
2250         if (input_mail_id == 0|| output_attachment_data == NULL || output_attachment_count == NULL)  {
2251                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2252                 return EMAIL_ERROR_INVALID_PARAM;
2253         }
2254
2255         int                        i = 0;
2256         int                        err = EMAIL_ERROR_NONE;
2257         int                        attachment_tbl_count = 0;
2258         emstorage_attachment_tbl_t *attachment_tbl_list = NULL;
2259         email_attachment_data_t     *temp_attachment_data = NULL;
2260         
2261         /* get attachment from attachment tbl */
2262         if ( (err = emstorage_get_attachment_list(input_mail_id, true, &attachment_tbl_list, &attachment_tbl_count)) != EMAIL_ERROR_NONE ){
2263                 EM_DEBUG_EXCEPTION("emstorage_get_attachment_list failed [%d]", err);
2264
2265                 goto FINISH_OFF;
2266         } 
2267
2268         if (attachment_tbl_count)  {
2269                 EM_DEBUG_LOG("attchment count %d", attachment_tbl_count);
2270
2271                 *output_attachment_data = em_malloc(sizeof(email_attachment_data_t) * attachment_tbl_count);
2272
2273                 if(*output_attachment_data == NULL) {
2274                         EM_DEBUG_EXCEPTION("em_malloc failed");
2275                         err = EMAIL_ERROR_OUT_OF_MEMORY;
2276                         goto FINISH_OFF;
2277                 }
2278
2279                 for (i = 0; i < attachment_tbl_count; i++)  {
2280                         temp_attachment_data = (*output_attachment_data) + i;
2281
2282                         temp_attachment_data->attachment_id         = attachment_tbl_list[i].attachment_id;
2283                         temp_attachment_data->attachment_name       = attachment_tbl_list[i].attachment_name; attachment_tbl_list[i].attachment_name = NULL;
2284                         temp_attachment_data->attachment_path       = attachment_tbl_list[i].attachment_path; attachment_tbl_list[i].attachment_path = NULL;
2285                         temp_attachment_data->attachment_size       = attachment_tbl_list[i].attachment_size;
2286                         temp_attachment_data->mail_id               = attachment_tbl_list[i].mail_id;
2287                         temp_attachment_data->account_id            = attachment_tbl_list[i].account_id;
2288                         temp_attachment_data->mailbox_id            = attachment_tbl_list[i].mailbox_id;
2289                         temp_attachment_data->save_status           = attachment_tbl_list[i].attachment_save_status;
2290                         temp_attachment_data->drm_status            = attachment_tbl_list[i].attachment_drm_type;
2291                         temp_attachment_data->inline_content_status = attachment_tbl_list[i].attachment_inline_content_status;
2292                 }
2293         }
2294         
2295 FINISH_OFF: 
2296         
2297         *output_attachment_count = attachment_tbl_count;
2298
2299         if (attachment_tbl_list)
2300                 emstorage_free_attachment(&attachment_tbl_list, attachment_tbl_count, NULL);
2301         
2302         return err;
2303 }
2304
2305
2306 INTERNAL_FUNC int emcore_download_attachment(int account_id, int mail_id, int nth, int *err_code)
2307 {
2308         EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], nth[%d], err_code[%p]", account_id, mail_id, nth, err_code);
2309
2310         int err = EMAIL_ERROR_NONE;
2311
2312         if (mail_id < 1)  {
2313                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2314                 err = EMAIL_ERROR_INVALID_PARAM;
2315
2316                 if (err_code != NULL)
2317                         *err_code = err;
2318
2319                 emcore_notify_network_event(NOTI_DOWNLOAD_ATTACH_FAIL, mail_id, 0, nth, err);
2320                 return false;
2321         }
2322
2323         int ret = false;
2324         int status = EMAIL_DOWNLOAD_FAIL;
2325         MAILSTREAM *stream = NULL;
2326         BODY *mbody = NULL;
2327         emstorage_mail_tbl_t *mail = NULL;
2328         emstorage_attachment_tbl_t *attachment = NULL;
2329         struct attachment_info *ai = NULL;
2330         struct _m_content_info *cnt_info = NULL;
2331         void *tmp_stream = NULL;
2332         char *s_uid = NULL, buf[1024];
2333         int msg_no = 0;
2334         emstorage_attachment_tbl_t *attachment_list = NULL;
2335         int current_attachment_no = 0;
2336         int attachment_count_to_be_downloaded = 0;              /*  how many attachments should be downloaded */
2337         int i = 0;
2338         int server_mbox_id = 0;
2339
2340         if (!emcore_check_thread_status())  {
2341                 err = EMAIL_ERROR_CANCELLED;
2342                 goto FINISH_OFF;
2343         }
2344         
2345         only_body_download = false;
2346         
2347         /*  get mail from mail table. */
2348         if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail)  {
2349                 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
2350
2351                 goto FINISH_OFF;
2352         }
2353
2354         if (!mail->server_mail_status)  {
2355                 EM_DEBUG_EXCEPTION("not synchronous mail...");
2356                 err = EMAIL_ERROR_INVALID_MAIL;
2357                 goto FINISH_OFF;
2358         }
2359         
2360         if (nth == 0) { /*  download all attachments, nth starts from 1, not zero */
2361                 /*  get attachment list from db */
2362                 attachment_count_to_be_downloaded = EMAIL_ATTACHMENT_MAX_COUNT;
2363                 if ( (err = emstorage_get_attachment_list(mail_id, true, &attachment_list, &attachment_count_to_be_downloaded)) != EMAIL_ERROR_NONE ){
2364                         EM_DEBUG_EXCEPTION("emstorage_get_attachment_list failed [%d]", err);
2365
2366                         goto FINISH_OFF;
2367                 }
2368         }
2369         else {  /*  download only nth attachment */
2370                 attachment_count_to_be_downloaded = 1;
2371                 if (!emstorage_get_attachment_nth(mail_id, nth, &attachment_list, true, &err) || !attachment_list)  {
2372                         EM_DEBUG_EXCEPTION("emstorage_get_attachment_nth failed [%d]", err);
2373
2374                         goto FINISH_OFF;
2375                 }
2376         }
2377         
2378         if (!emcore_check_thread_status())  {
2379                 err = EMAIL_ERROR_CANCELLED;
2380                 goto FINISH_OFF;
2381         }
2382         
2383         account_id              = mail->account_id;
2384         s_uid                   = EM_SAFE_STRDUP(mail->server_mail_id);
2385         server_mbox_id  = mail->mailbox_id;
2386
2387         /*  open mail server. */
2388         if (!emcore_connect_to_remote_mailbox(account_id, server_mbox_id, (void **)&tmp_stream, &err) || !tmp_stream)  {
2389                 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
2390                 if(err == EMAIL_ERROR_NO_SUCH_HOST)
2391                         err = EMAIL_ERROR_CONNECTION_FAILURE;
2392                 status = EMAIL_DOWNLOAD_CONNECTION_FAIL;
2393                 goto FINISH_OFF;
2394         }
2395         
2396         stream = (MAILSTREAM *)tmp_stream;
2397         
2398         for (i = 0; i < attachment_count_to_be_downloaded; i++) {
2399                 EM_DEBUG_LOG(" >>>>>> Attachment Downloading [%d / %d] start", i + 1, attachment_count_to_be_downloaded);
2400
2401                 attachment = attachment_list + i;
2402                 if (nth == 0)                                   /*  download all attachments, nth starts from 1, not zero */
2403                         current_attachment_no = i + 1;                  /*  attachment no */
2404                 else                                                                            /*  download only nth attachment */
2405                         current_attachment_no = nth;    /*  attachment no */
2406
2407                 if (!emcore_check_thread_status())  {
2408                         err = EMAIL_ERROR_CANCELLED;
2409                         goto FINISH_OFF;
2410                 }
2411                 
2412                 if (!(cnt_info = em_malloc(sizeof(struct _m_content_info))))  {
2413                         EM_DEBUG_EXCEPTION("malloc failed...");
2414                         err = EMAIL_ERROR_OUT_OF_MEMORY;
2415                         goto FINISH_OFF;
2416                 }
2417                 
2418                 cnt_info->grab_type = GRAB_TYPE_ATTACHMENT;     /*  attachment */
2419                 cnt_info->file_no   = current_attachment_no;    /*  attachment no */
2420
2421 #ifdef CHANGE_HTML_BODY_TO_ATTACHMENT
2422                 /*  text/html be changed to attachment, this isn't real attachment in RFC822. */
2423                 if (html_changed)
2424                         cnt_info->file_no--;
2425 #endif
2426
2427                 /*  set sparep(member of BODY) memory free function. */
2428                 mail_parameters(stream, SET_FREEBODYSPAREP, emcore_free_body_sharep);
2429                 
2430                 if (!emcore_check_thread_status()) {
2431                         err = EMAIL_ERROR_CANCELLED;
2432                         goto FINISH_OFF;
2433                 }
2434
2435                 msg_no = s_uid? atoi(s_uid): 0;
2436
2437                 /*  get body structure. */
2438                 /*  don't free mbody because mbody is freed in closing mail_stream. */
2439                 if ((!stream) || emcore_get_body_structure(stream, msg_no, &mbody, &err) < 0)  {
2440                         EM_DEBUG_EXCEPTION("emcore_get_body_structure failed [%d]", err);
2441                         goto FINISH_OFF;
2442                 }
2443
2444                 if (!emcore_check_thread_status())  {
2445                         err = EMAIL_ERROR_CANCELLED;
2446                         goto FINISH_OFF;
2447                 }
2448
2449                 /*  set body fetch section. */
2450                 if (emcore_set_fetch_body_section(mbody, false, NULL,  &err) < 0) {
2451                         EM_DEBUG_EXCEPTION("emcore_set_fetch_body_section failed [%d]", err);
2452                         goto FINISH_OFF;
2453                 }
2454         
2455                 /*  download attachment. */
2456                 _imap4_received_body_size = 0;
2457                 _imap4_last_notified_body_size = 0;
2458                 _imap4_total_body_size = 0;                                     /*  This will be assigned in imap_mail_write_body_to_file() */
2459                 _imap4_download_noti_interval_value = 0;        /*  This will be assigned in imap_mail_write_body_to_file() */
2460
2461                 EM_DEBUG_LOG("cnt_info->file_no[%d], current_attachment_no[%d]", cnt_info->file_no, current_attachment_no);
2462                 if (emcore_get_body(stream, account_id, mail_id, msg_no, mbody, cnt_info, &err) < 0)  {
2463                         EM_DEBUG_EXCEPTION("emcore_get_body failed [%d]", err);
2464                         goto FINISH_OFF;
2465                 }
2466
2467                 if (!emcore_check_thread_status())  {
2468                         err = EMAIL_ERROR_CANCELLED;
2469                         goto FINISH_OFF;
2470                 }
2471
2472                 /*  select target attachment information. */
2473                 for (ai = cnt_info->file ; ai; ai = ai->next) {
2474                         EM_DEBUG_LOG("[in loop] name[%s] save[%s] no[%d]", ai->save, ai->name, cnt_info->file_no);
2475                         if (--cnt_info->file_no == 0)
2476                                 break;
2477                 }
2478
2479                 EM_DEBUG_LOG("selected cnt_info->file_no = %d, ai = %p", cnt_info->file_no, ai);
2480                 
2481                 if (cnt_info->file_no == 0 && ai) {
2482                         /*  rename temporary file to real file. */
2483                         if (!emstorage_create_dir(account_id, mail_id, current_attachment_no, &err))  {
2484                                 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
2485                                 goto FINISH_OFF;
2486                         }
2487
2488                         if (!emstorage_get_save_name(account_id, mail_id, current_attachment_no, ai->name, buf, &err))  {
2489                                 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
2490                                 goto FINISH_OFF;
2491                         }
2492
2493                         if (!emstorage_move_file(ai->save, buf, false, &err))  {
2494                                 EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
2495                                 err = EMAIL_ERROR_INVALID_ATTACHMENT_SAVE_NAME;
2496                                 goto FINISH_OFF;
2497                         }
2498
2499                         EM_SAFE_FREE(ai->save);
2500
2501                         EM_DEBUG_LOG("ai->size [%d]", ai->size);
2502                         attachment->attachment_size = ai->size;
2503                         attachment->attachment_path = EM_SAFE_STRDUP(buf);
2504
2505                         /*  update attachment information. */
2506                         if (!emstorage_change_attachment_field(mail_id, UPDATE_SAVENAME, attachment, true, &err))  {
2507                                 EM_DEBUG_EXCEPTION("emstorage_change_attachment_field failed [%d]", err);
2508                                 /*  delete created file. */
2509                                 remove(buf);
2510
2511                                 goto FINISH_OFF;
2512                         }
2513
2514 #ifdef __FEATURE_DRM__
2515                         if (emcore_check_drm(attachment))  {    /*  has drm attachment ? */
2516                                 if (drm_process_request(DRM_REQUEST_TYPE_REGISTER_FILE, (void *)attachment->attachment_path, NULL) == DRM_RETURN_SUCCESS)
2517                                         EM_DEBUG_LOG("drm_svc_register_file success");
2518                                 else
2519                                         EM_DEBUG_EXCEPTION("drm_svc_register_file fail");
2520                                 mail->DRM_status = attachment->attachment_drm_type;
2521                         }
2522 #endif /* __FEATURE_DRM__ */
2523                 }
2524                 else  {
2525                         EM_DEBUG_EXCEPTION("invalid attachment sequence...");
2526                         err = EMAIL_ERROR_INVALID_ATTACHMENT;
2527                         goto FINISH_OFF;
2528                 }
2529         
2530                 if (cnt_info)  {
2531                         emcore_free_content_info(cnt_info);
2532                         cnt_info = NULL;
2533                 }
2534                 EM_DEBUG_LOG(" >>>>>> Attachment Downloading [%d / %d] completed", i+1, attachment_count_to_be_downloaded);
2535         }
2536
2537         if (stream)  {
2538                 emcore_close_mailbox(0, stream);
2539                 stream = NULL;
2540         }
2541         
2542         ret = true;
2543         
2544 FINISH_OFF:
2545         if (stream)
2546                 emcore_close_mailbox(account_id, stream);
2547         if (attachment_list)
2548                 emstorage_free_attachment(&attachment_list, attachment_count_to_be_downloaded, NULL);
2549         if (cnt_info)
2550                 emcore_free_content_info(cnt_info);
2551         if (mail)
2552                 emstorage_free_mail(&mail, 1, NULL);
2553
2554         EM_SAFE_FREE(s_uid);
2555
2556         if (ret == true)
2557                 emcore_notify_network_event(NOTI_DOWNLOAD_ATTACH_FINISH, mail_id, NULL, nth, 0);
2558         else
2559                 emcore_notify_network_event(NOTI_DOWNLOAD_ATTACH_FAIL, mail_id, NULL, nth, err);
2560
2561         if (err_code != NULL)
2562                 *err_code = err;
2563         
2564         EM_DEBUG_FUNC_END();
2565         return ret;
2566 }
2567
2568 #ifdef __ATTACHMENT_OPTI__
2569 INTERNAL_FUNC int emcore_download_attachment_bulk(int account_id, int mail_id, int nth, int *err_code)
2570 {
2571         EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], nth[%d], err_code[%p]", account_id, mail_id, nth, err_code);
2572
2573         int err = EMAIL_ERROR_NONE; /* Prevent Defect - 25093 */
2574         int ret = false;
2575         int status = EMAIL_DOWNLOAD_FAIL;
2576         MAILSTREAM *stream = NULL;
2577         emstorage_mail_tbl_t *mail = NULL;
2578         emstorage_attachment_tbl_t *attachment = NULL;
2579         void *tmp_stream = NULL;
2580         char *s_uid = NULL, *server_mbox = NULL, buf[512];
2581         emstorage_attachment_tbl_t *attachment_list = NULL;
2582         int current_attachment_no = 0;
2583         int attachment_count_to_be_downloaded = 0;              /*  how many attachments should be downloaded */
2584         int i = 0;
2585         char *savefile = NULL;
2586         int dec_len = 0;
2587         int uid = 0;
2588 #ifdef SUPPORT_EXTERNAL_MEMORY
2589         int iActualSize = 0;
2590         int is_add = 0;
2591         char dirName[512];
2592         int bIs_empty = 0;
2593         int bIs_full = 0;
2594         int bIsAdd_to_mmc = false;
2595         int is_on_mmc = false;
2596         email_file_list *pFileListMMc = NULL;
2597         email_file_list *pFileList = NULL;
2598 #endif /*  SUPPORT_EXTERNAL_MEMORY */
2599         
2600         
2601         memset(buf, 0x00, 512);
2602         /* CID FIX 31230 */
2603         if (mail_id < 1 || !nth) {
2604                 EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d], nth[%p]", account_id, mail_id, nth);
2605
2606                 err = EMAIL_ERROR_INVALID_PARAM;
2607
2608                 if (err_code != NULL)
2609                         *err_code = err;
2610
2611                 if (nth)
2612                         attachment_no = atoi(nth);
2613
2614                 emcore_notify_network_event(NOTI_DOWNLOAD_ATTACH_FAIL, mail_id, 0, attachment_no, err);         /*  090525, kwangryul.baek */
2615
2616                 return false;
2617         }
2618         
2619         if (!emcore_check_thread_status()) {
2620                 err = EMAIL_ERROR_CANCELLED;
2621                 goto FINISH_OFF;
2622         }
2623         only_body_download = false;
2624         
2625         attachment_no = atoi(nth);
2626
2627         if (attachment_no == 0) {
2628                 /*  download all attachments, nth starts from 1, not zero */
2629                 /*  get attachment list from db */
2630                 attachment_count_to_be_downloaded = EMAIL_ATTACHMENT_MAX_COUNT;
2631                 if ( (err = emstorage_get_attachment_list(mail_id, true, &attachment_list, &attachment_count_to_be_downloaded)) != EMAIL_ERROR_NONE || !attachment_list){
2632                         EM_DEBUG_EXCEPTION("emstorage_get_attachment_list failed [%d]", err);
2633         
2634                         goto FINISH_OFF;
2635                 }
2636         }
2637         else {  /*  download only nth attachment */
2638                 attachment_count_to_be_downloaded = 1;
2639                 if (!emstorage_get_attachment_nth(mail_id, attachment_no, &attachment_list, true, &err) || !attachment_list) {
2640                         EM_DEBUG_EXCEPTION("emstorage_get_attachment_nth failed [%d]", err);
2641
2642
2643                         goto FINISH_OFF;
2644                 }
2645         }
2646
2647
2648         if (!emcore_check_thread_status()) {
2649                 err = EMAIL_ERROR_CANCELLED;
2650                 goto FINISH_OFF;
2651         }
2652
2653         
2654         /*  get mail from mail table. */
2655         if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail) {
2656                 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
2657                 
2658
2659                 goto FINISH_OFF;
2660         }
2661
2662         /* if (!mail->server_mail_yn || !mail->text_download_yn) {*/ /*  faizan.h@samsung.com */
2663
2664         if (!emcore_check_thread_status()) {
2665                 err = EMAIL_ERROR_CANCELLED;
2666                 goto FINISH_OFF;
2667         }
2668
2669         account_id = mail->account_id;
2670         s_uid = EM_SAFE_STRDUP(mail->server_mail_id); mail->server_mail_id = NULL;
2671         server_mbox = EM_SAFE_STRDUP(mail->mailbox); mail->server_mailbox_name = NULL;
2672
2673
2674
2675         /*  open mail server. */
2676         if (!emcore_connect_to_remote_mailbox(account_id, server_mbox, (void **)&tmp_stream, &err) || !tmp_stream) {
2677                 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
2678
2679                 status = EMAIL_DOWNLOAD_CONNECTION_FAIL;
2680                 goto FINISH_OFF;
2681         }
2682
2683         stream = (MAILSTREAM *)tmp_stream;
2684
2685
2686         if (!emcore_check_thread_status()) {
2687                 err = EMAIL_ERROR_CANCELLED;
2688                 goto FINISH_OFF;
2689         }
2690
2691
2692         for (i = 0; i < attachment_count_to_be_downloaded; i++) {
2693                 EM_DEBUG_LOG(" >>>>>> Attachment Downloading [%d / %d] start", i+1, attachment_count_to_be_downloaded);
2694
2695                 attachment = attachment_list + i;
2696                 if (attachment_no == 0) {
2697                         /*  download all attachments, nth starts from 1, not zero */
2698                         current_attachment_no = i + 1;          /*  attachment no */
2699                 }
2700                 else {
2701                         /*  download only nth attachment */
2702                         current_attachment_no = attachment_no;          /*  attachment no */
2703                 }
2704
2705                 if (!emcore_check_thread_status()) {
2706                         err = EMAIL_ERROR_CANCELLED;
2707                         goto FINISH_OFF;
2708                 }
2709
2710                 _imap4_received_body_size = 0;
2711                 _imap4_last_notified_body_size = 0;
2712                 _imap4_total_body_size = 0;                             /*  This will be assigned in imap_mail_write_body_to_file() */
2713                 _imap4_download_noti_interval_value = 0;        /*  This will be assigned in imap_mail_write_body_to_file() */
2714
2715                 
2716                 EM_SAFE_FREE(savefile);
2717                 if (!emcore_get_temp_file_name(&savefile, &err)) {
2718                         EM_DEBUG_EXCEPTION("emcore_get_temp_file_name failed [%d]", err);
2719
2720                         if (err_code != NULL)
2721                                 *err_code = err;
2722                         goto FINISH_OFF;
2723                 }
2724
2725                 if (s_uid)
2726                         uid = atoi(s_uid);
2727
2728                 EM_DEBUG_LOG("uid [%d]", uid);
2729
2730                 if (!imap_mail_write_body_to_file(stream, account_id, mail_id, attachment_no, savefile, uid , attachment->section, attachment->encoding, &dec_len, NULL, &err)) {
2731                         EM_DEBUG_EXCEPTION("imap_mail_write_body_to_file failed [%d]", err);
2732                         if (err_code != NULL)
2733                                 *err_code = err;
2734                         goto FINISH_OFF;
2735                 }
2736
2737 #ifdef SUPPORT_EXTERNAL_MEMORY
2738                 iActualSize = emcore_get_actual_mail_size (cnt_info->text.plain , cnt_info->text.html, cnt_info->file , &err);
2739                 if (!emstorage_mail_check_free_space(iActualSize, &bIs_full, &err)) {
2740                         EM_DEBUG_EXCEPTION("emstorage_mail_check_free_space failed [%d]", err);
2741                         goto FINISH_OFF;
2742                 }
2743                 
2744                 if (bIs_full) {
2745                         /* If external memory not present, return error */
2746                         if (PS_MMC_REMOVED == emstorage_get_mmc_status()) {
2747                                 err = EMAIL_ERROR_MAIL_MEMORY_FULL;
2748                                 goto FINISH_OFF;
2749                         }
2750                         bIsAdd_to_mmc = true;
2751                 }
2752 #endif /*  SUPPORT_EXTERNAL_MEMORY */
2753
2754                 if (!emstorage_create_dir(account_id, mail_id, attachment_no, &err)) {
2755                         EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
2756                         goto FINISH_OFF;
2757                 }
2758
2759                 if (!emstorage_get_save_name(account_id, mail_id, attachment_no, attachment->name, buf, &err)) {
2760                         EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
2761                         goto FINISH_OFF;
2762                 }
2763
2764                 if (!emstorage_move_file(savefile, buf, false, &err)) {
2765                         EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
2766
2767                         goto FINISH_OFF;
2768                 }
2769
2770                 attachment->attachment = EM_SAFE_STRDUP(buf);
2771                 /*  update attachment information. */
2772                 if (!emstorage_change_attachment_field(mail_id, UPDATE_SAVENAME, attachment, true, &err)) {
2773                         EM_DEBUG_EXCEPTION("emstorage_change_attachment_field failed [%d]", err);
2774
2775                         goto FINISH_OFF;
2776                 }
2777
2778 #ifdef __FEATURE_DRM__
2779                 if (emcore_check_drm(attachment)) {
2780                         /*  is drm */
2781                         if (drm_svc_register_file(attachment->attachment) == DRM_RESULT_SUCCESS)
2782                                 EM_DEBUG_LOG("drm_svc_register_file success");
2783                         else
2784                                 EM_DEBUG_EXCEPTION("drm_svc_register_file fail");
2785                         mail->flag3 = attachment->flag2;
2786                 }
2787 #endif /* __FEATURE_DRM__ */
2788
2789                 if (!emcore_check_thread_status()) {
2790                         err = EMAIL_ERROR_CANCELLED;
2791                         goto FINISH_OFF;
2792                 }
2793
2794                 EM_DEBUG_LOG(" >>>>>> Attachment Downloading [%d / %d] completed", i+1, attachment_count_to_be_downloaded);
2795         }
2796
2797         ret = true;
2798
2799         FINISH_OFF:
2800
2801         EM_SAFE_FREE(savefile);
2802
2803         emcore_close_mailbox(0, stream);
2804         stream = NULL;
2805
2806         if (attachment_list) 
2807                 emstorage_free_attachment(&attachment_list, attachment_count_to_be_downloaded, NULL);
2808
2809         if (mail) 
2810                 emstorage_free_mail(&mail, 1, NULL);
2811
2812         if (s_uid)
2813                 free(s_uid);
2814
2815         if (server_mbox)
2816                 free(server_mbox);server_mbox = NULL;
2817
2818         if (ret == true)
2819                 emcore_notify_network_event(NOTI_DOWNLOAD_ATTACH_FINISH, mail_id, NULL, attachment_no, 0);
2820         else if (err != EMAIL_ERROR_CANCELLED)
2821                 emcore_notify_network_event(NOTI_DOWNLOAD_ATTACH_FAIL, mail_id, NULL, attachment_no, err);
2822
2823         if (err_code != NULL)
2824                 *err_code = err;
2825
2826         return ret;
2827 }
2828 #endif
2829
2830 INTERNAL_FUNC int emcore_download_body_multi_sections_bulk(void *mail_stream, int account_id, int mail_id, int verbose, int with_attach, int limited_size, int event_handle , int *err_code)
2831 {
2832         EM_DEBUG_FUNC_BEGIN("mail_stream[%p], account_id[%d], mail_id[%d], verbose[%d], with_attach[%d], event_handle [ %d ] ", mail_stream, account_id, mail_id, verbose, with_attach, event_handle);
2833
2834         int ret = false;
2835         int err = EMAIL_ERROR_NONE;
2836         int status = EMAIL_DOWNLOAD_FAIL;
2837         int pop3_body_size = 0;
2838         int pop3_downloaded_size = 0;
2839         MAILSTREAM *stream = NULL;
2840         BODY *mbody = NULL;
2841         PARTLIST *section_list = NULL;
2842         email_internal_mailbox_t mailbox = { 0 };
2843         emstorage_mail_tbl_t *mail = NULL;
2844         emstorage_attachment_tbl_t attachment = {0, 0, NULL, };
2845         email_account_t *ref_account = NULL;
2846         struct attachment_info *ai = NULL;
2847         struct _m_content_info *cnt_info = NULL;
2848         void *tmp_stream = NULL;
2849         char *s_uid = NULL, buf[512];
2850         int msgno = 0, attachment_num = 1, local_attachment_count = 0, local_inline_content_count = 0;
2851         int iActualSize = 0;
2852         char html_body[MAX_PATH] = {0, };
2853         emcore_uid_list *uid_list = NULL;
2854         char *mailbox_name = NULL;
2855 #ifdef CHANGE_HTML_BODY_TO_ATTACHMENT
2856         int html_changed = 0;
2857 #endif
2858
2859         if (mail_id < 1)  {
2860                 EM_DEBUG_EXCEPTION("mail_stream[%p], account_id[%d], mail_id[%d], verbose[%d], with_attach[%d]", mail_stream, account_id, mail_id, verbose, with_attach);
2861                 err = EMAIL_ERROR_INVALID_PARAM;
2862                 
2863                 if (err_code != NULL)
2864                         *err_code = err;
2865
2866                 emcore_notify_network_event(NOTI_DOWNLOAD_BODY_FAIL, mail_id, NULL, event_handle, err);
2867                 return false;
2868         }
2869         
2870         FINISH_OFF_IF_CANCELED;
2871
2872         only_body_download = true;
2873
2874         if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail)  {
2875                 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
2876
2877                 goto FINISH_OFF;
2878         }
2879         
2880         if (mail->mailbox_name)
2881                 mailbox_name = EM_SAFE_STRDUP(mail->mailbox_name);
2882         
2883         if (1 == mail->body_download_status)  {
2884                 EM_DEBUG_EXCEPTION("not synchronous mail...");
2885                 err = EMAIL_ERROR_INVALID_MAIL;
2886                 goto FINISH_OFF;
2887         }
2888         
2889         s_uid                             = EM_SAFE_STRDUP(mail->server_mail_id);
2890
2891         attachment.account_id             = mail->account_id;
2892         attachment.mail_id                = mail->mail_id;
2893         attachment.mailbox_id             = mail->mailbox_id;
2894         attachment.attachment_save_status = 0;
2895         
2896         if (!(ref_account = emcore_get_account_reference(account_id)))   {
2897                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
2898                 err = EMAIL_ERROR_INVALID_ACCOUNT;
2899                 goto FINISH_OFF;
2900         }
2901
2902         FINISH_OFF_IF_CANCELED;
2903
2904         /*  open mail server. */
2905         if (!mail_stream)  {
2906                 if (!emcore_connect_to_remote_mailbox(account_id, mail->mailbox_id, (void **)&tmp_stream, &err) || !tmp_stream)  {
2907                         EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
2908                         status = EMAIL_DOWNLOAD_CONNECTION_FAIL;
2909                         goto FINISH_OFF;
2910                 }
2911                 stream = (MAILSTREAM *)tmp_stream;
2912         }
2913         else
2914                 stream = (MAILSTREAM *)mail_stream;
2915         
2916         FINISH_OFF_IF_CANCELED;
2917         
2918         if (!(cnt_info = em_malloc(sizeof(struct _m_content_info))))  {
2919                 EM_DEBUG_EXCEPTION("em_malloc failed...");
2920                 err = EMAIL_ERROR_OUT_OF_MEMORY;
2921                 goto FINISH_OFF;
2922         }
2923
2924         if (ref_account->incoming_server_type == EMAIL_SERVER_TYPE_POP3)  {
2925                 /*  POP3 */
2926                 /*  in POP3 case, both text and attachment are downloaded in this call. */
2927                 cnt_info->grab_type = GRAB_TYPE_TEXT | GRAB_TYPE_ATTACHMENT;
2928                 attachment.attachment_save_status = 1;          /*  all attachments should be downloaded in the case of POP3 */
2929
2930                 mailbox.account_id = account_id;
2931                 mailbox.mail_stream = stream;
2932
2933                 /*  download all uids from server. */
2934                 if (!emcore_download_uid_all(&mailbox, &uid_list, NULL, NULL, 0, EM_CORE_GET_UIDS_FOR_NO_DELETE, &err))  {
2935                         EM_DEBUG_EXCEPTION("emcore_download_uid_all failed [%d]", err);
2936                         goto FINISH_OFF;
2937                 }
2938
2939                 /*  get mesg number to be related to last download mail from uid list file */
2940                 if (!emcore_get_msgno(uid_list, s_uid, &msgno, &err))  {
2941                         EM_DEBUG_EXCEPTION("emcore_get_msgno failed [%d]", err);
2942                         err = EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER;
2943                         goto FINISH_OFF;
2944                 }
2945
2946                 if (!emcore_check_thread_status())  {
2947                         err = EMAIL_ERROR_CANCELLED;
2948                         goto FINISH_OFF;
2949                 }
2950
2951                 _pop3_received_body_size = 0;
2952                 _pop3_total_body_size = 0;
2953                 _pop3_last_notified_body_size = 0;
2954                 _pop3_receiving_mail_id = mail_id;
2955                 
2956                 /*  send read mail commnad. */
2957                 if (!emcore_mail_cmd_read_mail_pop3(stream, msgno, limited_size, &pop3_downloaded_size, &pop3_body_size, &err)) {
2958                         EM_DEBUG_EXCEPTION("emcore_mail_cmd_read_mail_pop3 failed [%d]", err);
2959                         goto FINISH_OFF;
2960                 }
2961
2962                 _pop3_total_body_size = pop3_body_size;
2963
2964                 if (!emcore_notify_network_event(NOTI_DOWNLOAD_BODY_START, mail_id, "dummy-file", _pop3_total_body_size, 0))
2965                         EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_DOWNLOAD_BODY_START] failed >>>> ");
2966                 else
2967                         EM_DEBUG_LOG("NOTI_DOWNLOAD_BODY_START notified (%d / %d)", 0, _pop3_total_body_size);
2968
2969                 FINISH_OFF_IF_CANCELED;
2970                 
2971                 /*  save message into tempfile */
2972                 /*  parsing mime from stream. */
2973
2974                 if (!emcore_parse_mime(stream, 0, cnt_info,  &err))  {
2975                         EM_DEBUG_EXCEPTION("emcore_parse_mime failed [%d]", err);
2976                         goto FINISH_OFF;
2977                 }
2978                 
2979                 FINISH_OFF_IF_CANCELED;
2980         }
2981         else  { /*  in IMAP case, both text and attachment list are downloaded in this call. */
2982                 /*  This flag is just for downloading mailbox.(sync header), don't be used when retrieve body. */
2983                 if (with_attach > 0)
2984                         cnt_info->grab_type = GRAB_TYPE_TEXT | GRAB_TYPE_ATTACHMENT;
2985                 else
2986                         cnt_info->grab_type = GRAB_TYPE_TEXT;
2987
2988                 int uid = s_uid? atoi(s_uid):0; /*prevent 39118*/
2989
2990                 /*  set sparep(member of BODY) memory free function  */
2991                 mail_parameters(stream, SET_FREEBODYSPAREP, emcore_free_body_sharep);
2992
2993                 /*  get body strucutre. */
2994                 /*  don't free mbody because mbody is freed in closing mail_stream. */
2995                 if (emcore_get_body_structure(stream, uid, &mbody, &err) < 0 || (mbody == NULL))  {
2996                         EM_DEBUG_EXCEPTION("emcore_get_body_structure failed [%d]", err);
2997                         err = EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER;
2998                         goto FINISH_OFF;
2999                 }
3000
3001                 FINISH_OFF_IF_CANCELED;
3002
3003                 if (mbody->type == TYPEMULTIPART) {
3004                         EM_DEBUG_LOG(">>> check multipart body size to download : only_body_download[%d]", only_body_download);
3005                         PART *part_child = mbody->nested.part;
3006                         int counter = 0;
3007
3008                         char filename[MAX_PATH+1] = {0, };
3009                         int is_attachment = 0;
3010                         while (part_child)  {
3011                                 BODY *body = &(part_child->body);
3012                                 if (only_body_download == true) {
3013                                         if (((body->id) && EM_SAFE_STRLEN(body->id) > 1) || (body->location))
3014                                                 is_attachment = 0;
3015                                         else if (body->disposition.type)  {     /*  "attachment" or "inline" or etc... */
3016                                                 PARAMETER *param = body->disposition.parameter;
3017
3018                                                 while (param)  {
3019                                                         EM_DEBUG_LOG("param->attribute [%s], param->value [%s]", param->attribute, param->value);
3020
3021                                                         if (!strcasecmp(param->attribute, "filename"))  {       /* attribute is "filename" */
3022                                                                 strncpy(filename, param->value, MAX_PATH);
3023                                                                 EM_DEBUG_LOG(">>>>> FILENAME [%s] ", filename);
3024                                                                 break;
3025                                                         }
3026                                                         param = param->next;
3027                                                 }
3028
3029                                                 is_attachment = 1;
3030
3031                                                 if (!*filename)  {
3032                                                         /*  it may be report msg */
3033                                                         if (body->disposition.type[0] == 'i' || body->disposition.type[0] == 'I')
3034                                                                 is_attachment = 0;
3035                                                 }
3036                                         }
3037
3038                                         if (is_attachment == 0) {
3039                                                 EM_DEBUG_LOG("%d :  body->size.bytes[%ld]", counter+1, body->size.bytes);
3040                                                 multi_part_body_size = multi_part_body_size + body->size.bytes;
3041                                         }
3042                                 }
3043                                 else {
3044                                         /*  download all */
3045                                         EM_DEBUG_LOG("%d :  body->size.bytes[%ld]", counter+1, body->size.bytes);
3046                                         multi_part_body_size = multi_part_body_size + body->size.bytes;
3047                                 }
3048                                 part_child = part_child->next;
3049                                 counter++;
3050                         }
3051                 }
3052
3053                 /*  set body fetch section. */
3054                 if (emcore_set_fetch_body_section(mbody, true, &iActualSize, &err) < 0) {
3055                         EM_DEBUG_EXCEPTION("emcore_set_fetch_body_section failed [%d]", err);
3056                         goto FINISH_OFF;
3057                 }
3058
3059                 EM_DEBUG_LOG("iActualSize [%d]", iActualSize);
3060                 multi_part_body_size = iActualSize;
3061
3062                 _imap4_received_body_size = 0;
3063                 _imap4_last_notified_body_size = 0;
3064                 if (multi_part_body_size > 0) { /*  download multiparts */
3065                         _imap4_total_body_size = multi_part_body_size;
3066                         _imap4_download_noti_interval_value = DOWNLOAD_NOTI_INTERVAL_PERCENT * multi_part_body_size / 100;
3067                 }
3068                 else {  /*  download only one body part */
3069                         _imap4_total_body_size = 0;                                     /*  This will be assigned in imap_mail_write_body_to_file() */
3070                         _imap4_download_noti_interval_value = 0;        /*  This will be assigned in imap_mail_write_body_to_file() */
3071                 }
3072
3073                 /*  save message into tempfile */
3074                 /*  download body text and get attachment list. */
3075                 if (emcore_get_body_part_list_full(stream, uid, account_id, mail_id, mbody, cnt_info, &err, section_list, event_handle) < 0)  {
3076                         EM_DEBUG_EXCEPTION("emcore_get_body_part_list_full failed [%d]", err);
3077                         goto FINISH_OFF;
3078                 }
3079                 FINISH_OFF_IF_CANCELED;
3080         }
3081
3082         if (cnt_info->text.plain)  {
3083                 EM_DEBUG_LOG("cnt_info->text.plain [%s]", cnt_info->text.plain);
3084
3085                 if (!emstorage_create_dir(account_id, mail_id, 0, &err))  {
3086                         EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
3087                         goto FINISH_OFF;
3088                 }
3089
3090
3091                 if (!emstorage_get_save_name(account_id, mail_id, 0, cnt_info->text.plain_charset ? cnt_info->text.plain_charset  :  "UTF-8", buf, &err))  {
3092                         EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
3093                         goto FINISH_OFF;
3094                 }
3095
3096                 if (!emstorage_move_file(cnt_info->text.plain, buf, false, &err))  {
3097                         EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
3098                         goto FINISH_OFF;
3099                 }
3100
3101                 mail->file_path_plain = EM_SAFE_STRDUP(buf);
3102                 EM_DEBUG_LOG("mail->file_path_plain [%s]", mail->file_path_plain);
3103         }
3104         
3105         if (cnt_info->text.html)  {
3106                 if (!emstorage_create_dir(account_id, mail_id, 0, &err))  {
3107                         EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
3108                         goto FINISH_OFF;
3109                 }
3110                 
3111                 if (cnt_info->text.plain_charset != NULL) {
3112                         memcpy(html_body, cnt_info->text.plain_charset, EM_SAFE_STRLEN(cnt_info->text.plain_charset));
3113                         strcat(html_body, HTML_EXTENSION_STRING);
3114                 }
3115                 else {
3116                         memcpy(html_body, "UTF-8.htm", strlen("UTF-8.htm"));
3117                 }
3118                 if (!emstorage_get_save_name(account_id, mail_id, 0, html_body, buf, &err))  {
3119                         EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
3120                         goto FINISH_OFF;
3121                 }
3122
3123                 if (!emstorage_move_file(cnt_info->text.html, buf, false, &err))  {
3124                         EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
3125                         goto FINISH_OFF;
3126                 }
3127                 mail->file_path_html = EM_SAFE_STRDUP(buf);
3128         }
3129         
3130         if (ref_account->incoming_server_type == EMAIL_SERVER_TYPE_POP3 && limited_size != NO_LIMITATION && limited_size < pop3_body_size)
3131                 mail->body_download_status = EMAIL_BODY_DOWNLOAD_STATUS_PARTIALLY_DOWNLOADED;
3132         else
3133                 mail->body_download_status = EMAIL_BODY_DOWNLOAD_STATUS_FULLY_DOWNLOADED;
3134
3135         /* Update local_preview_text */
3136         if ((err = emcore_get_preview_text_from_file(mail->file_path_plain, mail->file_path_html, MAX_PREVIEW_TEXT_LENGTH, &(mail->preview_text))) != EMAIL_ERROR_NONE) {
3137                 EM_DEBUG_EXCEPTION("emcore_get_preview_text_from_file failedi : [%d]", err);
3138         }
3139         
3140 #ifdef CHANGE_HTML_BODY_TO_ATTACHMENT
3141         if (html_changed) mail->flag2 = 1;
3142 #endif
3143         
3144         FINISH_OFF_IF_CANCELED;
3145         
3146         for (ai = cnt_info->file; ai; ai = ai->next, attachment_num++)  {
3147                 attachment.attachment_id                    = attachment_num;
3148                 attachment.attachment_size                  = ai->size;
3149                 attachment.attachment_path                  = ai->save;
3150                 attachment.attachment_name                  = ai->name;
3151                 attachment.attachment_drm_type              = ai->drm;
3152                 attachment.attachment_inline_content_status = ai->type == 1;
3153                 attachment.attachment_save_status           = 0;
3154                 attachment.attachment_mime_type             = ai->attachment_mime_type;
3155 #ifdef __ATTACHMENT_OPTI__
3156                 attachment.encoding                         = ai->encoding;
3157                 attachment.section                          = ai->section;
3158 #endif
3159                 EM_DEBUG_LOG("attachment.attachment_id[%d]", attachment.attachment_id);
3160                 EM_DEBUG_LOG("attachment.attachment_size[%d]", attachment.attachment_size);
3161                 EM_DEBUG_LOG("attachment.attachment_path[%s]", attachment.attachment_path);
3162                 EM_DEBUG_LOG("attachment.attachment_name[%s]", attachment.attachment_name);
3163                 EM_DEBUG_LOG("attachment.attachment_drm_type[%d]", attachment.attachment_drm_type);
3164                 EM_DEBUG_LOG("attachment.attachment_inline_content_status[%d]", attachment.attachment_inline_content_status);
3165
3166                 if (ai->type == 1)
3167                         local_inline_content_count++;
3168                 local_attachment_count++;
3169
3170                 if (ai->save)  {
3171                         /*  in POP3 case, rename temporary file to real file. */
3172                         attachment.attachment_save_status = 1;
3173                         if (ai->type == 1)  {           /*  it is inline content */
3174                                 if (!emstorage_create_dir(account_id, mail_id, 0, &err))  {
3175                                         EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
3176                                         goto FINISH_OFF;
3177                                 }
3178                                 if (!emstorage_get_save_name(account_id, mail_id, 0, attachment.attachment_name, buf, &err))  {
3179                                         EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
3180                                         goto FINISH_OFF;
3181                                 }
3182                         }
3183                         else  {
3184                                 if (!emstorage_create_dir(account_id, mail_id, attachment_num, &err)) {
3185                                         EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
3186                                         goto FINISH_OFF;
3187                                 }
3188
3189                                 if (!emstorage_get_save_name(account_id, mail_id, attachment_num, attachment.attachment_name, buf, &err))  {
3190                                         EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
3191                                         goto FINISH_OFF;
3192                                 }
3193                         }
3194
3195                         if (!emstorage_move_file(ai->save, buf, false, &err))  {
3196                                 EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
3197
3198                                 /*  delete all created files. */
3199                                 if (!emstorage_get_save_name(account_id, mail_id, 0, NULL, buf, NULL)) {
3200                                         EM_DEBUG_EXCEPTION("emstorage_get_save_name failed...");
3201                                         /* goto FINISH_OFF; */
3202                                 }
3203
3204                                 if (!emstorage_delete_dir(buf, NULL)) {
3205                                         EM_DEBUG_EXCEPTION("emstorage_delete_dir failed...");
3206                                         /* goto FINISH_OFF; */
3207                                 }
3208
3209
3210                                 goto FINISH_OFF;
3211                         }
3212
3213                         free(ai->save); ai->save = EM_SAFE_STRDUP(buf);
3214
3215                         attachment.attachment_path = ai->save;
3216
3217 #ifdef __FEATURE_DRM__
3218                         if (emcore_check_drm(&attachment)) /*  is drm content ?*/ {
3219                                 if (drm_process_request(DRM_REQUEST_TYPE_REGISTER_FILE, attachment.attachment_path, NULL) != DRM_RETURN_SUCCESS)
3220                                         EM_DEBUG_EXCEPTION("drm_process_request : register file fail");
3221                                 mail->DRM_status = attachment.attachment_drm_type;
3222                         }
3223 #endif/*  __FEATURE_DRM__ */
3224                 }
3225
3226 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
3227                 /*  Information :  Attachment info already saved if partial body is dowloaded. */
3228                 if (ai->type)  {
3229                         emstorage_attachment_tbl_t *attch_info = NULL;
3230                         /*  Get attachment details  */
3231                         if (!emstorage_get_attachment_nth(mail_id, attachment.attachment_id, &attch_info, true, &err) || !attch_info)  {
3232                                 EM_DEBUG_EXCEPTION("emstorage_get_attachment_nth failed [%d]", err);
3233                                 if (err == EMAIL_ERROR_ATTACHMENT_NOT_FOUND) {   /*  save only attachment file. */
3234                                         if (!emstorage_add_attachment(&attachment, 0, false, &err)) {
3235                                                 EM_DEBUG_EXCEPTION("emstorage_add_attachment failed [%d]", err);
3236                                                 if (attch_info)
3237                                                         emstorage_free_attachment(&attch_info, 1, NULL);
3238                                                 /*  delete all created files. */
3239                                                 if (!emstorage_get_save_name(account_id, mail_id, 0, NULL, buf, &err))  {
3240                                                         EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
3241                                                         goto FINISH_OFF;
3242                                                 }
3243
3244                                                 if (!emstorage_delete_dir(buf, &err))  {
3245                                                         EM_DEBUG_EXCEPTION("emstorage_delete_dir failed [%d]", err);
3246                                                         goto FINISH_OFF;
3247                                                 }
3248
3249                                                 /*  ROLLBACK TRANSACTION; */
3250                                                 emstorage_rollback_transaction(NULL, NULL, NULL);
3251
3252
3253                                                 goto FINISH_OFF;
3254                                         }
3255                                 }
3256                         }
3257                         else {
3258                                 EM_DEBUG_LOG("Attachment info already exists...!");
3259                                 /* Update attachment size */
3260                                 EM_DEBUG_LOG("attachment_size [%d], ai->size [%d]", attch_info->attachment_size, ai->size);
3261                                 attch_info->attachment_size = ai->size;
3262                                 if (!emstorage_update_attachment(attch_info, true, &err)) {
3263                                         EM_DEBUG_EXCEPTION("emstorage_update_attachment failed [%d]", err);
3264
3265                                                 goto FINISH_OFF;
3266                                 }
3267                         }
3268
3269                         if (attch_info)
3270                                 emstorage_free_attachment(&attch_info, 1, NULL);
3271                 }
3272
3273 #else
3274                 
3275                 if (ai->type)  {
3276                         mail->attachment_yn = 1;
3277                         /*  save only attachment file. */
3278                         if (!emstorage_add_attachment(&attachment, 0, false, &err))  {
3279                                 EM_DEBUG_EXCEPTION("emstorage_add_attachment failed [%d]", err);
3280                                 if (bIsAdd_to_mmc)  {
3281                                         if (attachment.attachment) {
3282                                         }
3283                                 }
3284                                 else  {
3285                                         /*  delete all created files. */
3286                                         if (!emstorage_get_save_name(account_id, mail_id, 0, NULL, buf, &err))  {
3287                                                 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
3288                                                 goto FINISH_OFF;
3289                                         }
3290
3291                                         if (!emstorage_delete_dir(buf, &err))  {
3292                                                 EM_DEBUG_EXCEPTION("emstorage_delete_dir failed [%d]", err);
3293                                                 goto FINISH_OFF;
3294                                         }
3295
3296                                         /*  ROLLBACK TRANSACTION; */
3297                                         emstorage_rollback_transaction(NULL, NULL, NULL);
3298                                         goto FINISH_OFF;
3299                                 }
3300                         }
3301             }
3302 #endif  /*  End of #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
3303
3304         }
3305
3306         EM_DEBUG_LOG("Check #1");
3307
3308         mail->attachment_count = local_attachment_count;
3309         mail->inline_content_count = local_inline_content_count;
3310
3311         EM_DEBUG_LOG("Check #2");
3312
3313         EM_DEBUG_LOG("Mailbox Name [%s]", mailbox_name);
3314         mail->mailbox_name = EM_SAFE_STRDUP(mailbox_name);       /*  fix for mailboox sync fail */
3315
3316         EM_DEBUG_LOG("Check #3");
3317
3318         /*  change mail's information. */
3319         if (!emstorage_change_mail_field(mail_id, APPEND_BODY, mail, false, &err))  {
3320                 EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed [%d]", err);
3321                 emstorage_rollback_transaction(NULL, NULL, NULL); /*  ROLLBACK TRANSACTION; */
3322
3323                 goto FINISH_OFF;
3324         }
3325
3326         EM_DEBUG_LOG("cnt_info->text.plain [%s], cnt_info->text.html [%s]", cnt_info->text.plain, cnt_info->text.html);
3327         
3328         /*  in pop3 mail case, the mail is deleted from server after being downloaded. */
3329         if (ref_account->incoming_server_type == EMAIL_SERVER_TYPE_POP3)  {
3330 #ifdef DELETE_AFTER_DOWNLOADING
3331                 char delmsg[24];
3332
3333                 SNPRINTF(delmsg, sizeof(delmsg), "%d", msg_no);
3334
3335                 if (!ref_account->keep_mails_on_pop_server_after_download)  {
3336                         if (!emcore_delete_mails_from_pop3_server(&mbox, delmsg, &err))
3337                                 EM_DEBUG_EXCEPTION("emcore_delete_mails_from_pop3_server failed [%d]", err);
3338                 }
3339 #endif
3340                 
3341                 if (!mail_stream)  {
3342                         if (stream != NULL)  {
3343                                 emcore_close_mailbox(0, stream);
3344                                 stream = NULL;
3345                         }
3346                 }
3347         }
3348
3349         FINISH_OFF_IF_CANCELED;
3350         
3351         ret = true;
3352
3353 FINISH_OFF:
3354
3355         if (g_inline_count) {
3356                 g_inline_count = 0;
3357                 EM_SAFE_FREE(g_inline_list);
3358         }
3359
3360         if (ref_account) {
3361                 emcore_free_account(ref_account);
3362                 EM_SAFE_FREE(ref_account);
3363         }
3364
3365         multi_part_body_size = 0;
3366         _pop3_received_body_size = 0;
3367         _pop3_last_notified_body_size = 0;
3368         _pop3_total_body_size = 0;
3369         _pop3_receiving_mail_id = 0;
3370         
3371         _imap4_received_body_size = 0;
3372         _imap4_last_notified_body_size = 0;
3373         _imap4_total_body_size = 0;
3374         _imap4_download_noti_interval_value = 0;
3375         
3376         if (cnt_info)
3377                 emcore_free_content_info(cnt_info);
3378         if (mail)
3379                 emstorage_free_mail(&mail, 1, NULL);
3380         EM_SAFE_FREE(s_uid);
3381         EM_SAFE_FREE(mailbox_name);
3382
3383         multi_part_body_size = 0;
3384         
3385         if (ret == true)
3386                 emcore_notify_network_event(NOTI_DOWNLOAD_BODY_FINISH, mail_id, NULL, event_handle, 0);
3387         else
3388                 emcore_notify_network_event(NOTI_DOWNLOAD_BODY_FAIL, mail_id, NULL, event_handle, err);
3389
3390         if (err_code != NULL)
3391                 *err_code = err;
3392         
3393         return ret;
3394 }
3395
3396
3397
3398 void emcore_mail_copyuid(MAILSTREAM *stream, char *mailbox,
3399                            unsigned long uidvalidity, SEARCHSET *sourceset,
3400                            SEARCHSET *destset)
3401 {
3402         EM_DEBUG_FUNC_BEGIN();
3403         char  old_server_uid[129];
3404
3405         EM_DEBUG_LOG("mailbox name - %s", mailbox);
3406         EM_DEBUG_LOG("first sequence number source- %ld", sourceset->first);
3407         EM_DEBUG_LOG("last sequence number last- %ld", sourceset->last);
3408         EM_DEBUG_LOG("first sequence number dest - %ld", destset->first);
3409         EM_DEBUG_LOG("last sequence number dest- %ld", sourceset->last);
3410
3411         /* search for server _mail_id with value sourceset->first and update it with destset->first */
3412         /* faizan.h@samsung.com */
3413         memset(old_server_uid, 0x00, 129);
3414         sprintf(old_server_uid, "%ld", sourceset->first);
3415         EM_DEBUG_LOG(">>>>> old_server_uid = %s", old_server_uid);
3416
3417         memset(g_new_server_uid, 0x00, 129);
3418         sprintf(g_new_server_uid, "%ld", destset->first);
3419         EM_DEBUG_LOG(">>>>> new_server_uid =%s", g_new_server_uid);
3420
3421         if (!emstorage_update_server_uid(old_server_uid, g_new_server_uid, NULL)) {
3422                 EM_DEBUG_EXCEPTION("emstorage_update_server_uid falied...");
3423         }
3424 }
3425
3426 static int emcore_delete_mails_from_remote_server(int input_account_id, int input_mail_ids[], int input_mail_id_count, int input_delete_option)
3427 {
3428         EM_DEBUG_FUNC_BEGIN("input_account_id[%d], input_mail_ids[%p], input_mail_id_count[%d], input_delete_option [%d]", input_account_id, input_mail_ids, input_mail_id_count, input_delete_option);
3429
3430         int err = EMAIL_ERROR_NONE;
3431         email_account_t *account = NULL;
3432 #ifdef  __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
3433         int bulk_flag = false;
3434 #endif
3435
3436         if (!(account = emcore_get_account_reference(input_account_id)))  {
3437                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", input_account_id);
3438                 err = EMAIL_ERROR_INVALID_ACCOUNT;
3439                 goto FINISH_OFF;
3440         }
3441
3442         if (!emnetwork_check_network_status(&err)) {
3443                 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
3444                 goto FINISH_OFF;
3445         }
3446
3447         FINISH_OFF_IF_CANCELED;
3448
3449         if (account->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) {
3450                 if (!bulk_flag && !emcore_delete_mails_from_imap4_server(input_mail_ids, input_mail_id_count, input_delete_option, &err)) {
3451                         EM_DEBUG_EXCEPTION("emcore_delete_mails_from_imap4_server failed [%d]", err);
3452                         if (err == EMAIL_ERROR_IMAP4_STORE_FAILURE)
3453                                 err = EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER;
3454                         goto FINISH_OFF;
3455                 }
3456                 else
3457                         bulk_flag = true;
3458         }
3459         else if (account->incoming_server_type == EMAIL_SERVER_TYPE_POP3) {
3460                 if (!emcore_delete_mails_from_pop3_server(account, input_mail_ids, input_mail_id_count))  {
3461                         EM_DEBUG_EXCEPTION("emcore_delete_mails_from_pop3_server falied [%d]", err);
3462                         goto FINISH_OFF;
3463                 }
3464 #ifdef __FEATURE_LOCAL_ACTIVITY__
3465                 else {
3466                         /* Remove local activity */
3467                         emstorage_activity_tbl_t new_activity;
3468                         memset(&new_activity, 0x00, sizeof(emstorage_activity_tbl_t));
3469                         if (from_server == EMAIL_DELETE_FOR_SEND_THREAD) {
3470                                 new_activity.activity_type = ACTIVITY_DELETEMAIL_SEND;
3471                                 EM_DEBUG_LOG("from_server == EMAIL_DELETE_FOR_SEND_THREAD ");
3472                         }
3473                         else {
3474                                 new_activity.activity_type      = ACTIVITY_DELETEMAIL;
3475                         }
3476
3477                         new_activity.mail_id            = mail->mail_id;
3478                         new_activity.server_mailid      = mail->server_mail_id;
3479                         new_activity.src_mbox           = NULL;
3480                         new_activity.dest_mbox          = NULL;
3481
3482                         if (!emcore_delete_activity(&new_activity, &err)) {
3483                                 EM_DEBUG_EXCEPTION(" emcore_delete_activity  failed  - %d ", err);
3484                         }
3485
3486                         /* Fix for issue - Sometimes mail move and immediately followed by mail delete is not reflected on server */
3487                         if (!emstorage_remove_downloaded_mail(input_account_id, mail->server_mailbox_name, mail->server_mail_id, true, &err))  {
3488                                 EM_DEBUG_LOG("emstorage_remove_downloaded_mail falied [%d]", err);
3489                         }
3490                 }
3491
3492 #endif  /*  __FEATURE_LOCAL_ACTIVITY__ */
3493         }
3494
3495 FINISH_OFF:
3496
3497         if (account) {
3498                 emcore_free_account(account);
3499                 EM_SAFE_FREE(account);
3500         }
3501
3502         EM_DEBUG_FUNC_END("err [%d]", err);
3503         return err;
3504 }
3505
3506 int emcore_delete_mail(int account_id, int mail_ids[], int num, int from_server, int noti_param_1, int noti_param_2, int *err_code)
3507 {
3508         EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_ids[%p], num[%d], from_server[%d], noti_param_1 [%d], noti_param_2 [%d], err_code[%p]", account_id, mail_ids, num, from_server, noti_param_1, noti_param_2, err_code);
3509
3510         int ret = false;
3511         int err = EMAIL_ERROR_NONE;
3512         email_account_t *account = NULL;
3513
3514         if (!account_id || !mail_ids || !num)  {
3515                 EM_DEBUG_EXCEPTION("account_id[%d], mail_ids[%p], num[%d], from_server[%d]", account_id, mail_ids, num, from_server);
3516                 err = EMAIL_ERROR_INVALID_PARAM;
3517                 goto FINISH_OFF;
3518         }
3519
3520         if (!(account = emcore_get_account_reference(account_id)))  {
3521                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
3522                 err = EMAIL_ERROR_INVALID_ACCOUNT;
3523                 goto FINISH_OFF;
3524         }
3525
3526         FINISH_OFF_IF_CANCELED;
3527
3528         if (from_server == EMAIL_DELETE_LOCALLY) /* Delete mails from local storage*/ {
3529                 emcore_delete_mails_from_local_storage(account_id, mail_ids, num, noti_param_1, noti_param_2, err_code);
3530                 emcore_display_unread_in_badge();
3531         }
3532         else {   /* Delete mails from server*/
3533                 emcore_delete_mails_from_remote_server(account_id, mail_ids, num, from_server);
3534         }
3535
3536
3537         ret = true;
3538
3539 FINISH_OFF: 
3540         if (from_server)
3541                 emcore_show_user_message(account_id, EMAIL_ACTION_DELETE_MAIL, ret == true ? 0  :  err);
3542
3543         if (account) {
3544                 emcore_free_account(account);
3545                 EM_SAFE_FREE(account);
3546         }
3547
3548         if (err_code != NULL)
3549                 *err_code = err;
3550
3551         EM_DEBUG_FUNC_END("err [%d]", err);
3552
3553         return ret;
3554 }
3555
3556 int emcore_delete_all_mails_of_acount(int input_account_id)
3557 {
3558         EM_DEBUG_FUNC_BEGIN("input_account_id [%d]");
3559
3560         int   err = EMAIL_ERROR_NONE;
3561         char  buf[512] = { 0, };
3562
3563         /* emstorage_delete_mail_by_account is available only locally */
3564         if (!emstorage_delete_mail_by_account(input_account_id, false, &err))  {
3565                 EM_DEBUG_EXCEPTION("emstorage_delete_mail_by_account failed [%d]", err);
3566                 goto FINISH_OFF;
3567         }
3568
3569         if (!emstorage_delete_attachment_all_on_db(input_account_id, NULL, false, &err))  {
3570                 EM_DEBUG_EXCEPTION("emstorage_delete_attachment_all_on_db failed [%d]", err);
3571                 goto FINISH_OFF;
3572         }
3573
3574         /*  delete mail contents from filesystem */
3575         if (!emstorage_get_save_name(input_account_id, 0, 0, NULL, buf, &err))  {
3576                 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
3577                 goto FINISH_OFF;
3578         }
3579
3580         if (!emstorage_delete_dir(buf, &err))  {
3581                 EM_DEBUG_EXCEPTION("emstorage_delete_dir failed [%d]", err);
3582         }
3583
3584         /*  delete meeting request */
3585         if (!emstorage_delete_meeting_request(input_account_id, 0, 0, false, &err))  {
3586                 EM_DEBUG_EXCEPTION("emstorage_delete_attachment_all_on_db failed [%d]", err);
3587                 goto FINISH_OFF;
3588         }
3589
3590 FINISH_OFF:
3591         EM_DEBUG_FUNC_END("err [%d]",err);
3592         return err;
3593 }
3594
3595 INTERNAL_FUNC int emcore_delete_all_mails_of_mailbox(int input_account_id, int input_mailbox_id, int input_from_server, int *err_code)
3596 {
3597         EM_DEBUG_FUNC_BEGIN("input_account_id[%d] input_mailbox_id[%d] input_from_server[%d] err_code[%p]", input_account_id, input_mailbox_id, input_from_server, err_code);
3598
3599         int   ret = false;
3600         int   err = EMAIL_ERROR_NONE;
3601         int  *mail_id_array = NULL;
3602         int   mail_id_count = 0;
3603         char  conditional_clause[QUERY_SIZE] = { 0, };
3604
3605         if (!input_mailbox_id) {
3606                 err = EMAIL_ERROR_INVALID_PARAM;
3607                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3608                 goto FINISH_OFF;
3609         }
3610
3611         /* Delete all mails in specific mailbox */
3612
3613         SNPRINTF(conditional_clause, QUERY_SIZE, " where mailbox_id = %d ", input_mailbox_id);
3614
3615         emstorage_query_mail_id_list(conditional_clause, false, &mail_id_array, &mail_id_count);
3616
3617         EM_DEBUG_LOG("emstorage_query_mail_id_list returns [%d]", mail_id_count);
3618
3619         if (mail_id_count > 0) {
3620                 if (!emcore_delete_mail(input_account_id, mail_id_array, mail_id_count, input_from_server, EMAIL_DELETED_BY_COMMAND, false, &err)) {
3621                         EM_DEBUG_EXCEPTION("emcore_delete_mail failed [%d]", err);
3622                         goto FINISH_OFF;
3623                 }
3624         }
3625         
3626         ret = true;
3627
3628 FINISH_OFF:
3629         EM_SAFE_FREE(mail_id_array);
3630
3631         if (err_code != NULL)
3632                 *err_code = err;
3633
3634         EM_DEBUG_FUNC_END("ret [%d], err [%d]", ret, err);
3635         return ret;
3636 }
3637
3638 INTERNAL_FUNC int emcore_delete_mails_from_local_storage(int account_id, int *mail_ids, int num, int noti_param_1, int noti_param_2, int *err_code)
3639 {
3640         EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_ids[%p], num [%d], noti_param_1 [%d], noti_param_2 [%d], err_code[%p]", account_id, mail_ids, num, noti_param_1, noti_param_2, num, err_code);
3641         int ret = false, err = EMAIL_ERROR_NONE, i;
3642         emstorage_mail_tbl_t *result_mail_list = NULL;
3643         char mail_id_string[10], *noti_param_string = NULL, buf[512] = {0, };
3644
3645         /* Getting mail list by using select mail_id [in] */
3646         if(!emstorage_get_mail_field_by_multiple_mail_id(mail_ids, num, RETRIEVE_SUMMARY, &result_mail_list, true, &err) || !result_mail_list) {
3647                 EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_multiple_mail_id failed [%d]", err);
3648                 goto FINISH_OFF;
3649         }
3650
3651         /* Deleting mails by using select mail_id [in] */
3652         if(!emstorage_delete_multiple_mails(mail_ids, num, true, &err)) {
3653                 EM_DEBUG_EXCEPTION("emstorage_delete_multiple_mails failed [%d]", err);
3654                 goto FINISH_OFF;
3655         }
3656
3657         /* Sending Notification */
3658         noti_param_string = em_malloc(sizeof(char) * 10 * num);
3659
3660         if(!noti_param_string) {
3661                 EM_DEBUG_EXCEPTION("em_malloc failed");
3662                 err = EMAIL_ERROR_OUT_OF_MEMORY;
3663                 goto FINISH_OFF;
3664         }
3665
3666         for(i = 0; i < num; i++) {
3667                 memset(mail_id_string, 0, sizeof(mail_id_string));
3668                 SNPRINTF(mail_id_string, sizeof(mail_id_string), "%d,", mail_ids[i]);
3669                 strcat(noti_param_string, mail_id_string);
3670                 /* can be optimized by appending sub string with directly pointing on string array kyuho.jo 2011-10-07 */
3671         }
3672
3673         if (!emcore_notify_storage_event(NOTI_MAIL_DELETE, account_id, noti_param_1, noti_param_string, noti_param_2))
3674                 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event failed [ NOTI_MAIL_DELETE_FINISH ] >>>> ");
3675
3676         /* Updating Thread informations */
3677         /* Thread information should be updated as soon as possible. */
3678         for(i = 0; i < num; i++) {
3679                 if (result_mail_list[i].thread_item_count > 1)  {
3680                         if (!emstorage_update_latest_thread_mail(account_id, result_mail_list[i].thread_id, 0, 0, false,  &err)) {
3681                                 EM_DEBUG_EXCEPTION("emstorage_update_latest_thread_mail failed [%d]", err);
3682                 
3683                                 goto FINISH_OFF;
3684                         }
3685                 }
3686         }
3687         if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_FINISH, account_id, noti_param_1, noti_param_string, noti_param_2))
3688                 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event failed [ NOTI_MAIL_DELETE_FINISH ] >>>> ");
3689
3690         for(i = 0; i < num; i++) {
3691                 /* Deleting attachments */
3692                 if (!emstorage_delete_all_attachments_of_mail(result_mail_list[i].mail_id, false, &err)) {
3693                         EM_DEBUG_EXCEPTION("emstorage_delete_all_attachments_of_mail failed [%d]", err);
3694                         if (err == EMAIL_ERROR_ATTACHMENT_NOT_FOUND)
3695                                 err = EMAIL_ERROR_NONE;
3696                 }
3697
3698                 /* Deleting Directories */
3699                 /*  delete mail contents from filesystem */
3700                 if (!emstorage_get_save_name(account_id, result_mail_list[i].mail_id, 0, NULL, buf, &err)) {
3701                         EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
3702                         goto FINISH_OFF;
3703                 }
3704
3705                 if (!emstorage_delete_dir(buf, &err)) {
3706                         EM_DEBUG_EXCEPTION("emstorage_delete_dir failed [%d]", err);
3707                 }
3708                 
3709                 /* Deleting Meeting Request */
3710                 if (!emstorage_delete_meeting_request(account_id, result_mail_list[i].mail_id, 0, false, &err)) {
3711                         EM_DEBUG_EXCEPTION("emstorage_delete_meeting_request failed [%d]", err);
3712                         if (err != EMAIL_ERROR_CONTACT_NOT_FOUND) {
3713                                 goto FINISH_OFF;
3714                         }
3715                 }
3716         }
3717
3718         ret = true;
3719
3720 FINISH_OFF:
3721         EM_SAFE_FREE(noti_param_string);
3722
3723         if (result_mail_list)
3724                 emstorage_free_mail(&result_mail_list, num, NULL);
3725
3726         if (err_code != NULL)
3727                 *err_code = err;
3728
3729         EM_DEBUG_FUNC_END();
3730         return ret;
3731 }
3732
3733 static int emcore_delete_mails_from_pop3_server(email_account_t *input_account, int input_mail_ids[], int input_mail_id_count)
3734 {
3735         EM_DEBUG_FUNC_BEGIN("input_account[%p], input_mail_ids[%p], input_mail_id_count[%d]", input_account, input_mail_ids, input_mail_id_count);
3736
3737         int err = EMAIL_ERROR_NONE;
3738         int i = 0;
3739         int mail_id = 0;
3740         int msgno = 0;
3741         void *stream = NULL;
3742         email_internal_mailbox_t mailbox_data = { 0, };
3743         emstorage_mail_tbl_t    *mail_tbl_data = NULL;
3744         
3745         if (!input_account || !input_mail_ids) {
3746                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3747                 err = EMAIL_ERROR_INVALID_PARAM;
3748                 goto FINISH_OFF;
3749         }
3750
3751         for (i = 0; i < input_mail_id_count; i++)  {
3752                 FINISH_OFF_IF_CANCELED;
3753
3754                 mail_id = input_mail_ids[i];
3755
3756                 if (!emstorage_get_downloaded_mail(mail_id, &mail_tbl_data, false, &err) || !mail_tbl_data)  {
3757                         EM_DEBUG_EXCEPTION("emstorage_get_downloaded_mail failed [%d]", err);
3758
3759                         if (err == EMAIL_ERROR_MAIL_NOT_FOUND) {        /* not server mail */
3760                                 continue;
3761                         }
3762                         else
3763                                 goto FINISH_OFF;
3764                 }
3765
3766                 if (stream == NULL)  {
3767                         if (!emcore_connect_to_remote_mailbox(input_account->account_id, 0, (void **)&stream, &err))  {
3768                                 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
3769                                 goto FINISH_OFF;
3770                         }
3771
3772                         mailbox_data.mail_stream  = stream;
3773                         mailbox_data.account_id   = input_account->account_id;
3774                         mailbox_data.mailbox_id   = mail_tbl_data->mailbox_id;
3775                 }
3776
3777                 if (mailbox_data.user_data != NULL) {
3778                         emcore_free_uids(mailbox_data.user_data, NULL);
3779                         mailbox_data.user_data = NULL;
3780                 }
3781
3782                 if (!emcore_get_mail_msgno_by_uid(input_account, &mailbox_data, mail_tbl_data->server_mail_id, &msgno, &err))  {
3783                         EM_DEBUG_EXCEPTION("emcore_get_mail_msgno_by_uid faild [%d]", err);
3784                         if (err == EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER)
3785                                 goto NOT_FOUND_ON_SERVER;
3786                         else
3787                                 goto FINISH_OFF;
3788                 }
3789
3790                 if (!pop3_mail_delete(mailbox_data.mail_stream, msgno, &err)) {
3791                         EM_DEBUG_EXCEPTION("pop3_mail_delete failed [%d]", err);
3792         
3793                         if (err == EMAIL_ERROR_POP3_DELE_FAILURE)
3794                                 err = EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER;
3795                         goto FINISH_OFF;
3796                 }
3797
3798                 if (!emstorage_remove_downloaded_mail(input_account->account_id, mail_tbl_data->server_mailbox_name, mail_tbl_data->server_mail_id, true, &err))
3799                         EM_DEBUG_LOG("emstorage_remove_downloaded_mail falied [%d]", err);
3800
3801 NOT_FOUND_ON_SERVER :
3802
3803                 if (mail_tbl_data != NULL)
3804                         emstorage_free_mail(&mail_tbl_data, 1, NULL);
3805         }
3806
3807 FINISH_OFF: 
3808
3809         if (mail_tbl_data != NULL)
3810                 emstorage_free_mail(&mail_tbl_data, 1, NULL);
3811
3812         if (stream)              {
3813                 emcore_close_mailbox(0, stream);
3814                 stream = NULL;
3815         }
3816
3817         if (mailbox_data.user_data != NULL) {
3818                 emcore_free_uids(mailbox_data.user_data, NULL);
3819                 mailbox_data.user_data = NULL;
3820         }
3821
3822         EM_DEBUG_FUNC_END("err [%d]", err);
3823         return err;
3824 }
3825
3826 INTERNAL_FUNC int emcore_get_mail_msgno_by_uid(email_account_t *account, email_internal_mailbox_t *mailbox, char *uid, int *msgno, int *err_code)
3827 {
3828         EM_DEBUG_FUNC_BEGIN("account[%p], mailbox[%p], uid[%s], msgno[%p], err_code[%p]", account, mailbox, uid, msgno, err_code);
3829
3830         int ret = false;
3831         int err = EMAIL_ERROR_NONE;
3832         
3833         emcore_uid_list *uid_list = NULL;
3834         
3835         if (!account || !mailbox || !uid || !msgno)  {
3836                 EM_DEBUG_EXCEPTION("account[%p], mailbox[%p], uid[%s], msgno[%p]", account, mailbox, uid, msgno);
3837                 err = EMAIL_ERROR_INVALID_PARAM;
3838                 goto FINISH_OFF;
3839         }
3840         
3841         uid_list = mailbox->user_data;
3842         
3843         if (uid_list == NULL)  {
3844                 if (account->incoming_server_type == EMAIL_SERVER_TYPE_POP3)  {
3845                         if (!pop3_mailbox_get_uids(mailbox->mail_stream, &uid_list, &err))  {
3846                                 EM_DEBUG_EXCEPTION("pop3_mailbox_get_uids failed [%d]", err);
3847                                 goto FINISH_OFF;
3848                         }
3849                 }
3850                 else  {         /*  EMAIL_SERVER_TYPE_IMAP4 */
3851                         if (!imap4_mailbox_get_uids(mailbox->mail_stream, &uid_list, &err))  {
3852                                 EM_DEBUG_EXCEPTION("imap4_mailbox_get_uids failed [%d]", err);
3853                                 goto FINISH_OFF;
3854                         }
3855                 }
3856                 mailbox->user_data = uid_list;
3857         }
3858
3859         while (uid_list)  {
3860                 if (!strcmp(uid_list->uid, uid))  {
3861                         *msgno = uid_list->msgno;
3862                         EM_DEBUG_LOG("uid_list->msgno[%d]", uid_list->msgno);
3863                         ret = true;
3864                         goto FINISH_OFF;
3865                 }
3866                 EM_DEBUG_LOG("other uid_list->msgno[%d]", uid_list->msgno);
3867                 uid_list = uid_list->next;
3868         }
3869         
3870         err = EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER;
3871
3872 FINISH_OFF: 
3873         if (err_code != NULL)
3874                 *err_code = err;
3875
3876         if (uid_list != NULL)
3877                 emcore_free_uids(uid_list, NULL);
3878         /*  mailbox->user_data and uid_list both point to same memory address, So when uid_list  is freed then just set  */
3879         /*  mailbox->user_data to NULL and dont use EM_SAFE_FREE, it will crash  : ) */
3880         if (mailbox)
3881         mailbox->user_data = NULL;
3882         EM_DEBUG_FUNC_END();
3883         return ret;
3884 }
3885
3886 INTERNAL_FUNC int emcore_expunge_mails_deleted_flagged_from_local_storage(int input_mailbox_id)
3887 {
3888         EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d]", input_mailbox_id);
3889         int   err = EMAIL_ERROR_NONE;
3890         char *conditional_clause_string = NULL;
3891         email_list_filter_t *filter_list = NULL;
3892         int  filter_count = 0;
3893         int *result_mail_id_list = NULL;
3894         int  result_count = 0;
3895         emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
3896
3897         if (input_mailbox_id <= 0) {
3898                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3899                 err =  EMAIL_ERROR_INVALID_PARAM;
3900                 goto FINISH_OFF;
3901         }
3902
3903         if ((err = emstorage_get_mailbox_by_id(input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE) {
3904                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
3905                 goto FINISH_OFF;
3906         }
3907
3908         filter_count = 3;
3909
3910         filter_list = em_malloc(sizeof(email_list_filter_t) * filter_count);
3911
3912         if (filter_list == NULL) {
3913                 EM_DEBUG_EXCEPTION("em_malloc failed");
3914                 err = EMAIL_ERROR_OUT_OF_MEMORY;
3915                 goto FINISH_OFF;
3916         }
3917
3918         filter_list[0].list_filter_item_type                              = EMAIL_LIST_FILTER_ITEM_RULE;
3919         filter_list[0].list_filter_item.rule.rule_type                    = EMAIL_LIST_FILTER_RULE_EQUAL;
3920         filter_list[0].list_filter_item.rule.target_attribute             = EMAIL_MAIL_ATTRIBUTE_MAILBOX_ID;
3921         filter_list[0].list_filter_item.rule.key_value.integer_type_value = input_mailbox_id;
3922
3923         filter_list[1].list_filter_item_type                              = EMAIL_LIST_FILTER_ITEM_OPERATOR;
3924         filter_list[1].list_filter_item.operator_type                     = EMAIL_LIST_FILTER_OPERATOR_AND;
3925
3926         filter_list[2].list_filter_item_type                              = EMAIL_LIST_FILTER_ITEM_RULE;
3927         filter_list[2].list_filter_item.rule.rule_type                    = EMAIL_LIST_FILTER_RULE_EQUAL;
3928         filter_list[2].list_filter_item.rule.target_attribute             = EMAIL_MAIL_ATTRIBUTE_FLAGS_DELETED_FIELD;
3929         filter_list[2].list_filter_item.rule.key_value.integer_type_value = 1;
3930
3931         if ( (err = emstorage_write_conditional_clause_for_getting_mail_list(filter_list, filter_count, NULL, 0, -1, -1, &conditional_clause_string)) != EMAIL_ERROR_NONE) {
3932                 EM_DEBUG_EXCEPTION("emstorage_write_conditional_clause_for_getting_mail_list failed[%d]", err);
3933                 goto FINISH_OFF;
3934         }
3935
3936         EM_DEBUG_LOG("conditional_clause_string[%s].", conditional_clause_string);
3937
3938         if ((err = emstorage_query_mail_id_list(conditional_clause_string, true, &result_mail_id_list, &result_count)) != EMAIL_ERROR_NONE) {
3939                 EM_DEBUG_EXCEPTION("emstorage_query_mail_id_list [%d]", err);
3940                 goto FINISH_OFF;
3941         }
3942
3943         if (!emcore_delete_mails_from_local_storage(mailbox_tbl->account_id, result_mail_id_list, result_count, 1, EMAIL_DELETED_BY_COMMAND, &err)) {
3944                 EM_DEBUG_EXCEPTION("emcore_delete_mails_from_local_storage [%d]", err);
3945                 goto FINISH_OFF;
3946         }
3947
3948 FINISH_OFF:
3949
3950         if(mailbox_tbl)
3951                 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
3952
3953         if(filter_list)
3954                 emstorage_free_list_filter(&filter_list, filter_count);
3955
3956         EM_SAFE_FREE(conditional_clause_string);
3957         EM_SAFE_FREE(result_mail_id_list);
3958
3959         EM_DEBUG_FUNC_END("err [%d]", err);
3960         return err;
3961 }
3962
3963 INTERNAL_FUNC int emcore_expunge_mails_deleted_flagged_from_remote_server(int input_account_id, int input_mailbox_id)
3964 {
3965         int   err = EMAIL_ERROR_NONE;
3966         char *conditional_clause_string = NULL;
3967         email_list_filter_t *filter_list = NULL;
3968         int  filter_count = 0;
3969         int *result_mail_id_list = NULL;
3970         int  result_count = 0;
3971
3972         if ( input_mailbox_id <= 0) {
3973                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3974                 err =  EMAIL_ERROR_INVALID_PARAM;
3975                 goto FINISH_OFF;
3976         }
3977
3978         filter_count = 3;
3979
3980         filter_list = em_malloc(sizeof(email_list_filter_t) * filter_count);
3981
3982         if (filter_list == NULL) {
3983                 EM_DEBUG_EXCEPTION("em_malloc failed");
3984                 err = EMAIL_ERROR_OUT_OF_MEMORY;
3985                 goto FINISH_OFF;
3986         }
3987
3988         filter_list[0].list_filter_item_type                              = EMAIL_LIST_FILTER_ITEM_RULE;
3989         filter_list[0].list_filter_item.rule.rule_type                    = EMAIL_LIST_FILTER_RULE_EQUAL;
3990         filter_list[0].list_filter_item.rule.target_attribute             = EMAIL_MAIL_ATTRIBUTE_MAILBOX_ID;
3991         filter_list[0].list_filter_item.rule.key_value.integer_type_value = input_mailbox_id;
3992
3993         filter_list[1].list_filter_item_type                              = EMAIL_LIST_FILTER_ITEM_OPERATOR;
3994         filter_list[1].list_filter_item.operator_type                     = EMAIL_LIST_FILTER_OPERATOR_AND;
3995
3996         filter_list[2].list_filter_item_type                              = EMAIL_LIST_FILTER_ITEM_RULE;
3997         filter_list[2].list_filter_item.rule.rule_type                    = EMAIL_LIST_FILTER_RULE_EQUAL;
3998         filter_list[2].list_filter_item.rule.target_attribute             = EMAIL_MAIL_ATTRIBUTE_FLAGS_DELETED_FIELD;
3999         filter_list[2].list_filter_item.rule.key_value.integer_type_value = 1;
4000
4001         if ( (err = emstorage_write_conditional_clause_for_getting_mail_list(filter_list, filter_count, NULL, 0, -1, -1, &conditional_clause_string)) != EMAIL_ERROR_NONE) {
4002                 EM_DEBUG_EXCEPTION("emstorage_write_conditional_clause_for_getting_mail_list failed[%d]", err);
4003                 goto FINISH_OFF;
4004         }
4005
4006         EM_DEBUG_LOG("conditional_clause_string[%s].", conditional_clause_string);
4007
4008         if ((err = emstorage_query_mail_id_list(conditional_clause_string, true, &result_mail_id_list, &result_count)) != EMAIL_ERROR_NONE) {
4009                 EM_DEBUG_EXCEPTION("emstorage_query_mail_id_list [%d]", err);
4010                 goto FINISH_OFF;
4011         }
4012
4013         if (!emcore_delete_mail(input_account_id, result_mail_id_list, result_count, EMAIL_DELETE_FROM_SERVER, 1, EMAIL_DELETED_BY_COMMAND, &err)) {
4014                 EM_DEBUG_EXCEPTION("emcore_delete_mail [%d]", err);
4015                 goto FINISH_OFF;
4016         }
4017
4018 FINISH_OFF:
4019
4020         if(filter_list)
4021                 emstorage_free_list_filter(&filter_list, filter_count);
4022
4023         EM_SAFE_FREE(result_mail_id_list);
4024         EM_SAFE_FREE(conditional_clause_string);
4025
4026         EM_DEBUG_FUNC_END("err [%d]", err);
4027
4028         return err;
4029 }
4030
4031 /* description 
4032  *    add a attachment to mail.
4033  * arguments  
4034  *    mailbox  :  mail box
4035  *    mail_id  :  mail id
4036  *    attachment  :  attachment to be added
4037  * return  
4038  *    succeed  :  1
4039  *    fail  :  0
4040  */
4041 INTERNAL_FUNC int emcore_mail_add_attachment(int mail_id, email_attachment_data_t *attachment, int *err_code)
4042 {
4043         EM_DEBUG_FUNC_BEGIN("mail_id[%d], attachment[%p], err_code[%p]", mail_id, attachment, err_code);
4044         
4045         if (attachment == NULL)  {
4046                 EM_DEBUG_EXCEPTION("mail_id[%d], attachment[%p]", mail_id, attachment);
4047                 if (err_code)
4048                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4049                 return false;
4050         }
4051         
4052         int ret = false, err = EMAIL_ERROR_NONE;
4053         emstorage_mail_tbl_t *mail_table_data = NULL;
4054         int attachment_id = 0;
4055         
4056
4057         
4058         if (!emstorage_get_mail_field_by_id(mail_id, RETRIEVE_SUMMARY, &mail_table_data, true, &err) || !mail_table_data)  {
4059                 EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_id failed [%d]", err);
4060
4061                 goto FINISH_OFF2;
4062         }
4063         
4064         int account_id = mail_table_data->account_id;
4065         emstorage_attachment_tbl_t attachment_tbl;
4066         
4067         memset(&attachment_tbl, 0x00, sizeof(emstorage_attachment_tbl_t));
4068
4069         mail_table_data->attachment_count               = mail_table_data->attachment_count + 1;
4070         attachment_tbl.account_id                       = mail_table_data->account_id;
4071         attachment_tbl.mailbox_id                       = mail_table_data->mailbox_id;
4072         attachment_tbl.mail_id                          = mail_id;
4073         attachment_tbl.attachment_name                  = attachment->attachment_name;
4074         attachment_tbl.attachment_size                  = attachment->attachment_size;
4075         attachment_tbl.attachment_save_status           = attachment->save_status;
4076         attachment_tbl.attachment_drm_type              = attachment->drm_status;
4077         attachment_tbl.attachment_inline_content_status = attachment->inline_content_status;
4078
4079         /*  BEGIN TRANSACTION; */
4080         emstorage_begin_transaction(NULL, NULL, NULL);
4081
4082         if (!emstorage_add_attachment(&attachment_tbl, 0, false, &err))  {
4083                 EM_DEBUG_EXCEPTION("emstorage_add_attachment failed [%d]", err);
4084
4085                 goto FINISH_OFF;
4086         }
4087         
4088         attachment->attachment_id = attachment_tbl.attachment_id;
4089
4090         if (attachment->attachment_path)  {
4091                 char buf[512];
4092
4093                 if (!attachment->inline_content_status) {
4094                         if (!emstorage_create_dir(account_id, mail_id, attachment_tbl.attachment_id, &err))  {
4095                                 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
4096                                 goto FINISH_OFF;
4097                         }
4098                         attachment_id = attachment_tbl.attachment_id;
4099                 }
4100
4101                 if (!emstorage_get_save_name(account_id, mail_id, attachment_id, attachment->attachment_name, buf, &err))  {
4102                         EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
4103                         goto FINISH_OFF;
4104                 }
4105         attachment_tbl.attachment_path = buf;
4106
4107                 if (!emstorage_change_attachment_field(mail_id, UPDATE_SAVENAME, &attachment_tbl, false, &err))  {
4108                         EM_DEBUG_EXCEPTION("emstorage_change_attachment_field failed [%d]", err);
4109
4110                         goto FINISH_OFF;
4111                 }
4112
4113                 if (!emstorage_change_mail_field(mail_id, APPEND_BODY, mail_table_data, false, &err)) {
4114                         EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed [%d]", err);
4115         
4116                         goto FINISH_OFF;
4117                 }
4118
4119                 if (attachment->save_status) {
4120                         if (!emstorage_move_file(attachment->attachment_path, buf, false, &err))  {
4121                                 EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
4122
4123                                 goto FINISH_OFF;
4124                         }
4125                 }
4126
4127                 /* Here only filename is being updated. Since first add is being done there will not be any old files.
4128                     So no need to check for old files in this update case */
4129                 if (!emstorage_change_attachment_field(mail_id, UPDATE_SAVENAME, &attachment_tbl, false, &err))  {
4130                         EM_DEBUG_EXCEPTION("emstorage_change_attachment_field failed [%d]", err);
4131         
4132                         goto FINISH_OFF;
4133                 }
4134
4135                 EM_SAFE_FREE(attachment->attachment_path);
4136                 attachment->attachment_path = EM_SAFE_STRDUP(buf);
4137         }
4138         
4139         ret = true;
4140         
4141 FINISH_OFF: 
4142         if (ret == true) {      /*  COMMIT TRANSACTION; */
4143                 if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
4144                         err = EMAIL_ERROR_DB_FAILURE;
4145                         ret = false;
4146                 }
4147         }
4148         else {  /*  ROLLBACK TRANSACTION; */
4149                 if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)
4150                         err = EMAIL_ERROR_DB_FAILURE;
4151         }       
4152
4153 FINISH_OFF2: 
4154         if (mail_table_data != NULL)
4155                 emstorage_free_mail(&mail_table_data, 1, NULL);
4156
4157         if (err_code)
4158                 *err_code = err;
4159         EM_DEBUG_FUNC_END("err [%d]", err);
4160         return ret;
4161 }
4162
4163
4164 INTERNAL_FUNC int emcore_mail_add_attachment_data(int input_mail_id, email_attachment_data_t *input_attachment_data)
4165 {
4166         EM_DEBUG_FUNC_BEGIN("input_mail_id[%d], input_attachment_data[%p]", input_mail_id, input_attachment_data);
4167
4168         int                        err             = EMAIL_ERROR_NONE;
4169         int                        attachment_id   = 0;
4170         char                       buf[512] = { 0, };
4171         emstorage_mail_tbl_t            *mail_table_data = NULL;
4172         emstorage_attachment_tbl_t  attachment_tbl  = { 0 };
4173         
4174         if (input_attachment_data == NULL)  {
4175                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4176                 return EMAIL_ERROR_INVALID_PARAM;
4177         }
4178
4179         if (!emstorage_get_mail_field_by_id(input_mail_id, RETRIEVE_SUMMARY, &mail_table_data, true, &err) || !mail_table_data)  {
4180                 EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_id failed [%d]", err);
4181
4182                 goto FINISH_OFF2;
4183         }
4184
4185         mail_table_data->attachment_count               = mail_table_data->attachment_count + 1;
4186
4187         attachment_tbl.account_id                       = mail_table_data->account_id;
4188         attachment_tbl.mailbox_id                       = mail_table_data->mailbox_id;
4189         attachment_tbl.mail_id                          = input_mail_id;
4190         attachment_tbl.attachment_name                  = input_attachment_data->attachment_name;
4191         attachment_tbl.attachment_size                  = input_attachment_data->attachment_size;
4192         attachment_tbl.attachment_save_status           = input_attachment_data->save_status;
4193         attachment_tbl.attachment_drm_type          = input_attachment_data->drm_status;
4194         attachment_tbl.attachment_inline_content_status = input_attachment_data->inline_content_status;
4195         
4196         /*  BEGIN TRANSACTION; */
4197         emstorage_begin_transaction(NULL, NULL, NULL);
4198
4199         if (!emstorage_add_attachment(&attachment_tbl, 0, false, &err))  {
4200                 EM_DEBUG_EXCEPTION("emstorage_add_attachment failed [%d]", err);
4201
4202                 goto FINISH_OFF;
4203         }
4204         
4205         input_attachment_data->attachment_id = attachment_tbl.attachment_id;
4206
4207         if (input_attachment_data->attachment_path)  {
4208                 if (!input_attachment_data->inline_content_status) {
4209                         if (!emstorage_create_dir(mail_table_data->account_id, input_mail_id, attachment_tbl.attachment_id, &err))  {
4210                                 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
4211                                 goto FINISH_OFF;
4212                         }
4213                         attachment_id = attachment_tbl.attachment_id;
4214                 }
4215
4216                 if (!emstorage_get_save_name(mail_table_data->account_id, input_mail_id, attachment_id, input_attachment_data->attachment_name, buf, &err))  {
4217                         EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
4218                         goto FINISH_OFF;
4219                 }
4220         attachment_tbl.attachment_path = buf;
4221
4222                 if (!emstorage_change_attachment_field(input_mail_id, UPDATE_SAVENAME, &attachment_tbl, false, &err))  {
4223                         EM_DEBUG_EXCEPTION("emstorage_change_attachment_field failed [%d]", err);
4224
4225                         goto FINISH_OFF;
4226                 }
4227
4228                 if (!emstorage_change_mail_field(input_mail_id, APPEND_BODY, mail_table_data, false, &err)) {
4229                         EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed [%d]", err);
4230         
4231                         goto FINISH_OFF;
4232                 }
4233
4234                 if (input_attachment_data->save_status) {
4235                         if (!emstorage_move_file(input_attachment_data->attachment_path, buf, false, &err))  {
4236                                 EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
4237
4238                                 goto FINISH_OFF;
4239                         }
4240                 }
4241                 
4242                 /* Here only filename is being updated. Since first add is being done there will not be any old files.
4243                     So no need to check for old files in this update case */
4244                 if (!emstorage_change_attachment_field(input_mail_id, UPDATE_SAVENAME, &attachment_tbl, false, &err))  {
4245                         EM_DEBUG_EXCEPTION("emstorage_change_attachment_field failed [%d]", err);
4246         
4247                         goto FINISH_OFF;
4248                 }
4249                 
4250                 EM_SAFE_FREE(input_attachment_data->attachment_path);
4251                 input_attachment_data->attachment_path = EM_SAFE_STRDUP(buf);
4252         }
4253         
4254 FINISH_OFF:
4255         if (err == EMAIL_ERROR_NONE) {  /*  COMMIT TRANSACTION; */
4256                 if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
4257                         err = EMAIL_ERROR_DB_FAILURE;
4258                 }
4259         }
4260         else {  /*  ROLLBACK TRANSACTION; */
4261                 if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)
4262                         err = EMAIL_ERROR_DB_FAILURE;
4263         }       
4264         
4265 FINISH_OFF2:
4266         if (mail_table_data != NULL)
4267                 emstorage_free_mail(&mail_table_data, 1, NULL);
4268         
4269         EM_DEBUG_FUNC_END("err [%d]", err);
4270         return err;
4271 }
4272
4273
4274 /* description
4275  *    delete a attachment from mail.
4276  * arguments
4277  *    mailbox  :  mail box
4278  *    mail_id  :  mail id
4279  *    attachment_id  :  number string of attachment-id to be deleted
4280  *                 (ex :  if attachment id is 2, number stirng will be "2")
4281  * return
4282  *    succeed  :  1
4283  *    fail  :  0
4284  */
4285 int emcore_delete_mail_attachment(int attachment_id, int *err_code)
4286 {
4287         EM_DEBUG_FUNC_BEGIN("attachment_id[%d], err_code[%p]", attachment_id, err_code);
4288         
4289         if (attachment_id == 0)  {
4290                 EM_DEBUG_EXCEPTION("attachment_id[%d]", attachment_id);
4291                 if (err_code != NULL)
4292                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4293                 return false;
4294         }
4295
4296         int ret = true;
4297         int error = EMAIL_ERROR_NONE;
4298         char attachment_folder_path[MAX_PATH] = {0, };
4299         emstorage_attachment_tbl_t *attachment_tbl = NULL;
4300         
4301         if (!emstorage_get_attachment(attachment_id, &attachment_tbl, true, &error)) {
4302                 EM_DEBUG_EXCEPTION("emstorage_get_attachment failed");
4303                 return false;
4304         }
4305
4306         /*  BEGIN TRANSACTION; */
4307         emstorage_begin_transaction(NULL, NULL, NULL);
4308         
4309         if (!emstorage_delete_attachment_on_db(attachment_id, false, &error))  {
4310                 EM_DEBUG_EXCEPTION("emstorage_delete_attachment_on_db failed [%d]", error);
4311
4312                 if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)
4313                         error = EMAIL_ERROR_DB_FAILURE;
4314
4315                 ret = false;
4316                 goto FINISH_OFF;
4317         }
4318
4319         SNPRINTF(attachment_folder_path, sizeof(attachment_folder_path), "%s/%d/%d/%d", MAILHOME, attachment_tbl->account_id, attachment_tbl->mail_id, attachment_id);
4320
4321         if (!emstorage_delete_dir(attachment_folder_path, NULL)) {
4322                 EM_DEBUG_EXCEPTION("emstorage_delete_dir failed");
4323         }
4324
4325         if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
4326                 error = EMAIL_ERROR_DB_FAILURE;
4327                 ret = false;
4328         }
4329
4330 FINISH_OFF:
4331         if (attachment_tbl)
4332                 emstorage_free_attachment(&attachment_tbl, 1, NULL);
4333
4334         if (err_code != NULL)
4335                 *err_code = error;
4336
4337         EM_DEBUG_FUNC_END("ret [%d]", ret);
4338         return ret;
4339 }
4340
4341 static int emcore_mail_update_attachment_data(int input_mail_id, email_attachment_data_t *input_attachment_data)
4342 {
4343         EM_DEBUG_FUNC_BEGIN("input_mail_id[%d], input_attachment_data[%p]", input_mail_id, input_attachment_data);
4344
4345         int                         err = EMAIL_ERROR_NONE;
4346         int                         attachment_id = 0;
4347         char                        buf[512] = { 0 , };
4348         emstorage_attachment_tbl_t *existing_attachment_info = NULL;
4349         emstorage_attachment_tbl_t  attachment_tbl = { 0 };
4350
4351         if (input_attachment_data == NULL)  {
4352                 EM_DEBUG_EXCEPTION("Invalid Parameter");
4353                 err = EMAIL_ERROR_INVALID_PARAM;
4354                 goto FINISH_OFF2;
4355         }
4356
4357         if (!emstorage_get_attachment(input_attachment_data->attachment_id, &existing_attachment_info, 1, &err)) {
4358                 EM_DEBUG_EXCEPTION("emstorage_get_attachment failed [%d]", err);
4359
4360                 goto FINISH_OFF2;
4361         }
4362         
4363         attachment_tbl.mail_id                          = input_mail_id;
4364         attachment_tbl.account_id                       = existing_attachment_info->account_id;
4365         attachment_tbl.mailbox_id                       = existing_attachment_info->mailbox_id;
4366         attachment_tbl.attachment_name                  = input_attachment_data->attachment_name;
4367         attachment_tbl.attachment_size                  = input_attachment_data->attachment_size;
4368         attachment_tbl.attachment_path                  = input_attachment_data->attachment_path;
4369         attachment_tbl.attachment_save_status           = input_attachment_data->save_status;
4370         attachment_tbl.attachment_drm_type              = input_attachment_data->drm_status;
4371         attachment_tbl.attachment_inline_content_status = input_attachment_data->inline_content_status;
4372         attachment_tbl.attachment_id                    = input_attachment_data->attachment_id;
4373
4374         if (!input_attachment_data->inline_content_status) {
4375                 if (!emstorage_create_dir(attachment_tbl.account_id, input_mail_id, attachment_tbl.attachment_id, &err))  {
4376                         EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
4377                         goto FINISH_OFF;
4378                 }
4379                 attachment_id = attachment_tbl.attachment_id;
4380         }
4381         
4382         if (!emstorage_get_save_name(attachment_tbl.account_id, input_mail_id, attachment_id, input_attachment_data->attachment_name, buf, &err))  {
4383                 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
4384                 goto FINISH_OFF2;
4385         }
4386     attachment_tbl.attachment_path = buf;
4387
4388         EM_DEBUG_LOG("downloaded [%d], savename [%s], attachment_path [%s]", input_attachment_data->save_status, input_attachment_data->attachment_path, existing_attachment_info->attachment_path);
4389         if (input_attachment_data->save_status && EM_SAFE_STRCMP(input_attachment_data->attachment_path, existing_attachment_info->attachment_path) != 0) {
4390                 if (!emstorage_move_file(input_attachment_data->attachment_path, buf, false ,&err))  {
4391                         EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
4392
4393                         goto FINISH_OFF2;
4394                 }
4395         }
4396         else
4397                 EM_DEBUG_LOG("no need to move");
4398
4399         EM_SAFE_FREE(input_attachment_data->attachment_path);
4400         input_attachment_data->attachment_path = EM_SAFE_STRDUP(buf);
4401
4402         emstorage_begin_transaction(NULL, NULL, NULL);
4403
4404         if (!emstorage_update_attachment(&attachment_tbl, false, &err))  {
4405                 EM_DEBUG_EXCEPTION("emstorage_update_attachment failed [%d]", err);
4406
4407                 goto FINISH_OFF;
4408         }
4409         
4410 FINISH_OFF:
4411         if (err == EMAIL_ERROR_NONE) {  /*  COMMIT TRANSACTION; */
4412                 if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
4413                         err = EMAIL_ERROR_DB_FAILURE;
4414                 }
4415         }
4416         else {  /*  ROLLBACK TRANSACTION; */
4417                 if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)
4418                         err = EMAIL_ERROR_DB_FAILURE;
4419         }
4420
4421 FINISH_OFF2:
4422         if (existing_attachment_info)
4423                 emstorage_free_attachment(&existing_attachment_info, 1, NULL);
4424
4425         EM_DEBUG_FUNC_END("err [%d]", err);
4426         return err;
4427 }
4428
4429
4430 static int emcore_mail_compare_filename_of_attachment_data(int input_mail_id, int input_attachment_a_id, email_attachment_data_t *input_attachment_b_data, int *result)
4431 {
4432         EM_DEBUG_FUNC_BEGIN("input_mail_id[%d], input_attachment_a_id[%d], input_attachment_b_data[%p], result[%p]", input_mail_id, input_attachment_a_id, input_attachment_b_data, result);
4433
4434         EM_IF_NULL_RETURN_VALUE(input_attachment_b_data, false);
4435         EM_IF_NULL_RETURN_VALUE(result, false);
4436
4437         int err, err_2, ret = EMAIL_ERROR_NONE;
4438         emstorage_attachment_tbl_t *attachment_a_tbl = NULL;
4439
4440         if (!emstorage_get_attachment(input_attachment_a_id, &attachment_a_tbl, 1, &err)) {
4441                 EM_DEBUG_EXCEPTION("emstorage_get_attachment failed [%d]", err);
4442
4443                 goto FINISH_OFF;
4444         }
4445
4446         if (attachment_a_tbl->attachment_name && input_attachment_b_data->attachment_name) {
4447                 EM_DEBUG_LOG("attachment_a_tbl->attachment_name [%s], input_attachment_b_data->name [%s]", attachment_a_tbl->attachment_name, input_attachment_b_data->attachment_name);
4448                 *result = strcmp(attachment_a_tbl->attachment_name, input_attachment_b_data->attachment_name);
4449         }
4450
4451         ret = true;
4452
4453 FINISH_OFF: 
4454
4455         if (attachment_a_tbl)
4456                 emstorage_free_attachment(&attachment_a_tbl, 1, &err_2);
4457         EM_DEBUG_FUNC_END("*result [%d]", *result);
4458         return ret;
4459 }
4460
4461
4462 /* description 
4463  *    copy a mail to mail box
4464  * arguments  
4465  *    src_mailbox  :  source mail box
4466  *    msgno  :  mail sequence
4467  *    dst_mailbox  :  target mail box
4468  * return  
4469  *    succeed  :  1
4470  *    fail  :  0
4471  */
4472 INTERNAL_FUNC int emcore_mail_copy(int mail_id, email_mailbox_t *dst_mailbox, int *err_code)
4473 {
4474         EM_DEBUG_FUNC_BEGIN("mail_id[%d], dst_mailbox[%p], err_code[%p]", mail_id, dst_mailbox, err_code);
4475         
4476         int ret = false;
4477         int err = EMAIL_ERROR_NONE;
4478         int i;
4479         emstorage_mail_tbl_t *mail = NULL;
4480         emstorage_attachment_tbl_t *atch_list = NULL;
4481         char buf[512];
4482         int count = EMAIL_ATTACHMENT_MAX_COUNT;
4483         char *mailbox_name = NULL;
4484
4485         if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail)  {
4486                 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
4487
4488                 goto FINISH_OFF;
4489         }
4490
4491         if ( (err = emstorage_get_attachment_list(mail_id, true, &atch_list, &count)) != EMAIL_ERROR_NONE ){
4492                 EM_DEBUG_EXCEPTION("emstorage_get_attachment_list failed [%d]", err);
4493
4494                 goto FINISH_OFF;
4495         }
4496
4497         /*  get increased uid. */
4498         if (!emstorage_increase_mail_id(&mail->mail_id, true, &err))  {
4499                 EM_DEBUG_EXCEPTION("emstorage_increase_mail_id failed [%d]", err);
4500
4501                 goto FINISH_OFF;
4502         }
4503
4504         /*  copy mail body(text) file */
4505         if (mail->file_path_plain)  {
4506                 if (!emstorage_create_dir(dst_mailbox->account_id, mail->mail_id, 0, &err))  {
4507                         EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
4508
4509                         goto FINISH_OFF;
4510                 }
4511
4512                 gchar *filename = g_path_get_basename(mail->file_path_plain);
4513
4514                 if (!emstorage_get_save_name(dst_mailbox->account_id, mail->mail_id, 0, filename, buf, &err))  {
4515                         EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
4516                         g_free(filename);
4517
4518                         goto FINISH_OFF;
4519                 }
4520
4521                 g_free(filename);
4522
4523                 if (!emstorage_copy_file(mail->file_path_plain, buf, false, &err))  {
4524                         EM_DEBUG_EXCEPTION("emstorage_copy_file failed [%d]", err);
4525
4526                         goto FINISH_OFF;
4527                 }
4528
4529                 mail->file_path_plain = EM_SAFE_STRDUP(buf);
4530         }
4531
4532         /*  copy mail body(html) file */
4533         if (mail->file_path_html)  {
4534                 if (!emstorage_create_dir(dst_mailbox->account_id, mail->mail_id, 0, &err))  {
4535                         EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
4536
4537                         goto FINISH_OFF;
4538                 }
4539
4540                 gchar *filename = g_path_get_basename(mail->file_path_html);
4541
4542                 if (!emstorage_get_save_name(dst_mailbox->account_id, mail->mail_id, 0, filename, buf, &err))  {
4543                         EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
4544                         g_free(filename);
4545
4546                         goto FINISH_OFF;
4547                 }
4548
4549                 g_free(filename);
4550
4551                 if (!emstorage_copy_file(mail->file_path_html, buf, false, &err))  {
4552                         EM_DEBUG_EXCEPTION("emstorage_copy_file failed [%d]", err);
4553         
4554                         goto FINISH_OFF;
4555                 }
4556
4557                 mail->file_path_html = EM_SAFE_STRDUP(buf);
4558         }
4559
4560         /*  BEGIN TRANSACTION; */
4561         emstorage_begin_transaction(NULL, NULL, NULL);
4562
4563         /*  insert mail data */
4564         EM_SAFE_FREE(mail->mailbox_name);
4565         
4566         mail->account_id   = dst_mailbox->account_id;
4567         mail->mailbox_id   = dst_mailbox->mailbox_id;
4568         mail->mailbox_name = EM_SAFE_STRDUP(dst_mailbox->mailbox_name);
4569         mail->mailbox_type = dst_mailbox->mailbox_type;
4570         
4571         if (!emstorage_add_mail(mail, 0, false, &err))  {
4572                 EM_DEBUG_EXCEPTION("emstorage_add_mail failed [%d]", err);
4573
4574                 if (mail->file_path_plain)  {
4575                         if (!emstorage_delete_file(mail->file_path_plain, &err))  {
4576                                 EM_DEBUG_EXCEPTION("emstorage_delete_file failed [%d]", err);
4577                                 emstorage_rollback_transaction(NULL, NULL, NULL);
4578                                 goto FINISH_OFF;
4579                         }
4580                 }
4581                 if (mail->file_path_html) {
4582                         if (!emstorage_delete_file(mail->file_path_html, &err))  {
4583                                 EM_DEBUG_EXCEPTION("emstorage_delete_file failed [%d]", err);
4584                                 emstorage_rollback_transaction(NULL, NULL, NULL);
4585                                 goto FINISH_OFF;
4586                         }
4587                 }
4588
4589                 /*  ROLLBACK TRANSACTION; */
4590                 emstorage_rollback_transaction(NULL, NULL, NULL);
4591
4592
4593                 goto FINISH_OFF;
4594         }
4595
4596         /*  copy attachment file */
4597         for (i = 0; i<count; i++)  {
4598                 if (atch_list[i].attachment_path)  {
4599                         if (!emstorage_create_dir(dst_mailbox->account_id, mail->mail_id, i+1, &err))  {
4600                                 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
4601                                 break;
4602                         }
4603                         
4604                         if (!emstorage_get_save_name(dst_mailbox->account_id, mail->mail_id, i+1, atch_list[i].attachment_name, buf, &err))  {
4605                                 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
4606                                 break;
4607                         }
4608
4609                         if (!emstorage_copy_file(atch_list[i].attachment_path, buf, false, &err))  {
4610                                 EM_DEBUG_EXCEPTION("emstorage_copy_file failed [%d]", err);
4611                                 break;
4612                         }
4613
4614                         EM_SAFE_FREE(atch_list[i].attachment_path);
4615
4616                         atch_list[i].attachment_path = EM_SAFE_STRDUP(buf);
4617                 }
4618
4619                 atch_list[i].account_id = dst_mailbox->account_id;
4620                 atch_list[i].mail_id = mail->mail_id;
4621                 atch_list[i].mailbox_id = mail->mailbox_id;
4622
4623                 if (!emstorage_add_attachment(&atch_list[i], 0, false, &err))  {
4624                         EM_DEBUG_EXCEPTION("emstorage_add_attachment failed [%d]", err);
4625
4626                         break;
4627                 }
4628         }
4629
4630         /*  in case error happened, delete copied file. */
4631         if (i && i != count)  {
4632                 for (;i >= 0; i--)  {
4633                         if (atch_list[i].attachment_path)  {
4634                         
4635                                 if (!emstorage_delete_file(atch_list[i].attachment_path, &err))  {
4636                                         EM_DEBUG_EXCEPTION("emstorage_delete_file failed [%d]", err);
4637                                         emstorage_rollback_transaction(NULL, NULL, NULL);
4638                                         goto FINISH_OFF;
4639                                 }
4640                         }
4641                 }
4642
4643                 if (mail->file_path_plain)  {
4644                         if (!emstorage_delete_file(mail->file_path_plain, &err))  {
4645                                 EM_DEBUG_EXCEPTION("emstorage_delete_file failed [%d]", err);
4646                                 emstorage_rollback_transaction(NULL, NULL, NULL);
4647                                 goto FINISH_OFF;
4648                         }
4649                 }
4650                 if (mail->file_path_html)  {
4651                         if (!emstorage_delete_file(mail->file_path_html, &err))  {
4652                                 EM_DEBUG_EXCEPTION("emstorage_delete_file failed [%d]", err);
4653                                 emstorage_rollback_transaction(NULL, NULL, NULL);
4654                                 goto FINISH_OFF;
4655                         }
4656                 }
4657
4658                 /*  ROLLBACK TRANSACTION; */
4659                 emstorage_rollback_transaction(NULL, NULL, NULL);
4660                 goto FINISH_OFF;
4661         }
4662
4663         if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
4664                 err = EMAIL_ERROR_DB_FAILURE;
4665                 if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)
4666                         err = EMAIL_ERROR_DB_FAILURE;
4667                 goto FINISH_OFF;
4668         }
4669
4670         if (!emstorage_get_mailbox_name_by_mailbox_type(dst_mailbox->account_id, EMAIL_MAILBOX_TYPE_INBOX, &mailbox_name, false, &err))  {
4671                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_name_by_mailbox_type failed [%d]", err);
4672                 goto FINISH_OFF;
4673         }
4674
4675         if (!strcmp(dst_mailbox->mailbox_name, mailbox_name) && !(mail->flags_seen_field))
4676                 emcore_display_unread_in_badge();
4677
4678         ret = true;
4679
4680 FINISH_OFF: 
4681         if (atch_list != NULL)
4682                 emstorage_free_attachment(&atch_list, count, NULL);
4683
4684         if (mail != NULL)
4685                 emstorage_free_mail(&mail, 1, NULL);
4686
4687         EM_SAFE_FREE(mailbox_name);
4688
4689         if (err_code != NULL)
4690                 *err_code = err;
4691         EM_DEBUG_FUNC_END("err [%d]", err);
4692         return ret;
4693 }
4694
4695 /* description
4696  *    move a mail to mail box
4697  * arguments
4698  *    old_mailbox  :  previous mail box
4699  *    msgno  :  msgno
4700  *    new_mailbox  :  target mail box
4701  * return
4702  *    succeed  :  1
4703  *    fail  :  0
4704  */
4705 INTERNAL_FUNC int emcore_move_mail(int mail_ids[], int mail_ids_count, int dst_mailbox_id, int noti_param_1, int noti_param_2 ,int *err_code)
4706 {
4707         EM_DEBUG_FUNC_BEGIN("mail_ids[%p], mail_ids_count[%d], dst_mailbox_id[%d], noti_param [%d], err_code[%p]", mail_ids, mail_ids_count, dst_mailbox_id, noti_param_1, err_code);
4708
4709         int ret = false;
4710         int err = EMAIL_ERROR_NONE;
4711         emstorage_mail_tbl_t *mail_list = NULL;
4712         int account_id = 0;
4713         int i = 0, parameter_string_length = 0;
4714         char *parameter_string = NULL, mail_id_string[10];
4715
4716         if ( dst_mailbox_id <= 0  && mail_ids_count < 1) {
4717                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4718                 err = EMAIL_ERROR_INVALID_PARAM;
4719                 goto FINISH_OFF;
4720         }
4721
4722         if (!emstorage_get_mail_field_by_multiple_mail_id(mail_ids, mail_ids_count, RETRIEVE_FLAG, &mail_list, true, &err) || !mail_list) {
4723                 EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_multiple_mail_id failed [%d]", err);
4724                 goto FINISH_OFF;
4725         }
4726
4727         account_id = mail_list[0].account_id;
4728
4729         if(!emstorage_move_multiple_mails_on_db(account_id, dst_mailbox_id, mail_ids, mail_ids_count, true, &err)) {
4730                 EM_DEBUG_EXCEPTION("emstorage_move_multiple_mails_on_db failed [%d]", err);
4731                 goto FINISH_OFF;
4732         }
4733         
4734         /* Sending a notification */
4735         parameter_string_length = sizeof(char) * (mail_ids_count * 10 + 128/*MAILBOX_LEN_IN_MAIL_TBL*/ * 2);
4736         parameter_string = em_malloc(parameter_string_length);
4737
4738         if (parameter_string == NULL) {
4739                 EM_DEBUG_EXCEPTION("Memory allocation for mail_id_list_string failed");
4740                 err = EMAIL_ERROR_OUT_OF_MEMORY;
4741                 goto FINISH_OFF;
4742         }
4743
4744         if (mail_list[0].mailbox_id > 0)
4745                 SNPRINTF(parameter_string, parameter_string_length, "%d%c%d%c", mail_list[0].mailbox_id, 0x01, dst_mailbox_id , 0x01);
4746
4747         for (i = 0; i < mail_ids_count; i++) {
4748                 memset(mail_id_string, 0, 10);
4749                 SNPRINTF(mail_id_string, 10, "%d,", mail_ids[i]);
4750                 strcat(parameter_string, mail_id_string);
4751         }
4752         
4753         EM_DEBUG_LOG("num : [%d], param string : [%s]", mail_ids_count , parameter_string);
4754
4755         if (!emcore_notify_storage_event(NOTI_MAIL_MOVE, account_id, noti_param_1, parameter_string, noti_param_2))
4756                 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event failed [NOTI_MAIL_MOVE] >>>> ");
4757
4758
4759         for (i = 0; i < mail_ids_count; i++) {
4760                 if (!emstorage_update_latest_thread_mail(account_id, mail_list[i].thread_id, 0, 0, true, &err))
4761                         EM_DEBUG_EXCEPTION("emstorage_update_latest_thread_mail failed [%d]", err);
4762         }
4763
4764         if (!emcore_notify_storage_event(NOTI_MAIL_MOVE_FINISH, account_id, noti_param_1, parameter_string, noti_param_2))
4765                 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event failed [NOTI_MAIL_MOVE_FINISH] >>>> ");
4766
4767         emcore_display_unread_in_badge();
4768
4769         ret = true;
4770
4771 FINISH_OFF:
4772         emstorage_free_mail(&mail_list, mail_ids_count, NULL);
4773         EM_SAFE_FREE(parameter_string);
4774         if (err_code != NULL)
4775                 *err_code = err;
4776         EM_DEBUG_FUNC_END("err [%d]", err);
4777         return ret;
4778 }
4779
4780
4781 INTERNAL_FUNC int emcore_move_mail_on_server(int account_id, int src_mailbox_id,  int mail_ids[], int num, char *dest_mailbox, int *error_code)
4782 {
4783         EM_DEBUG_FUNC_BEGIN();
4784         MAILSTREAM *stream = NULL;
4785         int err_code = 0;
4786         email_account_t *ref_account = NULL;
4787         emstorage_mail_tbl_t *mail = NULL;
4788         int ret = 1;
4789         int mail_id = 0;
4790         int i = 0;
4791         
4792         mail_id = mail_ids[0];
4793         
4794         ref_account = emcore_get_account_reference(account_id);
4795         if (!ref_account)  {
4796                 EM_DEBUG_EXCEPTION("emcore_move_mail_on_server failed  :  get account reference[%d]", account_id);
4797                 *error_code = EMAIL_ERROR_INVALID_ACCOUNT;
4798                 ret = 0;
4799                 goto FINISH_OFF;
4800         }
4801
4802         /* if not imap4 mail, return */
4803         if (ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
4804                 *error_code = EMAIL_ERROR_INVALID_PARAM;
4805                 ret = 0;
4806                 goto FINISH_OFF;
4807         }
4808
4809         for (i = 0; i < num; i++)  {
4810                 mail_id = mail_ids[i];
4811
4812                 if (!emstorage_get_mail_by_id(mail_id, &mail, false, &err_code) || !mail)  {
4813                         EM_DEBUG_EXCEPTION("emstorage_get_uid_by_mail_id  :  emstorage_get_downloaded_mail failed [%d]", err_code);
4814                         mail = NULL;
4815                         if (err_code == EMAIL_ERROR_MAIL_NOT_FOUND)  {  /*  not server mail */
4816                                 /* err = EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER; */
4817                                 /* continue; */
4818                         }
4819                         else {
4820                                 *error_code = err_code;
4821                         }
4822
4823                         ret = 0;
4824                         goto FINISH_OFF;
4825                 }
4826
4827                 if (!emcore_connect_to_remote_mailbox(account_id, src_mailbox_id, (void **)&stream, &err_code))         /* faizan.h@samsung.com mail_move_fix_07042009 */ {
4828                         EM_DEBUG_EXCEPTION("emcore_move_mail_on_server failed :  Mailbox open[%d]", err_code);
4829
4830                         ret = 0;
4831                         goto FINISH_OFF;
4832                 }
4833
4834                 if (stream) {
4835                         /* set callback for COPY_UID */
4836                         mail_parameters(stream, SET_COPYUID, emcore_mail_copyuid);
4837
4838                         EM_DEBUG_LOG("calling mail_copy_full FODLER MAIL COPY ");
4839                         
4840                         if (mail->server_mail_id) {
4841                                 if (!mail_copy_full(stream, mail->server_mail_id, dest_mailbox, CP_UID | CP_MOVE)) {
4842                                         EM_DEBUG_EXCEPTION("emcore_move_mail_on_server :   Mail cannot be moved failed");
4843                                         ret = 0;
4844                                 }
4845                                 else {
4846                                         /*  send EXPUNGE command */
4847                                         if (!imap4_send_command(stream, IMAP4_CMD_EXPUNGE, &err_code)) {
4848                                                 EM_DEBUG_EXCEPTION("imap4_send_command failed [%d]", err_code);
4849
4850                                                 if (err_code == EMAIL_ERROR_IMAP4_STORE_FAILURE)
4851                                                         err_code = EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER;
4852                                                 goto FINISH_OFF;
4853                                         }
4854
4855                                         /* faizan.h@samsung.com   duplicate copy while sync mailbox issue fixed */
4856                                         EM_DEBUG_LOG(">>>mailbox_name[%s]>>>>>>", mail->mailbox_name);
4857                                         EM_DEBUG_LOG(">>>g_new_server_uid[%s]>>>>>>", g_new_server_uid);
4858                                         EM_DEBUG_LOG(">>>mail_id[%d]>>>>>>", mail_id);
4859
4860                                         if (!emstorage_update_read_mail_uid(mail_id, g_new_server_uid, mail->mailbox_name, &err_code)) {
4861                                                 EM_DEBUG_EXCEPTION("emstorage_update_read_mail_uid failed [%d]", err_code);
4862                                                 goto FINISH_OFF;
4863                                         }
4864
4865                                         EM_DEBUG_LOG("Mail MOVE SUCCESS ");
4866                                 }
4867                         }
4868                         else
4869                                 EM_DEBUG_EXCEPTION(">>>> Server MAIL ID IS NULL >>>> ");
4870                 }
4871                 else {
4872                         EM_DEBUG_EXCEPTION(">>>> STREAM DATA IS NULL >>> ");
4873                         ret = 0;
4874                         goto FINISH_OFF;
4875                 }
4876         }
4877
4878         /* ret = true; */
4879
4880 FINISH_OFF:
4881         if (stream) emcore_close_mailbox(account_id, stream);
4882
4883         if (ref_account) {
4884                 emcore_free_account(ref_account);
4885                 EM_SAFE_FREE(ref_account);
4886         }
4887
4888         if (mail != NULL)
4889                 emstorage_free_mail(&mail, 1, NULL);
4890         EM_DEBUG_FUNC_END("ret [%d]", ret);
4891         return ret;
4892 }
4893
4894
4895 static int emcore_copy_mail_to_another_account_on_local_storeage(int input_mail_id, emstorage_mailbox_tbl_t *input_source_mailbox, emstorage_mailbox_tbl_t *input_target_mailbox, int input_task_id, int *output_mail_id)
4896 {
4897         EM_DEBUG_FUNC_BEGIN("input_mail_id[%d] input_source_mailbox[%p] input_target_mailbox[%p] input_task_id [%d] output_mail_id[%p]", input_mail_id, input_source_mailbox, input_target_mailbox, input_task_id, output_mail_id);
4898
4899         int err = EMAIL_ERROR_NONE;
4900         int   attachment_count = 0;
4901         email_mail_data_t       *mail_data = NULL;
4902         email_attachment_data_t *attachment_data = NULL;
4903
4904         if ( input_source_mailbox == NULL || input_target_mailbox == NULL || input_mail_id <= 0 || output_mail_id == NULL) {
4905                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4906                 err = EMAIL_ERROR_INVALID_PARAM;
4907                 goto FINISH_OFF;
4908         }
4909
4910         if((err = emcore_get_mail_data(input_mail_id, &mail_data)) != EMAIL_ERROR_NONE) {
4911                 EM_DEBUG_EXCEPTION("emcore_get_mail_data failed [%d]", err);
4912                 err = EMAIL_ERROR_INVALID_MAIL;
4913                 goto FINISH_OFF;
4914         }
4915
4916         /* Check download status */
4917         if(mail_data->body_download_status != 1) {
4918                 /* If not downloaded, download fully */
4919                 if (!emcore_download_body_multi_sections_bulk(NULL,
4920                                         input_source_mailbox->account_id,
4921                                         input_mail_id,
4922                                         0,
4923                                         (mail_data->attachment_count > 0)?1:0,
4924                                         NO_LIMITATION,
4925                                         input_task_id,
4926                                         &err)) {
4927                         EM_DEBUG_EXCEPTION("emcore_download_body_multi_sections_bulk failed [%d]", err);
4928                         goto FINISH_OFF;
4929                 }
4930         }
4931
4932         /* Get attachments */
4933         if((err = emcore_get_attachment_data_list(input_mail_id, &attachment_data, &attachment_count)) != EMAIL_ERROR_NONE) {
4934                 EM_DEBUG_EXCEPTION("emcore_get_attachment_data_list failed [%d]", err);
4935                 goto FINISH_OFF;
4936         }
4937
4938         mail_data->account_id        = input_target_mailbox->account_id;
4939         mail_data->mail_id           = 0;
4940         mail_data->mailbox_id        = input_target_mailbox->mailbox_id;
4941         mail_data->mailbox_type      = input_target_mailbox->mailbox_type;
4942         mail_data->thread_id         = 0;
4943         mail_data->thread_item_count = 0;
4944
4945         if((err = emcore_add_mail(mail_data, attachment_data, attachment_count, NULL, false)) != EMAIL_ERROR_NONE) {
4946                 EM_DEBUG_EXCEPTION("emcore_add_mail failed [%d]", err);
4947                 goto FINISH_OFF;
4948         }
4949
4950         *output_mail_id = mail_data->mail_id;
4951
4952 FINISH_OFF:
4953         if (mail_data) {
4954                 emcore_free_mail_data(mail_data);
4955                 free(mail_data);                /* prevent 34648 */
4956         }
4957
4958         if (attachment_data)
4959                 emcore_free_attachment_data(&attachment_data, attachment_count, NULL);
4960
4961         EM_DEBUG_FUNC_END("err [%d]", err);
4962         return err;
4963 }
4964
4965 INTERNAL_FUNC int emcore_move_mail_to_another_account(int input_mail_id, int input_source_mailbox_id, int input_target_mailbox_id, int input_task_id)
4966 {
4967         EM_DEBUG_FUNC_BEGIN("input_mail_id[%d] input_source_mailbox_id[%d] input_target_mailbox_id[%d] result_mail_id[%p] input_task_id [%d]", input_mail_id, input_source_mailbox_id, input_target_mailbox_id, input_task_id);
4968         int err = EMAIL_ERROR_NONE;
4969         int err_for_delete_mail = EMAIL_ERROR_NONE;
4970         int moved_mail_id = 0;
4971         emstorage_mailbox_tbl_t *source_mailbox = NULL;
4972         emstorage_mailbox_tbl_t *target_mailbox = NULL;
4973         email_account_t *source_account_ref = NULL;
4974         email_account_t *target_account_ref = NULL;
4975
4976         if((err = emstorage_get_mailbox_by_id(input_source_mailbox_id, &source_mailbox)) != EMAIL_ERROR_NONE) {
4977                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed for source_mailbox [%d]", err);
4978                 goto FINISH_OFF;
4979         }
4980
4981         if((err = emstorage_get_mailbox_by_id(input_target_mailbox_id, &target_mailbox)) != EMAIL_ERROR_NONE) {
4982                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed for target_mailbox [%d]", err);
4983                 goto FINISH_OFF;
4984         }
4985
4986         /* Check account type */
4987         /* POP  -> IMAP possible */
4988         /* IMAP -> POP  possible, but the mail would not be on server */
4989         /* EAS  -> X    impossible */
4990         /* X    -> EAS  impossible */
4991
4992         source_account_ref = emcore_get_account_reference(source_mailbox->account_id);
4993
4994         if(source_account_ref == NULL || source_account_ref->incoming_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
4995                 EM_DEBUG_EXCEPTION("Invalid account");
4996                 err = EMAIL_ERROR_INVALID_ACCOUNT;
4997                 goto FINISH_OFF;
4998         }
4999
5000         target_account_ref = emcore_get_account_reference(target_mailbox->account_id);
5001
5002         if(target_account_ref == NULL || target_account_ref->incoming_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
5003                 EM_DEBUG_EXCEPTION("Invalid account");
5004                 err = EMAIL_ERROR_INVALID_ACCOUNT;
5005                 goto FINISH_OFF;
5006         }
5007
5008
5009         if((err = emcore_copy_mail_to_another_account_on_local_storeage(input_mail_id, source_mailbox, target_mailbox, input_task_id, &moved_mail_id)) != EMAIL_ERROR_NONE) {
5010                 EM_DEBUG_EXCEPTION("emcore_copy_mail_to_another_account_on_local_storeage failed [%d]", err);
5011                 goto FINISH_OFF;
5012         }
5013
5014         if(!emcore_set_flags_field(source_mailbox->account_id, &input_mail_id, 1, EMAIL_FLAGS_DELETED_FIELD, 1 , &err)) {
5015                 EM_DEBUG_EXCEPTION("emcore_set_flags_field failed [%d]", err);
5016                 goto FINISH_OFF;
5017         }
5018
5019         if(target_account_ref->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) {
5020                 if((err = emcore_sync_mail_from_client_to_server(moved_mail_id)) != EMAIL_ERROR_NONE) {
5021                         EM_DEBUG_EXCEPTION("emcore_sync_mail_from_client_to_server failed [%d]", err);
5022
5023                         /* if append is failed, restore source mail and delete copied mail. */
5024                         if(!emcore_set_flags_field(source_mailbox->account_id, &input_mail_id, 1, EMAIL_FLAGS_DELETED_FIELD, 0 , &err)) {
5025                                 EM_DEBUG_EXCEPTION("emcore_set_flags_field failed [%d]", err);
5026                                 goto FINISH_OFF;
5027                         }
5028
5029                         if(!emcore_delete_mail(target_mailbox->account_id, &moved_mail_id, 1, false, 0, 0, &err_for_delete_mail))
5030                                 EM_DEBUG_EXCEPTION("emcore_delete_mail failed [%d]", err_for_delete_mail);
5031                         goto FINISH_OFF;
5032                 }
5033         }
5034
5035         if(!emcore_delete_mail(source_mailbox->account_id, &input_mail_id, 1, true, 0, 0, &err)) {
5036                 EM_DEBUG_EXCEPTION("emcore_delete_mail failed [%d]", err);
5037                 goto FINISH_OFF;
5038         }
5039
5040
5041 FINISH_OFF:
5042         if (source_mailbox)
5043                 emstorage_free_mailbox(&source_mailbox, 1, NULL);
5044
5045         if (target_mailbox)
5046                 emstorage_free_mailbox(&target_mailbox, 1, NULL);
5047
5048         if (source_account_ref) {
5049                 emcore_free_account(source_account_ref);
5050                 EM_SAFE_FREE(source_account_ref);
5051         }
5052
5053         if (target_account_ref) {
5054                 emcore_free_account(target_account_ref);
5055                 EM_SAFE_FREE(target_account_ref);
5056         }
5057
5058         EM_DEBUG_FUNC_END("err [%d]", err);
5059         return err;
5060 }
5061
5062 INTERNAL_FUNC int emcore_save_mail_file(int account_id, int mail_id, int attachment_id, char *src_file_path, char *file_name, char *full_path, int *err_code)
5063 {
5064         EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], attachment_id[%d] , file_name[%p] , full_path[%p] , err_code[%p]", account_id, mail_id, attachment_id, file_name, full_path, err_code);
5065
5066         int ret = false, err = EMAIL_ERROR_NONE;
5067
5068         if (!file_name || !full_path || !src_file_path) {
5069                 EM_DEBUG_EXCEPTION("Invalid paramter");
5070                 err = EMAIL_ERROR_INVALID_PARAM;
5071                 goto FINISH_OFF;
5072         }
5073
5074         if (!emstorage_create_dir(account_id, mail_id, attachment_id, &err))  {
5075                 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
5076                 goto FINISH_OFF;
5077         }
5078         
5079         if (!emstorage_get_save_name(account_id, mail_id, attachment_id, file_name, full_path, &err))  {
5080                 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
5081                 goto FINISH_OFF;
5082         }
5083
5084         if (strcmp(src_file_path, full_path) != 0)  {
5085                 if (!emstorage_copy_file(src_file_path, full_path, false, &err))  {
5086                         EM_DEBUG_EXCEPTION("emstorage_copy_file failed [%d]", err);
5087                         goto FINISH_OFF;
5088                 }
5089         }
5090
5091         ret = true;
5092
5093 FINISH_OFF:
5094         if (err_code)
5095                 *err_code = err;
5096         return 1;
5097 }
5098
5099 /* description : update mail information */
5100 INTERNAL_FUNC int emcore_update_mail(email_mail_data_t *input_mail_data, email_attachment_data_t *input_attachment_data_list, int input_attachment_count, email_meeting_request_t* input_meeting_request, int input_from_eas)
5101 {
5102         EM_DEBUG_FUNC_BEGIN("input_mail_data[%p], input_attachment_data_list[%p], input_attachment_count[%d], input_meeting_request[%p], input_from_eas[%d]", input_mail_data, input_attachment_data_list, input_attachment_count, input_meeting_request, input_from_eas);
5103
5104         char                   filename_buf[1024]         = {0, };
5105         char                  *body_text_file_name        = NULL;
5106         int                    i                          = 0;
5107         int                    err                        = EMAIL_ERROR_NONE;
5108         int                    local_inline_content_count = 0;
5109         emstorage_mail_tbl_t  *converted_mail_tbl_data    = NULL;
5110         email_meeting_request_t *meeting_req = NULL;
5111         struct stat            st_buf;
5112
5113         if (!input_mail_data || (input_attachment_count && !input_attachment_data_list) || (!input_attachment_count &&input_attachment_data_list))  {
5114                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5115                 err = EMAIL_ERROR_INVALID_PARAM;
5116                 goto FINISH_OFF2;
5117         }
5118
5119         if(input_from_eas == 0) {
5120                 if (input_mail_data->file_path_plain)  {
5121                         if (stat(input_mail_data->file_path_plain, &st_buf) < 0)  {
5122                                 EM_DEBUG_EXCEPTION("input_mail_data->file_path_plain, stat(\"%s\") failed...", input_mail_data->file_path_plain);
5123                                 err = EMAIL_ERROR_INVALID_MAIL;
5124                                 goto FINISH_OFF;
5125                         }
5126                 }
5127                 
5128                 if (input_mail_data->file_path_html)  {
5129                         if (stat(input_mail_data->file_path_html, &st_buf) < 0)  {
5130                                 EM_DEBUG_EXCEPTION("input_mail_data->file_path_html, stat(\"%s\") failed...", input_mail_data->file_path_html);
5131                                 err = EMAIL_ERROR_INVALID_MAIL;
5132                                 goto FINISH_OFF;
5133                         }
5134                 }
5135                 
5136                 if (input_attachment_count && input_attachment_data_list)  {
5137                         for (i = 0; i < input_attachment_count; i++)  {
5138                                 if (input_attachment_data_list[i].save_status) {
5139                                         if (!input_attachment_data_list[i].attachment_path || stat(input_attachment_data_list[i].attachment_path, &st_buf) < 0)  {
5140                                                 EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", input_attachment_data_list[i].attachment_path);
5141                                                 err = EMAIL_ERROR_INVALID_ATTACHMENT;
5142                                                 goto FINISH_OFF;
5143                                         }
5144                                 }
5145                         }
5146                 }
5147         }
5148         
5149         if(input_mail_data->mail_size == 0) {
5150                  emcore_calc_mail_size(input_mail_data, input_attachment_data_list, input_attachment_count, &(input_mail_data->mail_size));
5151         }
5152
5153         if (input_mail_data->file_path_plain)  {   /*  Save plain text body. */
5154                 if ( (err = em_get_file_name_from_file_path(input_mail_data->file_path_plain, &body_text_file_name)) != EMAIL_ERROR_NONE) {
5155                         EM_DEBUG_EXCEPTION("em_get_file_name_from_file_path failed [%d]", err);
5156                         err = EMAIL_ERROR_INVALID_FILE_PATH;
5157                         goto FINISH_OFF;
5158                 }
5159
5160                 if (!emcore_save_mail_file(input_mail_data->account_id, input_mail_data->mail_id, 0, input_mail_data->file_path_plain, body_text_file_name, filename_buf, &err)) {
5161                         EM_DEBUG_EXCEPTION("emcore_save_mail_file failed [%d]", err);
5162                         goto FINISH_OFF2;
5163                 }
5164                 EM_SAFE_FREE(input_mail_data->file_path_plain);
5165                 input_mail_data->file_path_plain = EM_SAFE_STRDUP(filename_buf);
5166         }
5167         
5168         if (input_mail_data->file_path_html)  {   /*  Save HTML text body. */
5169                 EM_SAFE_FREE(body_text_file_name);
5170                 if ( (err = em_get_file_name_from_file_path(input_mail_data->file_path_html, &body_text_file_name)) != EMAIL_ERROR_NONE) {
5171                         EM_DEBUG_EXCEPTION("em_get_file_name_from_file_path failed [%d]", err);
5172                         err = EMAIL_ERROR_INVALID_FILE_PATH;
5173                         goto FINISH_OFF;
5174                 }
5175
5176                 if (!emcore_save_mail_file(input_mail_data->account_id, input_mail_data->mail_id, 0, input_mail_data->file_path_html, body_text_file_name, filename_buf, &err)) {
5177                         EM_DEBUG_EXCEPTION("emcore_save_mail_file failed [%d]", err);
5178                         goto FINISH_OFF2;
5179                 }
5180                 EM_SAFE_FREE(input_mail_data->file_path_html);
5181                 input_mail_data->file_path_html = EM_SAFE_STRDUP(filename_buf);
5182         }
5183
5184         if (input_mail_data->file_path_mime_entity)  {   /*  Save mime entity. */
5185                 if (!emcore_save_mail_file(input_mail_data->account_id, input_mail_data->mail_id, 0, input_mail_data->file_path_mime_entity, "mime_entity", filename_buf, &err)) {
5186                         EM_DEBUG_EXCEPTION("emcore_save_mail_file failed [%d]", err);
5187                         goto FINISH_OFF2;
5188                 }
5189                 EM_SAFE_FREE(input_mail_data->file_path_mime_entity);
5190                 input_mail_data->file_path_mime_entity = EM_SAFE_STRDUP(filename_buf);
5191         }
5192
5193         if (input_attachment_data_list && input_attachment_count)  {
5194                 int i = 0;
5195                 int compare_result = 1;
5196                 email_attachment_data_t *temp_attachment_data = NULL;
5197
5198                 for(i = 0; i < input_attachment_count; i++) {
5199                         temp_attachment_data = input_attachment_data_list + i;
5200                         if ( (err = emcore_mail_compare_filename_of_attachment_data(input_mail_data->mail_id, temp_attachment_data->attachment_id, temp_attachment_data, &compare_result)) != EMAIL_ERROR_NONE) {
5201                                 EM_DEBUG_EXCEPTION("emcore_mail_compare_filename_of_attachment_data failed [%d]", err);
5202                                 compare_result = 1;
5203                         }
5204         
5205                         if (compare_result == 0) {
5206                                 EM_DEBUG_LOG("file name and attachment id are same, update exising attachment");
5207                                 if (!emcore_mail_update_attachment_data(input_mail_data->mail_id, temp_attachment_data))  {
5208                                         EM_DEBUG_EXCEPTION("emcore_mail_update_attachment_data failed [%d]", err);
5209                                         goto FINISH_OFF2;
5210                                 }
5211                         }
5212                         else {
5213                                 EM_DEBUG_LOG("save names are different");
5214                                 if(temp_attachment_data->attachment_id > 0) {
5215                                         if (!emcore_delete_mail_attachment(temp_attachment_data->attachment_id, &err)) {
5216                                                 EM_DEBUG_EXCEPTION("emcore_delete_mail_attachment failed [%d]", err);
5217                                                 goto FINISH_OFF2;
5218                                         }
5219                                 }
5220
5221                                 if ( (err = emcore_mail_add_attachment_data(input_mail_data->mail_id, temp_attachment_data)) != EMAIL_ERROR_NONE)  {
5222                                         EM_DEBUG_EXCEPTION("emcore_mail_add_attachment failed [%d]", err);
5223                                         goto FINISH_OFF2;
5224                                 }
5225                         }
5226
5227                         if (temp_attachment_data->inline_content_status)
5228                                 local_inline_content_count++;
5229                 }
5230         }
5231         
5232         input_mail_data->attachment_count     = input_attachment_count;
5233         input_mail_data->inline_content_count = local_inline_content_count;
5234
5235         if (!input_mail_data->date_time) {
5236                 /* time isn't set */
5237                 input_mail_data->date_time = time(NULL);
5238         }
5239
5240         EM_DEBUG_LOG("preview_text[%p]", input_mail_data->preview_text);
5241         if (input_mail_data->preview_text == NULL) {
5242                 if ( (err =emcore_get_preview_text_from_file(input_mail_data->file_path_plain, input_mail_data->file_path_html, MAX_PREVIEW_TEXT_LENGTH, &(input_mail_data->preview_text))) != EMAIL_ERROR_NONE) {
5243                         EM_DEBUG_EXCEPTION("emcore_get_preview_text_from_file failed[%d]", err);
5244                         if (err != EMAIL_ERROR_EMPTY_FILE)
5245                                 goto FINISH_OFF2;
5246                 }
5247         }
5248
5249         if(!em_convert_mail_data_to_mail_tbl(input_mail_data, 1, &converted_mail_tbl_data,  &err)) {
5250                 EM_DEBUG_EXCEPTION("em_convert_mail_data_to_mail_tbl failed[%d]", err);
5251                 goto FINISH_OFF2;
5252         }
5253         
5254         /*  BEGIN TRANSACTION; */
5255         emstorage_begin_transaction(NULL, NULL, NULL);
5256         
5257         if (!emstorage_change_mail_field(input_mail_data->mail_id, UPDATE_MAIL, converted_mail_tbl_data, false, &err))  {
5258                 EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed [%d]", err);
5259
5260                 goto FINISH_OFF;
5261         }
5262         
5263         if (input_mail_data->meeting_request_status == EMAIL_MAIL_TYPE_MEETING_REQUEST
5264                 || input_mail_data->meeting_request_status == EMAIL_MAIL_TYPE_MEETING_RESPONSE
5265                 || input_mail_data->meeting_request_status == EMAIL_MAIL_TYPE_MEETING_ORIGINATINGREQUEST) {
5266                 /*  check where there is a meeting request in DB */
5267                 if (!emstorage_get_meeting_request(input_mail_data->mail_id, &meeting_req, false, &err) && err != EMAIL_ERROR_DATA_NOT_FOUND) {
5268                         EM_DEBUG_EXCEPTION("emstorage_get_meeting_request failed [%d]", err);
5269                         goto FINISH_OFF;
5270                 }
5271                 EM_SAFE_FREE(meeting_req);
5272                 if (err == EMAIL_ERROR_DATA_NOT_FOUND) {        /*  insert */
5273                         emstorage_mail_tbl_t *original_mail = NULL;
5274
5275                         if (!emstorage_get_mail_by_id(input_mail_data->mail_id, &original_mail, false, &err)) {
5276                                 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
5277                                 goto FINISH_OFF;
5278                         }
5279
5280                         if (original_mail)       {
5281                         if (!emstorage_add_meeting_request(input_mail_data->account_id, original_mail->mailbox_id, input_meeting_request, false, &err))  {
5282                                 EM_DEBUG_EXCEPTION("emstorage_add_meeting_request failed [%d]", err);
5283
5284                                 goto FINISH_OFF;
5285                         }
5286                                 emstorage_free_mail(&original_mail, 1, NULL);
5287                 }
5288                 }
5289                 else {  /*  update */
5290                         if (!emstorage_update_meeting_request(input_meeting_request, false, &err))  {
5291                                 EM_DEBUG_EXCEPTION("emstorage_update_meeting_request failed [%d]", err);
5292
5293                                 goto FINISH_OFF;
5294                         }
5295                 }
5296         }
5297         
5298 FINISH_OFF: 
5299         if (err == EMAIL_ERROR_NONE) {
5300                 /*  COMMIT TRANSACTION; */
5301                 if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
5302                         err = EMAIL_ERROR_DB_FAILURE;
5303                 }
5304
5305                 if (input_mail_data->meeting_request_status && !emcore_notify_storage_event(NOTI_MAIL_UPDATE, input_mail_data->account_id, input_mail_data->mail_id, NULL, UPDATE_MEETING))
5306                         EM_DEBUG_EXCEPTION(" emcore_notify_storage_event failed [ NOTI_MAIL_UPDATE : UPDATE_MEETING_RESPONSE ] >>>> ");
5307         }
5308         else {
5309                 /*  ROLLBACK TRANSACTION; */
5310                 if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)
5311                         err = EMAIL_ERROR_DB_FAILURE;
5312         }
5313         
5314 FINISH_OFF2:
5315
5316         EM_SAFE_FREE(body_text_file_name);
5317
5318         if(meeting_req)
5319                 emstorage_free_meeting_request(meeting_req);
5320
5321         if(converted_mail_tbl_data)
5322                 emstorage_free_mail(&converted_mail_tbl_data, 1, NULL);
5323
5324         EM_DEBUG_FUNC_END("err [%d]", err);
5325         return err;
5326 }
5327
5328
5329 INTERNAL_FUNC int emcore_set_flags_field(int account_id, int mail_ids[], int num, email_flags_field_type field_type, int value, int *err_code)
5330 {
5331         EM_DEBUG_FUNC_BEGIN("account_id [%d], mail_ids[%p], num [%d], field_type [%d], value[%d], err_code[%p]", account_id, mail_ids, num, field_type, value, err_code);
5332
5333         int ret = false;
5334         int err = EMAIL_ERROR_NONE;
5335         char *field_type_name[EMAIL_FLAGS_FIELD_COUNT] = { "flags_seen_field"
5336                 , "flags_deleted_field", "flags_flagged_field", "flags_answered_field"
5337                 , "flags_recent_field", "flags_draft_field", "flags_forwarded_field" };
5338
5339         if(field_type < 0 || field_type >= EMAIL_FLAGS_FIELD_COUNT || mail_ids == NULL || num <= 0 || account_id == 0) {
5340                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5341                 err = EMAIL_ERROR_INVALID_PARAM;
5342                 goto  FINISH_OFF;
5343         }
5344
5345         if (!emstorage_set_field_of_mails_with_integer_value(account_id, mail_ids, num, field_type_name[field_type], value, true, &err)) {
5346                 EM_DEBUG_EXCEPTION("emstorage_set_field_of_mails_with_integer_value failed [%d]", err);
5347                 goto FINISH_OFF;
5348         }
5349
5350         if(field_type == EMAIL_FLAGS_SEEN_FIELD)
5351                 emcore_display_unread_in_badge();
5352
5353         ret = true;
5354         
5355 FINISH_OFF: 
5356         
5357         if (err_code)
5358                 *err_code = err;
5359
5360         EM_DEBUG_FUNC_END("err [%d]", err);     
5361         return ret;
5362 }
5363
5364 int emcore_mail_cmd_read_mail_pop3(void *stream, int msgno, int limited_size, int *downloded_size, int *result_total_body_size, int *err_code)
5365 {
5366         EM_DEBUG_FUNC_BEGIN("stream[%p], msgno[%d], limited_size[%d], err_code[%p]", stream, msgno, limited_size, err_code);
5367         
5368         int ret = false;
5369         int err = EMAIL_ERROR_NONE;
5370         int total_body_size = 0;
5371         char command[32];
5372         char *response = NULL;
5373         POP3LOCAL *pop3local;
5374         
5375         if (!stream || !result_total_body_size)  {
5376                 EM_DEBUG_EXCEPTION("stream[%p], total_body_size[%p]", stream, msgno, result_total_body_size);
5377                 err = EMAIL_ERROR_INVALID_PARAM;
5378                 goto FINISH_OFF;
5379         }
5380         
5381         pop3local = (POP3LOCAL *)(((MAILSTREAM *)stream)->local);
5382
5383         if (!pop3local  || !pop3local->netstream)  {
5384                 err = EMAIL_ERROR_INVALID_STREAM;
5385                 goto FINISH_OFF;
5386         }
5387         memset(command, 0x00, sizeof(command));
5388
5389         SNPRINTF(command, sizeof(command), "LIST %d\015\012", msgno);
5390
5391 #ifdef FEATURE_CORE_DEBUG
5392         EM_DEBUG_LOG("[POP3] >>> [%s]", command);
5393 #endif
5394
5395         
5396         /*  send command  :  LIST [msgno] - to get the size of the mail */
5397         if (!net_sout(pop3local->netstream, command, (int)EM_SAFE_STRLEN(command)))  {
5398                 EM_DEBUG_EXCEPTION("net_sout failed...");
5399
5400                 err = EMAIL_ERROR_INVALID_RESPONSE;
5401                 goto FINISH_OFF;
5402         }
5403
5404         EM_DEBUG_LOG("Sending command success");
5405
5406         while (pop3local->netstream) {
5407                 /*  receive response */
5408                 if (!(response = net_getline(pop3local->netstream)))
5409                         break;
5410
5411                 if (response)
5412                         EM_DEBUG_LOG("[POP3] <<< %s", response);
5413
5414                 if (*response == '.')  {
5415                         EM_SAFE_FREE(response);
5416                         break;
5417                 }
5418
5419                 if (*response == '+')  {                /*  "+ OK" */
5420                         char *p = NULL;
5421
5422                         if (!(p = strchr(response + strlen("+OK "), ' ')))  {
5423                                 err = EMAIL_ERROR_INVALID_RESPONSE;
5424                                 goto FINISH_OFF;
5425                         }
5426
5427                         total_body_size = atoi(p + 1);
5428                         EM_DEBUG_LOG("Body size [%d]", total_body_size);
5429
5430                         if (result_total_body_size) {
5431                                 *result_total_body_size = total_body_size;
5432                                 break;
5433                         }
5434                 }
5435                 else if (*response == '-')  {   /*  "- ERR" */
5436                         err = EMAIL_ERROR_INVALID_RESPONSE;
5437                         goto FINISH_OFF;
5438                 }
5439                 else  {
5440                         err = EMAIL_ERROR_INVALID_RESPONSE;
5441                         goto FINISH_OFF;
5442                 }
5443
5444                 EM_SAFE_FREE(response);
5445         }
5446
5447         memset(command, 0x00, sizeof(command));
5448
5449         if (limited_size && total_body_size > limited_size) {
5450                 int count_of_line = limited_size / 80;
5451                 SNPRINTF(command, sizeof(command), "TOP %d %d", msgno, count_of_line);
5452         }
5453         else
5454                 SNPRINTF(command, sizeof(command), "RETR %d", msgno);
5455
5456         EM_DEBUG_LOG("[POP3] >>> %s", command);
5457
5458         emcore_set_network_error(EMAIL_ERROR_NONE);             /*  set current network error as EMAIL_ERROR_NONE before network operation */
5459         /*  get mail from mail server */
5460         if (!pop3_send((MAILSTREAM *)stream, command, NULL))  {
5461                 EM_DEBUG_EXCEPTION("pop3_send failed...");
5462
5463                 email_session_t *session = NULL;
5464                 
5465                 if (!emcore_get_current_session(&session))  {
5466                         EM_DEBUG_EXCEPTION("emcore_get_current_session failed...");
5467                         err = EMAIL_ERROR_SESSION_NOT_FOUND;
5468                         goto FINISH_OFF;
5469                 }
5470
5471                 if (session->network == EMAIL_ERROR_NONE)
5472                         err = EMAIL_ERROR_UNKNOWN;
5473                 else
5474                         err = session->network;
5475
5476                 goto FINISH_OFF;
5477         }
5478
5479         ret = true;
5480         
5481 FINISH_OFF: 
5482         EM_SAFE_FREE(response);
5483         
5484         if (err_code != NULL)
5485                 *err_code = err;
5486         EM_DEBUG_FUNC_END("err [%d]", err);     
5487         return ret;
5488 }
5489
5490
5491 INTERNAL_FUNC int emcore_sync_flag_with_server(int mail_id, int *err_code)
5492 {
5493         EM_DEBUG_FUNC_BEGIN("mail_id[%p], err_code[%p]", mail_id, err_code);
5494                 
5495         if (mail_id < 1)  {
5496                 if (err_code != NULL)
5497                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5498                 return false;
5499         }
5500         
5501         int ret = false;
5502         int err = EMAIL_ERROR_NONE;
5503         int status = EMAIL_DOWNLOAD_FAIL;
5504         MAILSTREAM *stream = NULL;
5505         email_internal_mailbox_t mailbox = {0};
5506         emstorage_mail_tbl_t *mail = NULL;
5507         email_account_t *ref_account = NULL;
5508         int account_id = 0;
5509         int msgno = 0;
5510         char set_flags[100] = { 0, };
5511         char clear_flags[100] = { 0, };
5512         char tmp[100] = { 0, };
5513         
5514         if (!emcore_check_thread_status())  {
5515                 err = EMAIL_ERROR_CANCELLED;
5516                 goto FINISH_OFF;
5517         }
5518         
5519         if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail)  {
5520                 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
5521
5522                 goto FINISH_OFF;
5523         }
5524
5525         account_id = mail->account_id;
5526
5527         if (!(ref_account = emcore_get_account_reference(account_id)))   {
5528                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
5529                 err = EMAIL_ERROR_INVALID_ACCOUNT;
5530                 goto FINISH_OFF;
5531         }
5532         
5533         if (!emcore_check_thread_status())  {
5534                 err = EMAIL_ERROR_CANCELLED;
5535                 goto FINISH_OFF;
5536         }
5537
5538         /*  open mail server. */
5539         if (!emcore_connect_to_remote_mailbox(account_id, mail->mailbox_id, (void **)&stream, &err) || !stream)  {
5540                 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
5541                 status = EMAIL_LIST_CONNECTION_FAIL;
5542                 goto FINISH_OFF;
5543         }
5544
5545
5546         if (!emcore_check_thread_status())  {
5547                 err = EMAIL_ERROR_CANCELLED;
5548                 goto FINISH_OFF;
5549         }
5550
5551         mailbox.mailbox_name = mail->mailbox_name;      
5552         mailbox.account_id = account_id;
5553         mailbox.mail_stream = stream;
5554
5555         /*  download all uids from server. */
5556         if (!emcore_get_mail_msgno_by_uid(ref_account, &mailbox, mail->server_mail_id, &msgno, &err))  {
5557                 EM_DEBUG_EXCEPTION("emcore_get_mail_msgno_by_uid failed message_no  :  %d ", err);
5558                 goto FINISH_OFF;
5559         }
5560                 
5561         sprintf (tmp, "%d", msgno);
5562
5563         if (mail->flags_seen_field)
5564                 sprintf(set_flags, "\\Seen");
5565         else
5566                 sprintf(clear_flags, "\\Seen");
5567
5568         if (mail->flags_answered_field)
5569                 sprintf(set_flags, "%s \\Answered", set_flags);
5570         else
5571                 sprintf(clear_flags, "%s \\Answered", clear_flags);
5572                 
5573         if (mail->flags_flagged_field)
5574                 sprintf(set_flags, "%s \\Flagged", set_flags);
5575         else
5576                 sprintf(clear_flags, "%s \\Flagged", clear_flags);
5577
5578         if (mail->flags_forwarded_field)
5579                 sprintf(set_flags, "%s $Forwarded", set_flags);
5580         else
5581                 sprintf(clear_flags, "%s $Forwarded", clear_flags);
5582
5583         if (EM_SAFE_STRLEN(set_flags) > 0)  {
5584                 EM_DEBUG_LOG(">>>> Calling mail_setflag [%s] ", set_flags);
5585                 mail_flag(stream, tmp, set_flags, ST_SET | ST_SILENT);
5586                 EM_DEBUG_LOG(">>>> End mail_setflag ");
5587         }
5588
5589         if (EM_SAFE_STRLEN(clear_flags) > 0)  {
5590                 EM_DEBUG_LOG(">>>> Calling mail_clearflag [%s]", clear_flags);
5591                 mail_clearflag(stream, tmp, clear_flags);
5592                 EM_DEBUG_LOG(">>>> End mail_clearflag ");
5593         }
5594                 
5595         if (mail->lock_status) {
5596                 memset(set_flags, 0x00, 100);
5597                 sprintf(set_flags, "Sticky");
5598                 if (EM_SAFE_STRLEN(set_flags) > 0)  {
5599                         EM_DEBUG_LOG(">>>> Calling mail_setflag [%s]", set_flags);
5600                         mail_flag(stream, tmp, set_flags, ST_SET | ST_SILENT);
5601                         EM_DEBUG_LOG(">>>> End mail_setflag ");
5602                 }
5603         }
5604
5605         EM_DEBUG_LOG(">>>> Returning from emcore_sync_flag_with_server ");
5606
5607         if (!emcore_check_thread_status())  {
5608                 err = EMAIL_ERROR_CANCELLED;
5609                 goto FINISH_OFF;
5610         }
5611
5612         ret = true;
5613
5614 FINISH_OFF: 
5615         
5616         if (stream)
5617                 emcore_close_mailbox(account_id, stream);
5618
5619         if (mail)
5620                 emstorage_free_mail(&mail, 1, NULL);
5621
5622         if (ref_account) {
5623                 emcore_free_account(ref_account);
5624                 EM_SAFE_FREE(ref_account);
5625         }
5626
5627         if (err_code != NULL)
5628                 *err_code = err;
5629         EM_DEBUG_FUNC_END("err [%d]", err);     
5630         return ret;
5631 }
5632
5633 INTERNAL_FUNC int emcore_sync_seen_flag_with_server(int mail_ids[], int num, int *err_code)
5634 {
5635         EM_DEBUG_FUNC_BEGIN("mail_ids[%p], err_code[%p]", mail_ids[0], err_code);
5636
5637         if (mail_ids[0] < 1)  {
5638                 if (err_code != NULL)
5639                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5640                 return false;
5641         }
5642
5643         int ret = false;
5644         int err = EMAIL_ERROR_NONE;
5645         int status = EMAIL_DOWNLOAD_FAIL;
5646         MAILSTREAM *stream = NULL;
5647         email_internal_mailbox_t mailbox;
5648         emstorage_mail_tbl_t *mail = NULL;
5649         email_account_t *ref_account = NULL;
5650         int account_id = 0;
5651         int msgno = 0;
5652         char set_flags[100];
5653         char clear_flags[100];
5654         char tmp[100];
5655         int mail_id = 0;
5656         int i = 0;
5657
5658         memset(&mailbox, 0x00, sizeof(email_mailbox_t));
5659
5660         mail_id = mail_ids[0];
5661
5662         if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail)  {
5663                 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
5664
5665                 goto FINISH_OFF;
5666         }
5667
5668         account_id = mail->account_id;
5669
5670         if (!(ref_account = emcore_get_account_reference(account_id))) {
5671                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
5672                 err = EMAIL_ERROR_INVALID_ACCOUNT;
5673                 goto FINISH_OFF;
5674         }
5675         
5676         /*  open mail server. */
5677         if (!emcore_connect_to_remote_mailbox(account_id, mail->mailbox_id, (void **)&stream, &err) || !stream)  {
5678                 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
5679                 status = EMAIL_LIST_CONNECTION_FAIL;
5680                 goto FINISH_OFF;
5681         }
5682
5683         mailbox.mailbox_name = mail->mailbox_name;
5684         mailbox.account_id = account_id;
5685         mailbox.mail_stream = stream;
5686         
5687         for (i = 0; i < num; i++)  {
5688                 mail_id = mail_ids[i];
5689         
5690                 if (!emcore_check_thread_status())  {
5691                         err = EMAIL_ERROR_CANCELLED;
5692                         goto FINISH_OFF;
5693                 }
5694
5695                 if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail)  {
5696                         EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
5697         
5698                         goto FINISH_OFF;
5699                 }
5700                 
5701                 if (!emcore_check_thread_status())  {
5702                         err = EMAIL_ERROR_CANCELLED;
5703                         goto FINISH_OFF;
5704                 }
5705
5706                 /*  download message number from server. */
5707                 if (!emcore_get_mail_msgno_by_uid(ref_account, &mailbox, mail->server_mail_id, &msgno, &err))  {
5708                         EM_DEBUG_LOG("emcore_get_mail_msgno_by_uid failed message_no  :  %d ", err);
5709                         goto FINISH_OFF;
5710                 }
5711                 
5712                 memset(tmp, 0x00, 100);
5713                 sprintf (tmp, "%d", msgno);
5714
5715                 memset(set_flags, 0x00, 100);
5716                 memset(clear_flags, 0x00, 100);
5717
5718                 if (mail->flags_seen_field)
5719                         sprintf(set_flags, "\\Seen");
5720                 else
5721                         sprintf(clear_flags, "\\Seen");
5722                 EM_DEBUG_LOG("new_flag.seen :  %s ", set_flags);
5723
5724                 if (EM_SAFE_STRLEN(set_flags) > 0)  {
5725                         EM_DEBUG_LOG(">>>> Calling mail_setflag ");
5726                         mail_flag(stream, tmp, set_flags, ST_SET | ST_SILENT);
5727                         EM_DEBUG_LOG(">>>> End mail_setflag ");
5728                 }
5729                 else {
5730                         EM_DEBUG_LOG(">>>> Calling mail_clearflag ");
5731                         mail_clearflag(stream, tmp, clear_flags);
5732                         EM_DEBUG_LOG(">>>> End mail_clearflag ");
5733                 }
5734
5735                 EM_DEBUG_LOG(">>>> Returning from emcore_sync_flag_with_server ");
5736
5737                 if (!emcore_check_thread_status())  {
5738                         err = EMAIL_ERROR_CANCELLED;
5739                         goto FINISH_OFF;
5740                 }
5741         }
5742         ret = true;
5743
5744 FINISH_OFF:
5745         
5746         if (stream) emcore_close_mailbox(account_id, stream);
5747         if (mail) emstorage_free_mail(&mail, 1, NULL);
5748
5749         if (ref_account) {
5750                 emcore_free_account(ref_account);
5751                 EM_SAFE_FREE(ref_account);
5752         }
5753
5754         if (err_code != NULL)
5755                 *err_code = err;
5756         EM_DEBUG_FUNC_END("err [%d]", err);
5757         return ret;
5758 }
5759
5760 INTERNAL_FUNC void emcore_free_mail_data_list(email_mail_data_t **mail_list, int count)
5761 {
5762         EM_DEBUG_FUNC_BEGIN("count[%d]", count);
5763         
5764         if (count <= 0 || !mail_list || !*mail_list)  {
5765                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");                        
5766                 return;
5767         }
5768                 
5769         email_mail_data_t* p = *mail_list;
5770         int i;
5771         
5772         for (i = 0; i < count; i++)
5773                 emcore_free_mail_data( p+i);
5774
5775         EM_SAFE_FREE(*mail_list);
5776
5777         EM_DEBUG_FUNC_END();
5778 }
5779         
5780 INTERNAL_FUNC void emcore_free_mail_data(email_mail_data_t *mail_data)
5781 {
5782         EM_DEBUG_FUNC_BEGIN();
5783
5784         if (!mail_data) {
5785                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_OUT_OF_MEMORY");
5786                 return;
5787         }
5788
5789         EM_SAFE_FREE(mail_data->subject);
5790         EM_SAFE_FREE(mail_data->server_mailbox_name);
5791         EM_SAFE_FREE(mail_data->server_mail_id);
5792         EM_SAFE_FREE(mail_data->message_id);
5793         EM_SAFE_FREE(mail_data->full_address_from);
5794         EM_SAFE_FREE(mail_data->full_address_reply);
5795         EM_SAFE_FREE(mail_data->full_address_to);
5796         EM_SAFE_FREE(mail_data->full_address_cc);
5797         EM_SAFE_FREE(mail_data->full_address_bcc);
5798         EM_SAFE_FREE(mail_data->full_address_return);
5799         EM_SAFE_FREE(mail_data->email_address_sender);
5800         EM_SAFE_FREE(mail_data->email_address_recipient);
5801         EM_SAFE_FREE(mail_data->alias_sender);
5802         EM_SAFE_FREE(mail_data->alias_recipient);
5803         EM_SAFE_FREE(mail_data->file_path_plain);
5804         EM_SAFE_FREE(mail_data->file_path_html);
5805         EM_SAFE_FREE(mail_data->preview_text);
5806
5807         EM_DEBUG_FUNC_END();    
5808 }
5809
5810
5811 INTERNAL_FUNC int emcore_free_attachment_data(email_attachment_data_t **attachment_data_list, int attachment_data_count, int *err_code)
5812 {
5813         EM_DEBUG_FUNC_BEGIN("attachment_data_list[%p], attachment_data_count [%d], err_code[%p]", attachment_data_list, attachment_data_count, err_code);       
5814         
5815         if (!attachment_data_list || !*attachment_data_list)  {
5816                 if (err_code != NULL)
5817                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5818                 return false;
5819         }
5820         
5821         email_attachment_data_t* p = *attachment_data_list;
5822         int i = 0;
5823         
5824         for (i = 0; i < attachment_data_count; i++) {
5825                 EM_SAFE_FREE(p[i].attachment_name);
5826                 EM_SAFE_FREE(p[i].attachment_path);
5827                 EM_SAFE_FREE(p[i].attachment_mime_type);
5828         }
5829
5830         EM_SAFE_FREE(p); *attachment_data_list = NULL;
5831
5832         if(err_code)
5833                 *err_code = EMAIL_ERROR_NONE;
5834         
5835         EM_DEBUG_FUNC_END();    
5836         return true;
5837 }
5838
5839
5840
5841 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
5842
5843 INTERNAL_FUNC int emcore_delete_pbd_activity(int account_id, int mail_id, int activity_id, int *err_code) 
5844 {
5845         EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], err_code[%p]", account_id, mail_id, err_code);
5846
5847         if (account_id < FIRST_ACCOUNT_ID || mail_id < 0) {
5848                 EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d], err_code[%p]", account_id, mail_id, err_code);
5849                 if (err_code != NULL)
5850                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5851                 return false;
5852         }
5853
5854         int ret = false;
5855         int err = EMAIL_ERROR_NONE;
5856         
5857         emstorage_begin_transaction(NULL, NULL, NULL);
5858         
5859         if (!emstorage_delete_pbd_activity(account_id, mail_id, activity_id, false, &err))  {
5860                 EM_DEBUG_EXCEPTION("emstorage_delete_pbd_activity failed [%d]", err);
5861                 goto FINISH_OFF;
5862         }
5863
5864         ret = true;
5865
5866 FINISH_OFF: 
5867         if (ret == true) {      /*  COMMIT TRANSACTION; */
5868                 if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
5869                         err = EMAIL_ERROR_DB_FAILURE;
5870                         ret = false;
5871                 }
5872         }
5873         else {  /*  ROLLBACK TRANSACTION; */
5874                 if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)
5875                         err = EMAIL_ERROR_DB_FAILURE;
5876         }       
5877         
5878         if (err_code != NULL)
5879                 *err_code = err;
5880         EM_DEBUG_FUNC_END();
5881         return ret;
5882 }
5883
5884 INTERNAL_FUNC int emcore_insert_pbd_activity(email_event_partial_body_thd *local_activity, int *activity_id, int *err_code) 
5885 {
5886         EM_DEBUG_FUNC_BEGIN("local_activity[%p], activity_id[%p], err_code[%p]", local_activity, activity_id, err_code);
5887
5888         if (!local_activity || !activity_id) {
5889                 EM_DEBUG_EXCEPTION("local_activity[%p], activity_id[%p] err_code[%p]", local_activity, activity_id, err_code);
5890                 if (err_code != NULL)
5891                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5892                 return false;
5893         }
5894         
5895         int ret = false;
5896         int err = EMAIL_ERROR_NONE;
5897         
5898         emstorage_begin_transaction(NULL, NULL, NULL);
5899         
5900         if (!emstorage_add_pbd_activity(local_activity, activity_id, false, &err))  {
5901                 EM_DEBUG_EXCEPTION("emstorage_add_pbd_activity failed [%d]", err);
5902                 
5903
5904                 goto FINISH_OFF;
5905         }
5906
5907         ret = true;
5908
5909 FINISH_OFF: 
5910         if (ret == true) {      /*  COMMIT TRANSACTION; */
5911                 if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
5912                         err = EMAIL_ERROR_DB_FAILURE;
5913                         ret = false;
5914                 }
5915         }
5916         else {  /*  ROLLBACK TRANSACTION; */
5917                 if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)
5918                         err = EMAIL_ERROR_DB_FAILURE;
5919         }       
5920         
5921         if (err_code != NULL)
5922                 *err_code = err;
5923         EM_DEBUG_FUNC_END();
5924         return ret;
5925 }
5926
5927 #endif
5928
5929 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
5930
5931 /* API to set or unset a field of flags on server in single IMAP request to server */
5932
5933 INTERNAL_FUNC int emcore_sync_flags_field_with_server(int mail_ids[], int num, email_flags_field_type field_type, int value, int *err_code)
5934 {
5935         EM_DEBUG_FUNC_BEGIN("mail_ids [%p], num [%d], field_type [%d], value [%d], err_code [%p]", mail_ids, num, field_type, value, err_code);
5936                 
5937         if (NULL == mail_ids || num <= 0 || field_type < 0 || field_type >= EMAIL_FLAGS_FIELD_COUNT)  {
5938                         EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5939                 if (err_code != NULL) {
5940                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5941                 }
5942                 return false;
5943         }
5944         
5945         MAILSTREAM *stream = NULL;
5946         IMAPLOCAL *imaplocal = NULL;
5947         char tag[MAX_TAG_SIZE] = {0, };
5948         char cmd[MAX_IMAP_COMMAND_LENGTH] = {0, };
5949         char *p = NULL;
5950         char **string_list = NULL;      
5951         int ret = false;
5952         int err = EMAIL_ERROR_NONE;
5953         int command_success = false;
5954         int account_id = 0;
5955         int mail_id = 0;
5956         int i = 0;
5957         int id_set_count = 0;
5958         int len_of_each_range = 0;
5959         int string_count = 0;
5960         email_account_t *temp_account = NULL;
5961         email_id_set_t *id_set = NULL;
5962         emstorage_mail_tbl_t *mail = NULL;
5963         email_uid_range_set *uid_range_set = NULL;
5964         email_uid_range_set *uid_range_node = NULL;
5965         char *field_type_name[EMAIL_FLAGS_FIELD_COUNT] = { "\\Seen"
5966                 , "\\Deleted", "\\Flagged", "\\Answered"
5967                 , "\\Recent", "\\Draft", "$Forwarded" }; 
5968         
5969         mail_id = mail_ids[0];
5970         
5971         if (!emstorage_get_mail_field_by_id(mail_id, RETRIEVE_FLAG, &mail, true, &err) || !mail)                /*To DO :  This is a existing bug. on mail deletion before this call it will fail always */ {
5972                 EM_DEBUG_LOG("emstorage_get_mail_by_id failed [%d]", err);
5973
5974                 goto FINISH_OFF;
5975         }
5976
5977         account_id = mail[0].account_id;
5978
5979         temp_account = emcore_get_account_reference(account_id);
5980
5981         if (!temp_account)   {
5982                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
5983                 err = EMAIL_ERROR_INVALID_ACCOUNT;
5984                 goto FINISH_OFF;
5985         }
5986         
5987         if (temp_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
5988                 EM_DEBUG_EXCEPTION("Syncing seen flag is available only for IMAP4 server. The server type [%d]", temp_account->incoming_server_type);
5989                 err = EMAIL_ERROR_NOT_SUPPORTED;
5990                 goto FINISH_OFF;
5991         }
5992         
5993         if (!emcore_connect_to_remote_mailbox(account_id, mail->mailbox_id, (void **)&stream, &err) || !stream)  {
5994                 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
5995                 goto FINISH_OFF;
5996         }
5997
5998         if (false == emstorage_free_mail(&mail, 1, &err)) {
5999                 EM_DEBUG_EXCEPTION("emstorage_free_mail failed - %d ", err);
6000                 goto FINISH_OFF;
6001         }
6002
6003         /* [h.gahlaut] Break the set of mail_ids into comma separated strings of given length */
6004         /* Length is decided on the basis of remaining keywords in the Query to be formed later on in emstorage_get_id_set_from_mail_ids */
6005         /* Here about 90 bytes are required for fixed keywords in the query-> SELECT local_uid, s_uid from mail_read_mail_uid_tbl where local_uid in (....) ORDER by s_uid */
6006         /* So length of comma separated strings which will be filled in (.....) in above query can be maximum QUERY_SIZE - 90 */
6007
6008         if (false == emcore_form_comma_separated_strings(mail_ids, num, QUERY_SIZE - 90, &string_list, &string_count, &err))   {
6009                 EM_DEBUG_EXCEPTION("emcore_form_comma_separated_strings failed [%d]", err);
6010                 goto FINISH_OFF;
6011         }
6012
6013         /* Now execute one by one each comma separated string of mail_ids */
6014
6015         for (i = 0; i < string_count; ++i) {
6016                 /* Get the set of mail_ds and corresponding server_mail_ids sorted by server mail ids in ascending order */
6017
6018                 if (false == emstorage_get_id_set_from_mail_ids(string_list[i], &id_set, &id_set_count, &err)) {
6019                         EM_DEBUG_EXCEPTION("emstorage_get_id_set_from_mail_ids failed [%d]", err);
6020                         goto FINISH_OFF;
6021                 }
6022                 
6023                 /* Convert the sorted sequence of server mail ids to range sequences of given length. A range sequence will be like A : B, C, D: E, H */
6024                 
6025                 len_of_each_range = MAX_IMAP_COMMAND_LENGTH - 40;               /*  1000 is the maximum length allowed RFC 2683. 40 is left for keywords and tag. */
6026                 
6027                 if (false == emcore_convert_to_uid_range_set(id_set, id_set_count, &uid_range_set, len_of_each_range, &err)) {
6028                         EM_DEBUG_EXCEPTION("emcore_convert_to_uid_range_set failed [%d]", err);
6029                         goto FINISH_OFF;
6030                 }
6031
6032                 uid_range_node = uid_range_set;
6033
6034                 while (uid_range_node != NULL) {
6035                         /* Remove comma from end of uid_range */
6036
6037                         uid_range_node->uid_range[EM_SAFE_STRLEN(uid_range_node->uid_range) - 1] = '\0';
6038                         
6039                         /* Form the IMAP command */
6040
6041                         SNPRINTF(tag, MAX_TAG_SIZE, "%08lx", 0xffffffff & (stream->gensym++));
6042
6043                         if (value)
6044                                 SNPRINTF(cmd, MAX_IMAP_COMMAND_LENGTH, "%s UID STORE %s +FLAGS (%s)\015\012", tag, uid_range_node->uid_range, field_type_name[field_type]);
6045                         else
6046                                 SNPRINTF(cmd, MAX_IMAP_COMMAND_LENGTH, "%s UID STORE %s -FLAGS (%s)\015\012", tag, uid_range_node->uid_range, field_type_name[field_type]);
6047
6048                         EM_DEBUG_LOG("[IMAP4] command %s", cmd);
6049
6050                         /* send command */
6051
6052                         if (!(imaplocal = stream->local) || !imaplocal->netstream)  {
6053                                 EM_DEBUG_EXCEPTION("invalid IMAP4 stream detected...");
6054                                 
6055                                 err = EMAIL_ERROR_UNKNOWN;              
6056                                 goto FINISH_OFF;
6057                         }
6058                 
6059                         if (!net_sout(imaplocal->netstream, cmd, (int)EM_SAFE_STRLEN(cmd))) {
6060                                 EM_DEBUG_EXCEPTION("net_sout failed...");
6061                                 err = EMAIL_ERROR_CONNECTION_BROKEN;            
6062                                 goto FINISH_OFF;
6063                         }
6064
6065                         /* Receive Response */
6066
6067                         command_success = false;
6068                         
6069                         while (imaplocal->netstream) {  
6070                                 if (!(p = net_getline(imaplocal->netstream)))  {
6071                                         EM_DEBUG_EXCEPTION("net_getline failed...");
6072                                 
6073                                         err = EMAIL_ERROR_INVALID_RESPONSE;     
6074                                         goto FINISH_OFF;
6075                                 }
6076                         
6077                                 EM_DEBUG_LOG("[IMAP4 Response ] %s", p);
6078                                 
6079                                 if (!strncmp(p, tag, EM_SAFE_STRLEN(tag)))  {
6080                                         if (!strncmp(p + EM_SAFE_STRLEN(tag) + 1, "OK", 2))  {
6081                                                 /*Delete all local activities */
6082                                                 command_success = true;
6083
6084                                                 EM_SAFE_FREE(p);        
6085                                                 break;
6086                                         }
6087                                         else  {
6088                                                 /* 'NO' or 'BAD' */
6089                                                 command_success = false;
6090                                                 err = EMAIL_ERROR_IMAP4_STORE_FAILURE;          
6091                                                 EM_SAFE_FREE(p);
6092                                                 goto FINISH_OFF;
6093                                         }               
6094                                 }
6095                                 
6096                                 EM_SAFE_FREE(p);                
6097                         }
6098
6099                         uid_range_node = uid_range_node->next;
6100                 }       
6101
6102                 emcore_free_uid_range_set(&uid_range_set);
6103
6104                 EM_SAFE_FREE(id_set);
6105                 
6106                 id_set_count = 0;
6107         }
6108
6109         ret = true;
6110
6111 FINISH_OFF: 
6112
6113 #ifdef __FEATURE_LOCAL_ACTIVITY__
6114         if (ret) {
6115                 emstorage_activity_tbl_t new_activity;
6116                 for (i = 0; i<num ; i++) {              
6117                         memset(&new_activity, 0x00, sizeof(emstorage_activity_tbl_t));
6118                         new_activity.activity_type = ACTIVITY_MODIFYSEENFLAG;
6119                         new_activity.account_id    = account_id;
6120                         new_activity.mail_id       = mail_ids[i];
6121                 
6122                         if (!emcore_delete_activity(&new_activity, &err))
6123                                 EM_DEBUG_EXCEPTION("Local Activity ACTIVITY_MOVEMAIL [%d] ", err);
6124                 }
6125
6126         }
6127 #endif
6128
6129         if (NULL != mail) {
6130                 if (false == emstorage_free_mail(&mail, 1, &err))                       
6131                         EM_DEBUG_EXCEPTION("emstorage_free_mail failed - %d ", err);
6132         }
6133         
6134         emcore_free_comma_separated_strings(&string_list, &string_count);
6135
6136         if (false == ret)
6137                 emcore_free_uid_range_set(&uid_range_set);
6138
6139         emcore_close_mailbox(0, stream);
6140         stream = NULL;
6141
6142         if (temp_account) {
6143                 emcore_free_account(temp_account);
6144                 EM_SAFE_FREE(temp_account);
6145         }
6146
6147         if (err_code != NULL)
6148                 *err_code = err;
6149         EM_DEBUG_FUNC_END();
6150         return ret;
6151 }
6152 #endif
6153
6154 INTERNAL_FUNC int emcore_mail_filter_by_rule(email_rule_t *filter_info, int *err_code)
6155 {
6156         EM_DEBUG_FUNC_BEGIN("filter_info[%p], err_code[%p]", filter_info, err_code);
6157
6158         int ret = false, err = EMAIL_ERROR_NONE;
6159         int account_index, account_count, mail_id_index = 0;
6160         email_account_t *account_ref = NULL, *account_list_ref = NULL;
6161         int filtered_mail_id_count = 0, *filtered_mail_id_list = NULL, parameter_string_length = 0;
6162         char *parameter_string = NULL, mail_id_string[10] = { 0x00, };
6163         emstorage_mailbox_tbl_t *spam_mailbox = NULL;
6164
6165         if (!filter_info)  {
6166                 EM_DEBUG_EXCEPTION("filter_info[%p]", filter_info);
6167                 err = EMAIL_ERROR_INVALID_PARAM;
6168                 goto FINISH_OFF;
6169         }
6170
6171         if (!emcore_get_account_reference_list(&account_list_ref, &account_count, &err)) {
6172                 EM_DEBUG_EXCEPTION("emcore_get_account_reference_list failed [%d]", err);
6173                 goto FINISH_OFF;
6174         }
6175
6176         for (account_index = 0; account_index < account_count; account_index++) {
6177                 account_ref = account_list_ref + account_index;
6178
6179                 if (!emstorage_get_mailbox_by_mailbox_type(account_ref->account_id, EMAIL_MAILBOX_TYPE_SPAMBOX, &spam_mailbox, false, &err))
6180                         EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_mailbox_type for account_id[%d] failed [%d]", account_ref->account_id, err);
6181                 else if (spam_mailbox && spam_mailbox->mailbox_id > 0) {
6182                         if (!emstorage_filter_mails_by_rule(account_ref->account_id, spam_mailbox->mailbox_id, (emstorage_rule_tbl_t *)filter_info, &filtered_mail_id_list, &filtered_mail_id_count, &err))
6183                                 EM_DEBUG_EXCEPTION("emstorage_filter_mails_by_rule failed [%d]", err);
6184                         else {
6185                                 if (filtered_mail_id_count) {
6186                                         parameter_string_length = 10 /*mailbox_id length*/ + 7 + (10 * filtered_mail_id_count);
6187                                         parameter_string = em_malloc(sizeof(char) * parameter_string_length);
6188
6189                                         if (parameter_string == NULL) {
6190                                                 err = EMAIL_ERROR_OUT_OF_MEMORY;
6191                                                 EM_DEBUG_EXCEPTION("em_malloc failed for parameter_string");
6192                                                 goto FINISH_OFF;
6193                                         }
6194                                         SNPRINTF(parameter_string, parameter_string_length, "[NA]%c%d%c", 0x01, spam_mailbox->mailbox_id, 0x01);
6195                                         
6196                                         for (mail_id_index = 0; mail_id_index < filtered_mail_id_count; mail_id_index++) {
6197                                                 memset(mail_id_string, 0, 10);
6198                                                 SNPRINTF(mail_id_string, 10, "%d", filtered_mail_id_list[mail_id_index]);
6199                                                 strcat(parameter_string, mail_id_string);
6200                                                 strcat(parameter_string, ",");
6201                                         }
6202
6203                                         EM_DEBUG_LOG("filtered_mail_id_count [%d]", filtered_mail_id_count);
6204                                         EM_DEBUG_LOG("param string [%s]", parameter_string);
6205
6206                                         if (!emcore_notify_storage_event(NOTI_MAIL_MOVE, account_ref->account_id, 0, parameter_string, 0)) 
6207                                                 EM_DEBUG_EXCEPTION("emcore_notify_storage_event failed [ NOTI_MAIL_MOVE ] >>>> ");
6208
6209                                         EM_SAFE_FREE(filtered_mail_id_list);
6210                                         EM_SAFE_FREE(parameter_string);
6211                                 }
6212                         }
6213                         emstorage_free_mailbox(&spam_mailbox, 1, &err);
6214                         spam_mailbox = NULL;
6215                 }
6216         }
6217
6218         emcore_display_unread_in_badge();
6219
6220         ret = true;
6221
6222 FINISH_OFF: 
6223         EM_SAFE_FREE(account_list_ref);
6224         EM_SAFE_FREE(filtered_mail_id_list);
6225         EM_SAFE_FREE(parameter_string);
6226
6227
6228         if (spam_mailbox)
6229                 emstorage_free_mailbox(&spam_mailbox, 1, &err);
6230
6231         if (err_code)
6232                 *err_code = err;
6233         EM_DEBUG_FUNC_END();
6234         return ret;
6235 }
6236