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