apply removal of mailbox_name field
[platform/core/messaging/email-service.git] / email-core / email-core-utils.c
1 /*
2 *  email-service
3 *
4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5 *
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 */
21
22
23 /******************************************************************************
24  * File: email-core-utils.c
25  * Desc: Mail Utils
26  *
27  * Auth:
28  *
29  * History:
30  *      2006.08.16 : created
31  *****************************************************************************/
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <ctype.h>
37 #include <errno.h>
38 #include <dlfcn.h>
39 #include <glib.h>
40 #include <glib-object.h>
41 #include <sys/time.h>
42 #include <unistd.h>
43 #include <sys/vfs.h>
44 #include <sys/stat.h>
45 #include <vconf.h>
46 #include <regex.h>
47 #include <pthread.h>
48 #include <notification.h>
49 #include <badge.h>
50
51 #include "email-types.h"
52 #include "email-core-global.h"
53 #include "email-core-utils.h"
54 #include "email-debug-log.h"
55 #include "email-core-mail.h"
56 #include "email-core-event.h"
57 #include "email-core-mailbox.h"
58 #include "email-core-account.h"
59 #include "email-core-mailbox-sync.h"
60 #include "email-core-mime.h"
61 #include "email-core-sound.h"
62 #include "email-core-signal.h"
63 #include "email-utilities.h"
64 #include "email-convert.h"
65 #include "email-internal-types.h"
66
67 #ifdef __FEATURE_DRIVING_MODE__
68 #include <app_service.h>
69 #endif /* __FEATURE_DRIVING_MODE__ */
70
71 #define LED_TIMEOUT_SECS          12
72 #define G_DISPLAY_LENGTH          256
73
74 #define DIR_SEPERATOR_CH          '/'
75 #define EMAIL_CH_QUOT             '"'
76 #define EMAIL_CH_BRACKET_S        '<'
77 #define EMAIL_CH_BRACKET_E        '>'
78 #define EMAIL_CH_COMMA            ','
79 #define EMAIL_CH_SEMICOLON        ';'
80 #define EMAIL_CH_ROUND_BRACKET_S  '('
81 #define EMAIL_CH_ROUND_BRACKET_E  ')'
82 #define EMAIL_CH_SQUARE_BRACKET_S '['
83 #define EMAIL_CH_SQUARE_BRACKET_E ']'
84 #define EMAIL_CH_SPACE            ' '
85 #define EMAIL_NOTI_ICON_PATH      EMAILPATH"/res/image/Q02_Notification_email.png"
86
87 typedef struct  _em_transaction_info_type_t {
88         int mail_id;
89         int     handle;
90         struct _em_transaction_info_type_t *next;
91
92 } em_transaction_info_type_t;
93
94 em_transaction_info_type_t  *g_transaction_info_list;
95
96 static email_session_t g_session_list[SESSION_MAX] = { {0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0},};
97
98 typedef struct emcore_account_list_t emcore_account_list_t;
99 struct emcore_account_list_t {
100         email_account_t *account;
101         emcore_account_list_t *next;
102 };
103
104 INTERNAL_FUNC char *emcore_convert_mutf7_to_utf8(char *mailbox_name)
105 {
106         EM_DEBUG_FUNC_BEGIN();
107         return (char *)(utf8_from_mutf7((unsigned char *)mailbox_name));
108 }
109
110 /*  in smtp case, path argument must be ENCODED_PATH_SMTP */
111 int emcore_get_long_encoded_path_with_account_info(email_account_t *account, char *path, int delimiter, char **long_enc_path, int *err_code)
112 {
113         EM_PROFILE_BEGIN(emCorelongEncodedpath);
114         EM_DEBUG_FUNC_BEGIN("account[%p], path[%s], delimiter[%d], long_enc_path[%p], err_code[%p]", account, path, delimiter, long_enc_path, err_code);
115
116         int ret = false;
117         int error = EMAIL_ERROR_NONE;
118         char *p = NULL;
119
120         size_t long_enc_path_len = 0;
121
122         if (path == NULL || (path && strncmp(path, ENCODED_PATH_SMTP, EM_SAFE_STRLEN(ENCODED_PATH_SMTP)) != 0)) {               /*  imap or pop3 */
123                 EM_DEBUG_LOG("account->incoming_server_address[%p]", account->incoming_server_address);
124                 EM_DEBUG_LOG("account->incoming_server_address[%s]", account->incoming_server_address);
125
126                 if (!account->incoming_server_address) {
127                         EM_DEBUG_EXCEPTION("account->incoming_server_address is null");
128                         error = EMAIL_ERROR_INVALID_ACCOUNT;
129                         goto FINISH_OFF;
130                 }
131
132                 long_enc_path_len = EM_SAFE_STRLEN(account->incoming_server_address) + EM_SAFE_STRLEN(path) + 64; /*prevent 34357*/
133
134                 *long_enc_path = em_malloc(long_enc_path_len);
135                 if (!*long_enc_path)  {
136                         EM_DEBUG_EXCEPTION("malloc failed...");
137                         error = EMAIL_ERROR_OUT_OF_MEMORY;
138                         goto FINISH_OFF;
139                 }
140
141                 p = *long_enc_path;
142
143                 /*  ex:"{mai.test.com:143/imap} or {mai.test.com:143/imap/tls}my-mailbox" */
144
145                 SNPRINTF(p, long_enc_path_len, "{%s:%d/%s/user=%d",
146                         account->incoming_server_address,
147                         account->incoming_server_port_number,
148                         account->incoming_server_type == EMAIL_SERVER_TYPE_POP3 ? "pop3" : "imap", account->account_id);
149
150                 if (account->incoming_server_secure_connection & 0x01)  {
151                         strncat(p, "/ssl", long_enc_path_len-(EM_SAFE_STRLEN(p)+1));
152                         /* strcat(p, "/tryssl"); */
153                 }
154
155                 /* Currently, receiving servers doesn't require tls.
156                 if (account->incoming_server_secure_connection & 0x02)
157                         strncat(p, "/tls", long_enc_path_len-(EM_SAFE_STRLEN(p)+1));
158                 else
159                         strncat(p, "/notls", long_enc_path_len-(EM_SAFE_STRLEN(p)+1));
160                 */
161
162                 if (account->incoming_server_requires_apop) {
163                         EM_DEBUG_LOG("emcore_get_long_encoded_path - incoming_server_requires_apop - %d", account->incoming_server_requires_apop);
164                         strncat(p, "/apop", long_enc_path_len-(EM_SAFE_STRLEN(p)+1));
165                         EM_DEBUG_LOG("long_enc_path - %s", p);
166                 }
167         }
168         else  {         /*  smtp */
169                 long_enc_path_len = EM_SAFE_STRLEN(account->outgoing_server_address) + 64;
170
171                 *long_enc_path = em_malloc(EM_SAFE_STRLEN(account->outgoing_server_address) + 64);
172                 if (!*long_enc_path) {
173                         EM_DEBUG_EXCEPTION("\t malloc failed...\n");
174
175                         error = EMAIL_ERROR_OUT_OF_MEMORY;
176                         goto FINISH_OFF;
177                 }
178
179                 p = *long_enc_path;
180
181                 /*  ex:"mail.test.com:25/smtp" */
182
183                 SNPRINTF(p, long_enc_path_len, "%s:%d/%s",
184                         account->outgoing_server_address,
185                         account->outgoing_server_port_number,
186                         "smtp");
187
188                 if (account->outgoing_server_need_authentication) {
189                         SNPRINTF(p + EM_SAFE_STRLEN(p), long_enc_path_len-(EM_SAFE_STRLEN(p)), "/user=%d", account->account_id);
190                 }
191
192                 if (account->outgoing_server_secure_connection & 0x01) {
193                         strncat(p, "/ssl", long_enc_path_len-(EM_SAFE_STRLEN(p)+1));
194                         /* strcat(p, "/tryssl"); */
195                 }
196                 if (account->outgoing_server_secure_connection & 0x02)
197                         strncat(p, "/tls", long_enc_path_len-(EM_SAFE_STRLEN(p)+1));
198                 else
199                         strncat(p, "/notls", long_enc_path_len-(EM_SAFE_STRLEN(p)+1));
200         }
201
202         if (path == NULL || (path && strncmp(path, ENCODED_PATH_SMTP, EM_SAFE_STRLEN(ENCODED_PATH_SMTP)) != 0)) {
203                 strncat(p, "}", long_enc_path_len-(EM_SAFE_STRLEN(p)+1));
204
205                 if (path != NULL) {
206                         char *enc_name = NULL;
207
208                         if (!emcore_get_encoded_mailbox_name(path, &enc_name, &error))  {
209                                 EM_DEBUG_EXCEPTION("emcore_get_encoded_mailbox_name failed - %d", error);
210                                 *long_enc_path = NULL;
211                                 goto FINISH_OFF;
212                         }
213
214                         if (enc_name)  {
215                                 strncat(p, enc_name, long_enc_path_len-(EM_SAFE_STRLEN(p)+1));
216                                 EM_SAFE_FREE(enc_name);
217                         }
218                 }
219         }
220
221         ret = true;
222
223 FINISH_OFF:
224         if (ret != true)
225                 EM_SAFE_FREE(p);
226
227         if (err_code != NULL)
228                 *err_code = error;
229         EM_PROFILE_END(emCorelongEncodedpath);
230         return ret;
231 }
232
233 int emcore_get_long_encoded_path(int account_id, char *path, int delimiter, char **long_enc_path, int *err_code)
234 {
235         EM_PROFILE_BEGIN(emCorelongEncodedpath);
236         EM_DEBUG_FUNC_BEGIN("account_id[%d], delimiter[%d], long_enc_path[%p], err_code[%p]", account_id, delimiter, long_enc_path, err_code);
237
238         int ret = false;
239         int error = EMAIL_ERROR_NONE;
240         email_account_t *ref_account = NULL;
241
242         ref_account = emcore_get_account_reference(account_id);
243         if (!ref_account)  {
244                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
245                 error = EMAIL_ERROR_INVALID_ACCOUNT;
246                 goto FINISH_OFF;
247         }
248
249         if (emcore_get_long_encoded_path_with_account_info(ref_account, path, delimiter, long_enc_path, &error) == false) {
250                 EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path_with_account_info failed [%d]", error);
251                 goto FINISH_OFF;
252         }
253
254         ret = true;
255
256 FINISH_OFF:
257
258         if (ref_account) {
259                 emcore_free_account(ref_account);
260                 EM_SAFE_FREE(ref_account);
261         }
262
263         if (err_code != NULL)
264                 *err_code = error;
265         EM_PROFILE_END(emCorelongEncodedpath);
266         return ret;
267 }
268
269 int emcore_get_encoded_mailbox_name(char *name, char **enc_name, int *err_code)
270 {
271         EM_DEBUG_FUNC_BEGIN("name[%s], enc_name[%p], err_code[%p]", name, enc_name, err_code);
272
273         if (!name || !enc_name)  {
274                 if (err_code != NULL)
275                         *err_code = EMAIL_ERROR_INVALID_PARAM;
276                 EM_DEBUG_FUNC_END();
277                 return false;
278         }
279
280         /* encoding mailbox name (Charset->UTF8->UTF7) */
281
282         *enc_name = em_malloc(EM_SAFE_STRLEN(name)+1);
283         if (*enc_name == NULL) {
284                 EM_DEBUG_EXCEPTION("malloc failed...");
285                 if (err_code != NULL)
286                         *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
287                 EM_DEBUG_FUNC_END();
288                 return false;
289         }
290
291         strcpy(*enc_name, name);
292
293         if (err_code != NULL)
294                 *err_code = EMAIL_ERROR_NONE;
295
296         EM_DEBUG_FUNC_END();
297         return true;
298 }
299
300 int emcore_get_temp_file_name(char **filename, int *err_code)
301 {
302         EM_DEBUG_FUNC_BEGIN("filename[%p], err_code[%p]", filename, err_code);
303
304         int ret = false;
305         int error = EMAIL_ERROR_NONE;
306
307         if (filename == NULL) {
308                 EM_DEBUG_EXCEPTION("\t filename[%p]\n", filename);
309                 error = EMAIL_ERROR_INVALID_PARAM;
310                 goto FINISH_OFF;
311         }
312
313         char tempname[512] = {0x00, };
314         struct timeval tv;
315
316
317         gettimeofday(&tv, NULL);
318         srand(tv.tv_usec);
319
320         /* Create Directory If deleted by user*/
321         emstorage_create_dir_if_delete();
322
323         SNPRINTF(tempname, sizeof(tempname), "%s%c%d", MAILTEMP, DIR_SEPERATOR_CH, rand());
324
325         char *p = EM_SAFE_STRDUP(tempname);
326         if (p == NULL) {
327                 EM_DEBUG_EXCEPTION("\t strdup failed...\n");
328                 error = EMAIL_ERROR_OUT_OF_MEMORY;
329                 goto FINISH_OFF;
330         }
331
332         *filename = p;
333
334         ret = true;
335
336 FINISH_OFF:
337         if (err_code != NULL)
338                 *err_code = error;
339         EM_DEBUG_FUNC_END();
340         return ret;
341 }
342
343 int emcore_get_file_name(char *path, char **filename, int *err_code)
344 {
345         EM_DEBUG_FUNC_BEGIN("path[%s], filename[%p], err_code[%p]", path, filename, err_code);
346
347         int ret = false;
348         int error = EMAIL_ERROR_NONE;
349
350         if (!path || !filename) {
351                 EM_DEBUG_EXCEPTION("path[%p], filename[%p]", path, filename);
352
353                 error = EMAIL_ERROR_INVALID_PARAM;
354                 goto FINISH_OFF;
355         }
356
357         int i = (int)EM_SAFE_STRLEN(path);
358
359         /*  get filename */
360         for (; i >= 0; i--)
361                 if (path[i] == DIR_SEPERATOR_CH)
362                         break;
363
364         *filename = path + i + 1;
365
366         ret = true;
367
368 FINISH_OFF:
369                 if (err_code != NULL)
370                 *err_code = error;
371         EM_DEBUG_FUNC_END();
372         return ret;
373 }
374
375 int emcore_get_file_size(char *path, int *size, int *err_code)
376 {
377         EM_DEBUG_FUNC_BEGIN("path[%s], size[%p], err_code[%p]", path, size, err_code);
378
379         int ret = false;
380         int error = EMAIL_ERROR_NONE;
381
382         if ((path == NULL) || (size == NULL)) {
383                 EM_DEBUG_EXCEPTION("\t path[%p], size[%p]\n", path, size);
384
385                 error = EMAIL_ERROR_INVALID_PARAM;
386                 goto FINISH_OFF;
387         }
388
389         struct stat st_buf;
390
391         if (stat(path, &st_buf) < 0)  {
392                 EM_DEBUG_EXCEPTION("\t stat failed - %s\n", path);
393
394                 error = EMAIL_ERROR_SYSTEM_FAILURE;
395                 goto FINISH_OFF;
396         }
397
398         *size = st_buf.st_size;
399
400         ret = true;
401
402 FINISH_OFF:
403         if (err_code != NULL)
404                 *err_code = error;
405         EM_DEBUG_FUNC_END();
406         return ret;
407 }
408
409
410
411 static int _emcore_check_host(char *host)
412 {
413         if (!host)
414                 return 0;
415         return strncmp(host, ".SYNTAX-ERROR.", strlen(".SYNTAX-ERROR."));
416 }
417
418
419
420 int emcore_get_address_count(char *addr_str, int *count, int *err_code)
421 {
422         EM_DEBUG_FUNC_BEGIN("addr_str[%s], count[%p], err_code[%p]", addr_str, count, err_code);
423
424         int ret = false;
425         int error = EMAIL_ERROR_NONE;
426
427         ADDRESS *addr = NULL;
428         ADDRESS *p_addr = NULL;
429         int i = 0, j;
430         char *p = NULL;
431
432
433         if (!count)  {
434                 EM_DEBUG_EXCEPTION("addr_str[%s], count[%p]", addr_str, count);
435                 error = EMAIL_ERROR_INVALID_PARAM;
436                 goto FINISH_OFF;
437         }
438
439         if (addr_str != NULL)  {
440                 em_skip_whitespace(addr_str, &p);
441                 EM_DEBUG_LOG("em_skip_whitespace[p][%s]", p);
442
443
444                 for (i = 0, j = EM_SAFE_STRLEN(p); i < j; i++)
445                         if (p[i] == ';') p[i] = ',';
446                 rfc822_parse_adrlist(&addr, p, NULL);
447                 EM_SAFE_FREE(p);
448
449
450                 for (p_addr = addr, i = 0; p_addr; p_addr = p_addr->next, i++)  {
451                         if (p_addr->mailbox && p_addr->host) {
452                                 if (!strncmp(p_addr->mailbox, "UNEXPECTED_DATA_AFTER_ADDRESS", strlen("UNEXPECTED_DATA_AFTER_ADDRESS"))
453                                 || !strncmp(p_addr->mailbox, "INVALID_ADDRESS", strlen("INVALID_ADDRESS"))
454                                 || !strncmp(p_addr->host, ".SYNTAX-ERROR.", strlen(".SYNTAX-ERROR."))) { /*prevent 34356*/
455                                         EM_DEBUG_LOG("Invalid address ");
456                                         continue;
457                                 }
458                         }
459                         if ((!p_addr->mailbox) || (_emcore_check_host(p_addr->host) == 0)) {
460                                 EM_DEBUG_EXCEPTION("\t invalid address : mailbox[%s], host[%s]\n", p_addr->mailbox, p_addr->host);
461
462                                 error = EMAIL_ERROR_INVALID_ADDRESS;
463                                 /* goto FINISH_OFF; */
464                         }
465                 }
466         }
467
468         *count = i;
469         if (error != EMAIL_ERROR_INVALID_ADDRESS)
470         ret = true;
471
472 FINISH_OFF:
473         if (addr)
474                 mail_free_address(&addr);
475
476         if (err_code != NULL)
477                 *err_code = error;
478         EM_DEBUG_FUNC_END();
479         return ret;
480 }
481
482 INTERNAL_FUNC int emcore_set_network_error(int err_code)
483 {
484         email_session_t *session = NULL;
485
486         EM_DEBUG_FUNC_BEGIN();
487
488         emcore_get_current_session(&session);
489
490         if (!session)
491                 return false;
492
493         session->network = err_code;
494         EM_DEBUG_FUNC_END();
495         return true;
496 }
497
498 int emcore_get_empty_session(email_session_t **session)
499 {
500         EM_DEBUG_FUNC_BEGIN("session[%p]", session);
501
502         /*  lock()... */
503
504         int i;
505
506         for (i = 0; i < SESSION_MAX; i++)  {
507                 if (!g_session_list[i].status)  {
508                         memset(g_session_list+i, 0x00, sizeof(email_session_t));
509                         g_session_list[i].tid = GPOINTER_TO_INT(THREAD_SELF());
510                         g_session_list[i].status = true;
511                         break;
512                 }
513         }
514
515         /*  unlock()... */
516
517         if (session != NULL)
518                 *session = (i != SESSION_MAX) ? &g_session_list[i] : NULL;
519         EM_DEBUG_FUNC_END();
520         return (i != SESSION_MAX) ? true : false;
521 }
522
523 int emcore_clear_session(email_session_t *session)
524 {
525         EM_DEBUG_FUNC_BEGIN();
526
527         if (session)
528                 memset(session, 0x00, sizeof(email_session_t));
529         EM_DEBUG_FUNC_END();
530         return true;
531 }
532
533 int emcore_get_current_session(email_session_t **session)
534 {
535         EM_DEBUG_FUNC_BEGIN("session[%p]", session);
536
537         int i;
538
539         for (i = 0; i < SESSION_MAX; i++)  {
540                 if (g_session_list[i].tid == GPOINTER_TO_INT(THREAD_SELF())) {
541                         if (session)
542                                 *session = g_session_list + i;
543
544                         break;
545                 }
546         }
547
548         if (session)
549                 *session = (i != SESSION_MAX) ? g_session_list + i : NULL;
550         EM_DEBUG_FUNC_END();
551         return (i != SESSION_MAX) ? true : false;
552 }
553
554 int emcore_get_mail_count_by_query(int account_id, int priority_sender, int *total_mail, int *unread_mail, int *err_code)
555 {
556         EM_DEBUG_FUNC_BEGIN();
557
558         int ret = false;
559         int err = EMAIL_ERROR_NONE;
560         int i, count;
561         int type = EMAIL_PRIORITY_SENDER;
562         int unread_count = 0;
563         int total_count = 0;
564         char *conditional_clause_string = NULL;
565
566         int rule_count = 0;
567         int is_completed = 0;
568         emstorage_rule_tbl_t *rule = NULL;
569
570         int filter_count = 0;
571         email_list_filter_t *filter_list = NULL;
572
573         if (priority_sender) {
574                 /* Get rule list */
575                 if (!emstorage_get_rule(ALL_ACCOUNT, type, 0, &rule_count, &is_completed, &rule, true, &err) || !rule) {
576                         EM_DEBUG_EXCEPTION("emstorage_get_rule failed");
577                         goto FINISH_OFF;
578                 }
579
580                 /* Make query for searching unread mail */
581                 if (account_id != ALL_ACCOUNT) {
582                         filter_count = (rule_count * 2) + 3;
583                 } else {
584                         filter_count = (rule_count * 2) + 1;                              // 1 is unseen field.
585                 }
586
587                 filter_list = em_malloc(sizeof(email_list_filter_t) * filter_count);
588                 if (filter_list == NULL) {
589                         EM_DEBUG_EXCEPTION("em_malloc failed");
590                         err = EMAIL_ERROR_OUT_OF_MEMORY;
591                         goto FINISH_OFF;
592                 }
593
594                 for (i = 0, count = 0; i < filter_count - 1; i += 2, count++) {
595                         filter_list[i].list_filter_item_type                             = EMAIL_LIST_FILTER_ITEM_RULE;
596                         filter_list[i].list_filter_item.rule.rule_type                   = EMAIL_LIST_FILTER_RULE_INCLUDE;
597                         filter_list[i].list_filter_item.rule.target_attribute            = EMAIL_MAIL_ATTRIBUTE_FROM;
598                         filter_list[i].list_filter_item.rule.key_value.string_type_value = EM_SAFE_STRDUP(rule[count].value);
599
600                         filter_list[i+1].list_filter_item_type                           = EMAIL_LIST_FILTER_ITEM_OPERATOR;
601                         if (i == (filter_count - 2)) {
602                                 filter_list[i+1].list_filter_item.operator_type          = EMAIL_LIST_FILTER_OPERATOR_AND;
603                         } else {
604                                 filter_list[i+1].list_filter_item.operator_type          = EMAIL_LIST_FILTER_OPERATOR_OR;
605                         }
606                 }
607
608                 filter_list[i].list_filter_item_type                              = EMAIL_LIST_FILTER_ITEM_RULE;
609                 filter_list[i].list_filter_item.rule.rule_type                    = EMAIL_LIST_FILTER_RULE_EQUAL;
610                 filter_list[i].list_filter_item.rule.target_attribute             = EMAIL_MAIL_ATTRIBUTE_FLAGS_SEEN_FIELD;
611                 filter_list[i].list_filter_item.rule.key_value.integer_type_value = 0;
612
613                 if (account_id != ALL_ACCOUNT) {
614                         filter_list[i+1].list_filter_item_type                              = EMAIL_LIST_FILTER_ITEM_OPERATOR;
615                         filter_list[i+1].list_filter_item.operator_type                     = EMAIL_LIST_FILTER_OPERATOR_AND;
616
617                         filter_list[i+2].list_filter_item_type                              = EMAIL_LIST_FILTER_ITEM_RULE;
618                         filter_list[i+2].list_filter_item.rule.rule_type                    = EMAIL_LIST_FILTER_RULE_EQUAL;
619                         filter_list[i+2].list_filter_item.rule.target_attribute             = EMAIL_MAIL_ATTRIBUTE_ACCOUNT_ID;
620                         filter_list[i+2].list_filter_item.rule.key_value.integer_type_value = account_id;
621                 }
622
623                 if ((err = emstorage_write_conditional_clause_for_getting_mail_list(filter_list, filter_count, NULL, 0, -1, -1, &conditional_clause_string)) != EMAIL_ERROR_NONE) {
624                         EM_DEBUG_EXCEPTION("emstorage_write_conditional_clause_for_getting_mail_list failed[%d]", err);
625                         goto FINISH_OFF;
626                 }
627
628                 EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
629
630                 /* Search the mail of priority sender in DB */
631                 if ((err = emstorage_query_mail_count(conditional_clause_string, true, &total_count, &unread_count)) != EMAIL_ERROR_NONE) {
632                         EM_DEBUG_EXCEPTION("emstorage_query_mail_count failed:[%d]", err);
633                         goto FINISH_OFF;
634                 }
635         } else {
636                 if (!emstorage_get_mail_count(account_id, 0, &total_count, &unread_count, true, &err))  {
637                         EM_DEBUG_EXCEPTION(" emstorage_get_mail_count failed - %d", err);
638
639                         goto FINISH_OFF;
640                 }
641         }
642
643         ret = true;
644
645 FINISH_OFF:
646
647         if (rule)
648                 emstorage_free_rule(&rule, rule_count, NULL);
649
650         if (filter_list)
651                 emstorage_free_list_filter(&filter_list, filter_count);
652
653         EM_SAFE_FREE(conditional_clause_string);
654
655         if (total_mail)
656                 *total_mail = total_count;
657
658         if (unread_mail)
659                 *unread_mail = unread_count;
660
661         if (err_code)
662                 *err_code = err;
663
664         return ret;
665 }
666
667 int emcore_display_unread_in_badge()
668 {
669         EM_DEBUG_FUNC_BEGIN();
670
671         int ret = false;
672         int err = EMAIL_ERROR_NONE;
673         int total_unread_count = 0;
674         int total_mail_count = 0;
675         int badge_ticker = 0;
676         int priority_sender = 0;
677
678
679         /* Get the Global noti ticker */
680         if (vconf_get_bool(VCONFKEY_TICKER_NOTI_BADGE_EMAIL, &badge_ticker) != 0) {
681                 EM_DEBUG_EXCEPTION("vconf_get_bool failed");
682                 err = EMAIL_ERROR_GCONF_FAILURE;
683                 goto FINISH_OFF;
684         }
685
686         /* Get the priority noti ticker */
687         if (!badge_ticker) {
688                 if (vconf_get_bool(VCONF_VIP_NOTI_BADGE_TICKER, &badge_ticker) != 0) {
689                         EM_DEBUG_EXCEPTION("vconf_get_bool failed");
690                         err = EMAIL_ERROR_GCONF_FAILURE;
691                         goto FINISH_OFF;
692                 }
693
694                 if (!badge_ticker) {
695                         EM_DEBUG_LOG("Not display the badge");
696                         ret = true;
697                         goto FINISH_OFF;
698                 }
699
700                 priority_sender = 1;
701         }
702
703         /* Get unread mail count */
704         if (!emcore_get_mail_count_by_query(ALL_ACCOUNT, priority_sender, &total_mail_count, &total_unread_count, &err)) {
705                 EM_DEBUG_EXCEPTION("emcore_get_mail_count_by_query failed");
706                 goto FINISH_OFF;
707         }
708
709         /* Use badge API */
710         badge_error_e badge_err = BADGE_ERROR_NONE;
711         bool exist;
712
713         if((badge_err = badge_is_existing("com.samsung.email", &exist)) != BADGE_ERROR_NONE) {
714                 EM_DEBUG_EXCEPTION("badge_is_existing failed [%d]", badge_err);
715                 err = EMAIL_ERROR_BADGE_API_FAILED;
716                 goto FINISH_OFF;
717         }
718         if (!exist) {
719                 /* create badge */
720                 if((badge_err = badge_create("com.samsung.email", "/usr/bin/email-service")) != BADGE_ERROR_NONE) {
721                         EM_DEBUG_EXCEPTION("badge_create failed [%d]", badge_err);
722                         err = EMAIL_ERROR_BADGE_API_FAILED;
723                         goto FINISH_OFF;
724                 }
725         }
726
727         if((badge_err = badge_set_count("com.samsung.email", total_unread_count)) != BADGE_ERROR_NONE) {
728                 EM_DEBUG_EXCEPTION("badge_set_count failed [%d]", badge_err);
729                 err = EMAIL_ERROR_BADGE_API_FAILED;
730                 goto FINISH_OFF;
731         }
732
733         if (total_unread_count <= 0)
734                 goto FINISH_OFF;
735
736         ret = true;
737
738 FINISH_OFF:
739
740         return ret;
741 }
742
743 static int emcore_layout_multi_noti(notification_h noti, int unread_mail, char *email_address, char *account_name)
744 {
745         EM_DEBUG_FUNC_BEGIN();
746         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
747         char modified_string[10];
748
749         noti_err = notification_set_layout(noti, NOTIFICATION_LY_NOTI_EVENT_MULTIPLE);
750         if (noti_err != NOTIFICATION_ERROR_NONE) {
751                 EM_DEBUG_EXCEPTION("notification_set_layout NOTI_EVENT_SINGLE failed [%d]", noti_err);
752                 goto FINISH_OFF;
753         }
754
755         SNPRINTF(modified_string, sizeof(modified_string), "%d", unread_mail);
756
757         noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_TITLE, "Email", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
758         if (noti_err != NOTIFICATION_ERROR_NONE) {
759                 EM_DEBUG_EXCEPTION("notification_set_text TEXT_TYPE_TITLE failed");
760                 goto FINISH_OFF;
761         }
762
763         noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_EVENT_COUNT, modified_string, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
764         if (noti_err != NOTIFICATION_ERROR_NONE) {
765                 EM_DEBUG_EXCEPTION("notification_set_text TEXT_TYPE_EVENT_COUNT failed");
766                 goto FINISH_OFF;
767         }
768
769         noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT, "new emails", "IDS_EMAIL_BODY_NEW_EMAILS", NOTIFICATION_VARIABLE_TYPE_NONE);
770         if (noti_err != NOTIFICATION_ERROR_NONE) {
771                 EM_DEBUG_EXCEPTION("notification_set_text TEXT_TYPE_CONTENT failed");
772                 goto FINISH_OFF;
773         }
774
775         noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_1, email_address, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
776         if (noti_err != NOTIFICATION_ERROR_NONE) {
777                 EM_DEBUG_EXCEPTION("notification_set_text TEXT_TYPE_INFO_1 failed");
778                 goto FINISH_OFF;
779         }
780
781         noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_2, account_name, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
782         if (noti_err != NOTIFICATION_ERROR_NONE) {
783                 EM_DEBUG_EXCEPTION("notification_set_text TEXT_TYPE_INFO_2 failed");
784                 goto FINISH_OFF;
785         }
786
787         noti_err = notification_set_image(noti, NOTIFICATION_IMAGE_TYPE_ICON, EMAIL_NOTI_ICON_PATH);
788         if (noti_err != NOTIFICATION_ERROR_NONE) {
789                 EM_DEBUG_EXCEPTION("notification_set_image TYPE_ICON failed");
790                 goto FINISH_OFF;
791         }
792
793 FINISH_OFF:
794
795         EM_DEBUG_FUNC_END("noti_err : [%d]", noti_err);
796         return noti_err;
797 }
798
799 static int emcore_layout_single_noti(notification_h noti, char *account_name, char *display_sender, time_t time, char *subject)
800 {
801         EM_DEBUG_FUNC_BEGIN();
802         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
803
804         noti_err = notification_set_layout(noti, NOTIFICATION_LY_NOTI_EVENT_SINGLE);
805         if (noti_err != NOTIFICATION_ERROR_NONE) {
806                 EM_DEBUG_EXCEPTION("notification_set_layout NOTI_EVENT_SINGLE failed [%d]", noti_err);
807                 goto FINISH_OFF;
808         }
809
810         noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_TITLE, "Email", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
811         if (noti_err != NOTIFICATION_ERROR_NONE) {
812                 EM_DEBUG_EXCEPTION("notification_set_text TEXT_TYPE_TITLE failed");
813                 goto FINISH_OFF;
814         }
815
816         noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT, account_name, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
817         if (noti_err != NOTIFICATION_ERROR_NONE) {
818                 EM_DEBUG_EXCEPTION("notification_set_text TEXT_TYPE_CONTENT failed");
819                 goto FINISH_OFF;
820         }
821
822         noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_1, display_sender, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
823         if (noti_err != NOTIFICATION_ERROR_NONE) {
824                 EM_DEBUG_EXCEPTION("notification_set_text TEXT_TYPE_INFO_1 failed");
825                 goto FINISH_OFF;
826         }
827
828         /*
829         noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_SUB_1, time, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
830         if (noti_err != NOTIFICATION_ERROR_NONE) {
831                 EM_DEBUG_EXCEPTION("notification_set_text TEXT_TYPE_INFO_SUB_1 failed");
832                 goto FINISH_OFF;
833         }
834         */
835
836         noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_2, subject, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
837         if (noti_err != NOTIFICATION_ERROR_NONE) {
838                 EM_DEBUG_EXCEPTION("notification_set_text TEXT_TYPE_INFO_2 failed");
839                 goto FINISH_OFF;
840         }
841
842         noti_err = notification_set_image(noti, NOTIFICATION_IMAGE_TYPE_ICON, EMAIL_NOTI_ICON_PATH);
843         if (noti_err != NOTIFICATION_ERROR_NONE) {
844                 EM_DEBUG_EXCEPTION("notification_set_image TYPE_ICON failed");
845                 goto FINISH_OFF;
846         }
847
848 FINISH_OFF:
849
850         EM_DEBUG_FUNC_END("noti_err : [%d]", noti_err);
851         return noti_err;
852 }
853
854 static int emcore_add_notification(int account_id, int mail_id, email_action_t action)
855 {
856         EM_DEBUG_FUNC_BEGIN();
857         int err = EMAIL_ERROR_NONE;
858
859         EM_DEBUG_FUNC_END("ret [%d]", err);
860         return err;
861 }
862
863 INTERNAL_FUNC int emcore_show_user_message(int id, email_action_t action, int error)
864 {
865         EM_DEBUG_FUNC_BEGIN("id[%d], action[%d], error[%d]", id, action, error);
866
867         int ret = false;
868
869         if (action == EMAIL_ACTION_SEND_MAIL && error != EMAIL_ERROR_CANCELLED) {
870         /*  In case email is cancelled using cancel button in Outbox there is no need to show Cancel/Retry Pop up */
871                 emstorage_mail_tbl_t *mail_table_data = NULL;
872
873                 if (error == 0) /*  error 0 means 'this is not error' */
874                         return true;
875
876                 if (id <= 0) {
877                         EM_DEBUG_LOG("Invalid mail_id");
878                         return false;
879                 }
880
881                 if (!emstorage_get_mail_by_id(id, &mail_table_data, true, NULL)) {
882                         EM_DEBUG_LOG("Mail not found");
883                         return false;
884                 }
885
886                 if (emcore_add_notification(mail_table_data->account_id, id, action) != EMAIL_ERROR_NONE) {
887                         EM_DEBUG_EXCEPTION("emcore_notification_set error");
888
889                         if (!emstorage_free_mail(&mail_table_data, 1, NULL))
890                                 EM_DEBUG_EXCEPTION("emstorage_free_mail Failed");
891
892                         return false;
893                 }
894
895                 if (!emstorage_free_mail(&mail_table_data, 1, NULL))
896                         EM_DEBUG_EXCEPTION("emstorage_free_mail Failed");
897
898                 ret = true;
899         }
900         EM_DEBUG_FUNC_END("ret [%d]", ret);
901         return ret;
902 }
903
904
905 /* storage space handling - 210709 */
906 int emcore_get_storage_status(void)
907 {
908         EM_DEBUG_FUNC_BEGIN();
909         int storage_status = 0, nError = 0;
910
911         g_type_init();
912
913 #ifdef STORAGE_STATUS
914         nError = vconf_get_int(PS_KEY_SYSTEM_STORAGE_MOVI_STATUS,
915                                                         &storage_status);
916 #endif /*  STORAGE_STATUS */
917
918         if (nError == -1) {
919                 EM_DEBUG_EXCEPTION("vconf_get_int Failed");
920                 return false;
921         }
922         EM_DEBUG_FUNC_END();
923         return storage_status;
924 }
925
926 int emcore_is_storage_full(int *err_code)
927 {
928         EM_DEBUG_FUNC_BEGIN();
929
930         int ret = false;
931         int err = EMAIL_ERROR_NONE;
932         struct statfs buf = {0};
933
934         if (statfs(DATA_PATH, &buf) == -1) {
935                 EM_DEBUG_EXCEPTION("statfs(\"%s\") failed - %d", DATA_PATH, errno);
936                 err = EMAIL_ERROR_SYSTEM_FAILURE;
937                 goto FINISH_OFF;
938         }
939         else  {
940                 long i_free = (buf.f_bfree * buf.f_bsize) / (1024 * 1024);
941                 EM_DEBUG_LOG("f_bfree[%d] f_bsize[%d]", buf.f_bfree, buf.f_bsize);
942                 EM_DEBUG_LOG("Free space of storage is[%ld] MB.", i_free);
943                 if (i_free < EMAIL_LIMITATION_FREE_SPACE)
944                         err = EMAIL_ERROR_MAIL_MEMORY_FULL;
945         }
946
947         if (err == EMAIL_ERROR_MAIL_MEMORY_FULL)
948                 ret = true;
949
950 FINISH_OFF:
951         if (err_code != NULL)
952                 *err_code = err;
953         EM_DEBUG_FUNC_END("ret[%d]", ret);
954         return ret;
955 }
956
957 int emcore_calc_mail_size(email_mail_data_t *input_mail_data, email_attachment_data_t *input_attachment_data_list, int input_attachment_count, int *output_size)
958 {
959         EM_DEBUG_FUNC_BEGIN("input_mail_data[%p], input_attachment_data_list[%p], input_attachment_count[%d], output_size[%p]", input_mail_data, input_attachment_data_list, input_attachment_count, output_size);
960
961         struct stat            st_buf;
962         int                    mail_size = 0; /*  size of the plain text body and attachments */
963         int                    err       = EMAIL_ERROR_NONE;
964         int                    i         = 0;
965
966         if (!input_mail_data || (input_attachment_count && !input_attachment_data_list) || (!input_attachment_count &&input_attachment_data_list) || !output_size)  {
967                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
968                 err = EMAIL_ERROR_INVALID_PARAM;
969                 goto FINISH_OFF;
970         }
971
972         if (input_mail_data->file_path_plain != NULL) {
973                 if (stat(input_mail_data->file_path_plain, &st_buf) < 0)  {
974                         EM_DEBUG_EXCEPTION("input_mail_data->file_path_plain : stat(\"%s\") failed...", input_mail_data->file_path_plain);
975                         err = EMAIL_ERROR_FILE_NOT_FOUND;
976                         goto FINISH_OFF;
977                 }
978
979                 mail_size += st_buf.st_size;
980
981         }
982
983         if (input_mail_data->file_path_html != NULL) {
984                 if (stat(input_mail_data->file_path_html, &st_buf) < 0) {
985                         EM_DEBUG_EXCEPTION("input_mail_data->file_path_html : stat(\"%s\") failed...", input_mail_data->file_path_html);
986                         err = EMAIL_ERROR_FILE_NOT_FOUND;
987                         goto FINISH_OFF;
988                 }
989
990                 mail_size += st_buf.st_size;
991         }
992
993         for(i = 0; i < input_attachment_count; i++)  {
994                 if (stat(input_attachment_data_list[i].attachment_path, &st_buf) < 0)  {
995                         EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", input_attachment_data_list[i].attachment_path);
996                         err = EMAIL_ERROR_FILE_NOT_FOUND;
997                         goto FINISH_OFF;
998                 }
999                 mail_size += st_buf.st_size;
1000         }
1001
1002         *output_size = mail_size;
1003
1004 FINISH_OFF:
1005
1006         EM_DEBUG_FUNC_END("mail_size [%d]", mail_size);
1007         return err;
1008 }
1009
1010
1011 /* parse the Full mailbox Path and Get the Alias Name of the Mailbox */
1012 char *emcore_get_alias_of_mailbox(const char *mailbox_path)
1013 {
1014         EM_DEBUG_FUNC_BEGIN();
1015         EM_IF_NULL_RETURN_VALUE(mailbox_path, NULL);
1016
1017         guint index = 0;
1018         gchar **token_list = NULL;
1019         gchar *mailbox = NULL, *name = NULL;
1020         char *converted_name;
1021
1022
1023         mailbox = g_strdup(mailbox_path);
1024         token_list = g_strsplit_set(mailbox, "/", -1);
1025
1026         if (mailbox)
1027                 g_free(mailbox);
1028
1029         while (token_list[index] != NULL)
1030                 index++;
1031
1032         name = g_strdup(token_list[index - 1]);
1033         if(!name) /* prevent 27459 */
1034                 return NULL;
1035
1036         g_strfreev(token_list);
1037
1038         converted_name = emcore_convert_mutf7_to_utf8(name);
1039
1040         if (name)
1041                 g_free(name);
1042
1043         EM_DEBUG_FUNC_END();
1044         return converted_name;
1045 }
1046
1047
1048 static int emcore_get_first_address(const char *full_address, char **alias, char **address)
1049 {
1050         EM_DEBUG_FUNC_BEGIN();
1051
1052         if (full_address == NULL || alias == NULL || address == NULL){
1053                 EM_DEBUG_EXCEPTION("Invalid Param  :  full_address[%p], alias[%p], address[%p]", full_address, alias, address);
1054                 return false;
1055         }
1056
1057         char *s = NULL;
1058         char *alias_start = NULL;
1059         char *alias_end = NULL;
1060         char *alias_cursor = NULL;
1061         char *address_start = NULL;
1062         char *address_end = NULL;
1063         char *first_address = NULL;
1064
1065         if (full_address){
1066                 s = (char *)strchr((char *)full_address, ';');
1067                 if (s == NULL)
1068                         first_address = strdup(full_address);   /*  only one  address */
1069                 else
1070                         first_address = strndup(full_address, s - full_address);        /*  over two addresses */
1071
1072                 /*  get alias */
1073                 *alias = NULL;
1074                 if ((alias_start = (char *)strchr((char *)first_address, '\"'))){
1075                         alias_start++;
1076                         alias_cursor = alias_start;
1077                         while ((alias_cursor = (char *)strchr((char *)(alias_cursor), '\"'))){
1078                                 alias_end = alias_cursor;
1079                                 alias_cursor++;
1080                                 if (*alias_cursor == 0)
1081                                         break;
1082                         }
1083                         if (alias_end)  {       /*  there is "alias" */
1084                                 *alias = strndup(alias_start, alias_end - alias_start);
1085                                 EM_DEBUG_LOG("alias [%s]", *alias);
1086                         }
1087                 }
1088
1089                 /*  get address */
1090                 *address = NULL;
1091                 if (alias_end == NULL)
1092                         s = first_address;
1093                 else
1094                         s = alias_end+1;
1095                 if ((address_start = (char *)strchr((char  *)s, '<'))){
1096                         address_start++;
1097                         if ((address_end = (char *)strchr((char  *)address_start, '>')))
1098                                 *address = strndup(address_start, address_end - address_start); /*  (alias) <(addr)>  ... */
1099                         else
1100                                 *address = strdup(s);
1101                 }
1102                 else
1103                *address = strdup(s);    /*  (addr) ; ...                 :  no alias */
1104         }
1105
1106         EM_SAFE_FREE(first_address);
1107         EM_DEBUG_FUNC_END();
1108         return true;
1109 }
1110
1111 void emcore_fill_address_information_of_mail_tbl(emstorage_mail_tbl_t *mail_data)
1112 {
1113         EM_DEBUG_FUNC_BEGIN("mail_data [%p]", mail_data);
1114
1115         char *first_alias   = NULL;
1116         char *first_address = NULL;
1117         char *recipient     = NULL;
1118
1119         /*  sender alias & address */
1120         if (emcore_get_first_address(mail_data->full_address_from, &first_alias, &first_address) == true) {
1121                 if (first_alias == NULL) {
1122                         mail_data->alias_sender = EM_SAFE_STRDUP(first_address);
1123                 }
1124                 else {
1125                         mail_data->alias_sender = first_alias;
1126                         first_alias = NULL;
1127                 }
1128                 mail_data->email_address_sender = first_address;
1129                 first_address = NULL;
1130         }
1131
1132         /*  recipient alias & address */
1133         if (mail_data->full_address_to != NULL)
1134                 recipient = mail_data->full_address_to;
1135         else if (mail_data->full_address_cc != NULL)
1136                 recipient = mail_data->full_address_cc;
1137         else if (mail_data->full_address_bcc != NULL)
1138                 recipient = mail_data->full_address_bcc;
1139
1140         if (emcore_get_first_address(recipient, &first_alias, &first_address) == true) {
1141                 if (first_alias == NULL)
1142                         mail_data->alias_recipient = EM_SAFE_STRDUP(first_address);
1143                 else
1144                         mail_data->alias_recipient = first_alias;
1145
1146                 mail_data->email_address_recipient = first_address;
1147         }
1148         EM_DEBUG_FUNC_END();
1149 }
1150
1151 struct email_attribute_info {
1152         email_mail_attribute_type        attribute_type;
1153         char                            *attribute_name;
1154         email_mail_attribute_value_type  attribute_value_type;
1155 };
1156
1157 static struct email_attribute_info _mail_attribute_info_array[] = {
1158                 {EMAIL_MAIL_ATTRIBUTE_MAIL_ID, "mail_id", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
1159                 {EMAIL_MAIL_ATTRIBUTE_ACCOUNT_ID, "account_id", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
1160                 {EMAIL_MAIL_ATTRIBUTE_MAILBOX_ID, "mailbox_id", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
1161                 {EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME, "mailbox_name", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_STRING},
1162                 {EMAIL_MAIL_ATTRIBUTE_MAILBOX_TYPE, "mailbox_type", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
1163                 {EMAIL_MAIL_ATTRIBUTE_SUBJECT, "subject", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_STRING},
1164                 {EMAIL_MAIL_ATTRIBUTE_DATE_TIME, "date_time",EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_TIME},
1165                 {EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_STATUS, "server_mail_status", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
1166                 {EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME, "server_mailbox_name", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_STRING},
1167                 {EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID, "server_mail_id", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_STRING},
1168                 {EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID, "message_id", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_STRING},
1169                 {EMAIL_MAIL_ATTRIBUTE_REFERENCE_MAIL_ID, "reference_mail_id", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
1170                 {EMAIL_MAIL_ATTRIBUTE_FROM, "full_address_from", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_STRING},
1171                 {EMAIL_MAIL_ATTRIBUTE_TO, "full_address_to", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_STRING},
1172                 {EMAIL_MAIL_ATTRIBUTE_CC, "full_address_cc", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_STRING},
1173                 {EMAIL_MAIL_ATTRIBUTE_BCC, "full_address_bcc", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_STRING},
1174                 {EMAIL_MAIL_ATTRIBUTE_BODY_DOWNLOAD_STATUS, "body_download_status", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
1175                 {EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN, "file_path_plain",EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_STRING},
1176                 {EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML,"file_path_html", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_STRING},
1177                 {EMAIL_MAIL_ATTRIBUTE_FILE_SIZE,"file_size", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
1178                 {EMAIL_MAIL_ATTRIBUTE_FLAGS_SEEN_FIELD,"flags_seen_field", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
1179                 {EMAIL_MAIL_ATTRIBUTE_FLAGS_DELETED_FIELD,"flags_deleted_field",EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER },
1180                 {EMAIL_MAIL_ATTRIBUTE_FLAGS_FLAGGED_FIELD,"flags_flagged_field", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
1181                 {EMAIL_MAIL_ATTRIBUTE_FLAGS_ANSWERED_FIELD,"flags_answered_field", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
1182                 {EMAIL_MAIL_ATTRIBUTE_FLAGS_RECENT_FIELD,"flags_recent_field", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
1183                 {EMAIL_MAIL_ATTRIBUTE_FLAGS_DRAFT_FIELD,"flags_draft_field", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
1184                 {EMAIL_MAIL_ATTRIBUTE_FLAGS_FORWARDED_FIELD,"flags_forwarded_field", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
1185                 {EMAIL_MAIL_ATTRIBUTE_DRM_STATUS,"drm_status", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
1186                 {EMAIL_MAIL_ATTRIBUTE_PRIORITY,"priority", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
1187                 {EMAIL_MAIL_ATTRIBUTE_SAVE_STATUS,"save_status", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
1188                 {EMAIL_MAIL_ATTRIBUTE_LOCK_STATUS,"lock_status", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
1189                 {EMAIL_MAIL_ATTRIBUTE_REPORT_STATUS,"report_status", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
1190                 {EMAIL_MAIL_ATTRIBUTE_ATTACHMENT_COUNT,"attachment_count", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
1191                 {EMAIL_MAIL_ATTRIBUTE_INLINE_CONTENT_COUNT,"inline_content_count", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
1192                 {EMAIL_MAIL_ATTRIBUTE_THREAD_ID,"thread_id", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
1193                 {EMAIL_MAIL_ATTRIBUTE_THREAD_ITEM_COUNT,"thread_item_count", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
1194                 {EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT,"preview_text", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_STRING},
1195                 {EMAIL_MAIL_ATTRIBUTE_MEETING_REQUEST_STATUS,"meeting_request_status", },
1196                 {EMAIL_MAIL_ATTRIBUTE_MESSAGE_CLASS,"message_class", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
1197                 {EMAIL_MAIL_ATTRIBUTE_DIGEST_TYPE,"digest_type", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
1198                 {EMAIL_MAIL_ATTRIBUTE_SMIME_TYPE,"smime_type", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
1199                 {EMAIL_MAIL_ATTRIBUTE_SCHEDULED_SENDING_TIME,"scheduled_sending_time", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_TIME},
1200                 {EMAIL_MAIL_ATTRIBUTE_EAS_DATA_LENGTH_TYPE,"eas_data_length", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
1201                 {EMAIL_MAIL_ATTRIBUTE_EAS_DATA_TYPE,"eas_data", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_BINARY}
1202 };
1203
1204 INTERNAL_FUNC char* emcore_get_mail_field_name_by_attribute_type(email_mail_attribute_type input_attribute_type)
1205 {
1206         EM_DEBUG_FUNC_BEGIN("input_attribute_type [%d]", input_attribute_type);
1207
1208         if(input_attribute_type > EMAIL_MAIL_ATTRIBUTE_EAS_DATA_TYPE || input_attribute_type < 0) {
1209                 EM_DEBUG_EXCEPTION("Invalid input_attribute_type [%d]", input_attribute_type);
1210                 return NULL;
1211         }
1212
1213         EM_DEBUG_FUNC_END("return [%s]", _mail_attribute_info_array[input_attribute_type].attribute_name);
1214         return _mail_attribute_info_array[input_attribute_type].attribute_name;
1215 }
1216
1217 INTERNAL_FUNC int emcore_get_attribute_type_by_mail_field_name(char *input_mail_field_name, email_mail_attribute_type *output_mail_attribute_type)
1218 {
1219         EM_DEBUG_FUNC_BEGIN("input_mail_field_name [%p], output_mail_attribute_type [%p]", input_mail_field_name, output_mail_attribute_type);
1220         int i = 0;
1221         int err = EMAIL_ERROR_DATA_NOT_FOUND;
1222
1223         if(!input_mail_field_name || !output_mail_attribute_type) {
1224                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1225                 return EMAIL_ERROR_INVALID_PARAM;
1226         }
1227
1228         for (i = 0; i < EMAIL_MAIL_ATTRIBUTE_END; i++) {
1229                 if (EM_SAFE_STRCMP(input_mail_field_name, _mail_attribute_info_array[i].attribute_name) == 0) {
1230                         *output_mail_attribute_type = i;
1231                         err = EMAIL_ERROR_NONE;
1232                         break;
1233                 }
1234         }
1235
1236         EM_DEBUG_FUNC_END("found mail attribute type [%d]", (int)*output_mail_attribute_type);
1237         return err;
1238 }
1239
1240 INTERNAL_FUNC int emcore_get_mail_attribute_value_type(email_mail_attribute_type input_attribute_type, email_mail_attribute_value_type *output_value_type)
1241 {
1242         EM_DEBUG_FUNC_BEGIN("input_attribute_type[%d] output_value_type[%p]", input_attribute_type, output_value_type);
1243         int err = EMAIL_ERROR_NONE;
1244         int i = 0;
1245         email_mail_attribute_value_type value_type = EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_NONE;
1246
1247         if (output_value_type == NULL) {
1248                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1249                 err = EMAIL_ERROR_INVALID_PARAM;
1250                 goto FINISH_OFF;
1251         }
1252
1253         for(i = 0; i < EMAIL_MAIL_ATTRIBUTE_END; i++) {
1254                 if(input_attribute_type == _mail_attribute_info_array[i].attribute_type) {
1255                         value_type = _mail_attribute_info_array[i].attribute_value_type;
1256                         break;
1257                 }
1258         }
1259
1260         *output_value_type = value_type;
1261
1262         EM_DEBUG_LOG("value_type [%d]", value_type);
1263
1264 FINISH_OFF:
1265         EM_DEBUG_FUNC_END("err [%d]", err);
1266         return err;
1267 }
1268
1269 #ifdef __FEATURE_BODY_SEARCH__
1270 int emcore_strip_mail_body_from_file(emstorage_mail_tbl_t *mail, char **stripped_text, int *err_code)
1271 {
1272         EM_DEBUG_FUNC_BEGIN("mail[%p]");
1273
1274         int ret = false;
1275         int err = EMAIL_ERROR_NONE;
1276         char *buf = NULL;
1277         char *encoding_type = NULL;
1278         char *utf8_encoded_string = NULL;
1279         unsigned int byte_read = 0;
1280         unsigned int byte_written = 0;
1281         GError *glib_error = NULL;
1282         FILE *fp_html = NULL;
1283         FILE *fp_plain = NULL;
1284         struct stat  st_buf;
1285
1286         if (!mail) {
1287                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1288                 err = EMAIL_ERROR_INVALID_PARAM;
1289                 goto FINISH_OFF;
1290         }
1291
1292         /* read file to buf & strip if html text */
1293         if (mail->file_path_html) {
1294                 if((err = em_get_encoding_type_from_file_path(mail->file_path_html, &encoding_type)) != EMAIL_ERROR_NONE) {
1295                         EM_DEBUG_EXCEPTION("em_get_encoding_type_from_file_path failed [%s]", err);
1296                         goto FINISH_OFF;
1297                 }
1298
1299                 if (stat(mail->file_path_html, &st_buf) < 0) {
1300                         EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", mail->file_path_html);
1301                         err = EMAIL_ERROR_FILE_NOT_FOUND;
1302                         goto FINISH_OFF;
1303                 }
1304
1305                 if (!(fp_html = fopen(mail->file_path_html, "r"))) {
1306                         EM_DEBUG_EXCEPTION("fopen failed [%s]", mail->file_path_html);
1307                         err = EMAIL_ERROR_SYSTEM_FAILURE;
1308                         goto FINISH_OFF;
1309                 }
1310
1311                 if (S_ISREG(st_buf.st_mode) && st_buf.st_size == 0) {
1312                         EM_DEBUG_LOG("html_file is empty size");
1313                         err = EMAIL_ERROR_EMPTY_FILE;
1314                         goto FINISH_OFF;
1315                 }
1316
1317                 if (!(buf = (char*)em_malloc(sizeof(char)*(st_buf.st_size + 1)))) {
1318                         EM_DEBUG_EXCEPTION("em_malloc failed");
1319                         err = EMAIL_ERROR_OUT_OF_MEMORY;
1320                         goto FINISH_OFF;
1321                 }
1322
1323                 byte_read = fread(buf, sizeof(char), st_buf.st_size, fp_html);
1324
1325                 if (byte_read <= 0) {
1326                         EM_SAFE_FREE(buf);
1327                         if (ferror(fp_html)) {
1328                                 EM_DEBUG_EXCEPTION("fread failed [%s]", mail->file_path_html);
1329                                 err = EMAIL_ERROR_SYSTEM_FAILURE;
1330                                 goto FINISH_OFF;
1331                         }
1332                 } else {
1333                         if ((err = emcore_strip_HTML(buf)) != EMAIL_ERROR_NONE) {
1334                                 EM_DEBUG_EXCEPTION("emcore_strip failed");
1335                                 goto FINISH_OFF;
1336                         }
1337                 }
1338         }
1339
1340         if (!buf && mail->file_path_plain) {
1341                 if ((err = em_get_encoding_type_from_file_path(mail->file_path_plain, &encoding_type)) != EMAIL_ERROR_NONE) {
1342                         EM_DEBUG_EXCEPTION("em_get_encoding_type_from_file_path failed [%s]", err);
1343                         goto FINISH_OFF;
1344                 }
1345
1346                 memset(&st_buf, 0, sizeof(struct stat));
1347                 if (stat(mail->file_path_plain, &st_buf) < 0) {
1348                         EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", mail->file_path_plain);
1349                         err = EMAIL_ERROR_INVALID_MAIL;
1350                         goto FINISH_OFF;
1351                 }
1352
1353                 if (!(fp_plain = fopen(mail->file_path_plain, "r"))) {
1354                         EM_DEBUG_EXCEPTION("fopen failed [%s]", mail->file_path_plain);
1355                         err = EMAIL_ERROR_SYSTEM_FAILURE;
1356                         goto FINISH_OFF;
1357                 }
1358
1359                 if (S_ISREG(st_buf.st_mode) && st_buf.st_size == 0) {
1360                         EM_DEBUG_LOG("text_file is empty size");
1361                         err = EMAIL_ERROR_EMPTY_FILE;
1362                         goto FINISH_OFF;
1363                 }
1364
1365                 if (!(buf = (char*)em_malloc(sizeof(char)*(st_buf.st_size + 1)))) {
1366                         EM_DEBUG_EXCEPTION("em_malloc failed");
1367                         goto FINISH_OFF;
1368                 }
1369
1370                 byte_read = fread(buf, sizeof(char), st_buf.st_size, fp_plain);
1371
1372                 if (byte_read <= 0) {
1373                         EM_SAFE_FREE(buf);
1374                         err = EMAIL_ERROR_NULL_VALUE;
1375                         if (ferror(fp_plain)) {
1376                                 EM_DEBUG_EXCEPTION("fread failed [%s]", mail->file_path_plain);
1377                                 err = EMAIL_ERROR_SYSTEM_FAILURE;
1378                         }
1379                         goto FINISH_OFF;
1380                 }
1381                 reg_replace(buf, CR_STRING, " ");
1382                 reg_replace(buf, LF_STRING, " ");
1383                 reg_replace(buf, TAB_STRING, " ");
1384         }
1385
1386         if (buf) {
1387                 em_trim_left(buf);
1388                 if(encoding_type && strcasecmp(encoding_type, "UTF-8") != 0) {
1389                         EM_DEBUG_LOG("encoding_type [%s]", encoding_type);
1390
1391                         if (strcasecmp(encoding_type, "ks_c_5601-1987") == 0 ||
1392                                         strcasecmp(encoding_type, "ksc5601") == 0 ||
1393                                         strcasecmp(encoding_type, "cp949") == 0) {
1394                                 EM_SAFE_FREE(encoding_type);
1395                                 encoding_type = EM_SAFE_STRDUP("EUC-KR");
1396                         }
1397
1398                         utf8_encoded_string = (char*)g_convert(buf, -1, "UTF-8", encoding_type, &byte_read, &byte_written, &glib_error);
1399
1400                         if(utf8_encoded_string == NULL) {
1401                                 EM_DEBUG_EXCEPTION("g_convert failed : byte_read[%d], EM_SAFE_STRLEN : [%d]", byte_read, EM_SAFE_STRLEN(buf));
1402                                 EM_DEBUG_LOG("Error is G_CONVERT_ERROR_ILLEGAL_SEQUENCE");
1403
1404                                 if (!g_error_matches(glib_error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE)) {
1405                                         EM_DEBUG_EXCEPTION("g_convert failed");
1406                                         *stripped_text = EM_SAFE_STRDUP(buf);
1407                                         goto FINISH_OFF;
1408                                 }
1409
1410                                 EM_DEBUG_LOG("Extract the preview text, again");
1411
1412                                 utf8_encoded_string = (char *)g_convert(buf, byte_read, "UTF-8", encoding_type, &byte_read, &byte_written, &glib_error);
1413                                 if (utf8_encoded_string == NULL) {
1414                                         EM_DEBUG_EXCEPTION("g_convert failed : byte_read[%d]", byte_read);
1415                                         *stripped_text = EM_SAFE_STRDUP(buf);
1416                                         goto FINISH_OFF;
1417                                 }
1418                         }
1419                         EM_SAFE_FREE(buf);
1420
1421                         *stripped_text = EM_SAFE_STRDUP(utf8_encoded_string);
1422                 } else {
1423                         *stripped_text = EM_SAFE_STRDUP(buf);
1424                 }
1425         }
1426
1427         ret = true;
1428
1429 FINISH_OFF:
1430
1431         if (err_code)
1432                 *err_code = err;
1433
1434         EM_SAFE_FREE(buf);
1435         EM_SAFE_FREE(encoding_type);
1436         EM_SAFE_FREE(utf8_encoded_string);
1437
1438         if (fp_html != NULL)
1439                 fclose(fp_html);
1440
1441         if (fp_plain != NULL)
1442                 fclose(fp_plain);
1443
1444         EM_DEBUG_FUNC_END("ret [%d]", ret);
1445         return ret;
1446 }
1447 #endif
1448
1449
1450 int emcore_get_preview_text_from_file(const char *input_plain_path, const char *input_html_path, int input_preview_buffer_length, char **output_preview_buffer)
1451 {
1452         EM_DEBUG_FUNC_BEGIN("input_plain_path[%p], input_html_path[%p], input_preview_buffer_length [%d], output_preview_buffer[%p]", input_plain_path, input_html_path, input_preview_buffer_length, output_preview_buffer);
1453
1454         int          err = EMAIL_ERROR_NONE;
1455         unsigned int byte_read = 0;
1456         unsigned int byte_written = 0;
1457         int          local_preview_buffer_length = 0;
1458         char        *local_preview_text = NULL;
1459         char        *encoding_type = NULL;
1460         char        *utf8_encoded_string = NULL;
1461         FILE        *fp_html = NULL;
1462         FILE        *fp_plain = NULL;
1463         GError      *glib_error = NULL;
1464         struct stat  st_buf;
1465
1466         if (!output_preview_buffer) {
1467                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1468                 err = EMAIL_ERROR_INVALID_PARAM;
1469                 goto FINISH_OFF;
1470         }
1471
1472         local_preview_buffer_length = input_preview_buffer_length * 2;
1473
1474         if ( input_html_path ) { /*prevent 26249*/
1475                 /*      get preview text from html file */
1476                 if( (err = em_get_encoding_type_from_file_path(input_html_path, &encoding_type)) != EMAIL_ERROR_NONE) {
1477                         EM_DEBUG_EXCEPTION("em_get_encoding_type_from_file_path failed [%s]", err);
1478                         goto FINISH_OFF;
1479                 }
1480
1481                 if (stat(input_html_path, &st_buf) < 0)  {
1482                         EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", input_html_path);
1483                         err = EMAIL_ERROR_FILE_NOT_FOUND;
1484                         goto FINISH_OFF;
1485                 }
1486
1487                 if (!(fp_html = fopen(input_html_path, "r")))   {
1488                         EM_DEBUG_EXCEPTION("fopen failed [%s]", input_html_path);
1489                         err = EMAIL_ERROR_SYSTEM_FAILURE;
1490                         goto FINISH_OFF;
1491                 }
1492
1493                 if (S_ISREG(st_buf.st_mode) && st_buf.st_size == 0) {
1494                         EM_DEBUG_LOG("input_html_file is empty size");
1495                         err = EMAIL_ERROR_EMPTY_FILE;
1496                         goto FINISH_OFF;
1497                 }
1498
1499                 if (!(local_preview_text = (char*)em_malloc(sizeof(char) * (st_buf.st_size + 1)))) {
1500                         EM_DEBUG_EXCEPTION("em_malloc failed");
1501                         err = EMAIL_ERROR_OUT_OF_MEMORY;
1502                         goto FINISH_OFF;
1503                 }
1504
1505                 byte_read = fread(local_preview_text, sizeof(char), st_buf.st_size, fp_html);
1506
1507                 if(byte_read <= 0) { /*prevent 26249*/
1508                         EM_SAFE_FREE(local_preview_text);
1509                         if (ferror(fp_html)) {
1510                                 EM_DEBUG_EXCEPTION("fread failed [%s]", input_html_path);
1511                                 err = EMAIL_ERROR_SYSTEM_FAILURE;
1512                                 goto FINISH_OFF;
1513                         }
1514                 }
1515                 else {
1516                         if ( (err = emcore_strip_HTML(local_preview_text)) != EMAIL_ERROR_NONE) {
1517                                 EM_DEBUG_EXCEPTION("emcore_strip failed");
1518                                 goto FINISH_OFF;
1519                         }
1520                 }
1521         }
1522
1523         if ( !local_preview_text && input_plain_path) { /*prevent 26249*/
1524                 /*  get preview text from plain text file */
1525                 if( (err = em_get_encoding_type_from_file_path(input_plain_path, &encoding_type)) != EMAIL_ERROR_NONE) {
1526                         EM_DEBUG_EXCEPTION("em_get_encoding_type_from_file_path failed [%s]", err);
1527                         goto FINISH_OFF;
1528                 }
1529
1530                 memset(&st_buf, 0, sizeof(struct stat));
1531                 if (stat(input_plain_path, &st_buf) < 0)  {
1532                         EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", input_plain_path);
1533                         err = EMAIL_ERROR_INVALID_MAIL;
1534                         goto FINISH_OFF;
1535                 }
1536
1537                 if (!(fp_plain = fopen(input_plain_path, "r")))  {
1538                         EM_DEBUG_EXCEPTION("fopen failed [%s]", input_plain_path);
1539                         err = EMAIL_ERROR_SYSTEM_FAILURE;
1540                         goto FINISH_OFF;
1541                 }
1542
1543                 if (S_ISREG(st_buf.st_mode) && st_buf.st_size == 0) {
1544                         EM_DEBUG_LOG("input_text_file is empty size");
1545                         err = EMAIL_ERROR_EMPTY_FILE;
1546                         goto FINISH_OFF;
1547                 }
1548
1549                 if (!(local_preview_text = (char*)em_malloc(sizeof(char) * local_preview_buffer_length))) {
1550                         EM_DEBUG_EXCEPTION("em_malloc failed");
1551                         goto FINISH_OFF;
1552                 }
1553
1554                 byte_read = fread(local_preview_text, sizeof(char), local_preview_buffer_length - 1, fp_plain);
1555
1556                 if(byte_read <=0) { /*prevent 26249*/
1557                         EM_SAFE_FREE(local_preview_text);
1558                         err = EMAIL_ERROR_NULL_VALUE;
1559                         if (ferror(fp_plain)) {
1560                                 EM_DEBUG_EXCEPTION("fread failed [%s]", input_plain_path);
1561                                 err = EMAIL_ERROR_SYSTEM_FAILURE;
1562                         }
1563                         goto FINISH_OFF;
1564                 }
1565                 reg_replace(local_preview_text, CR_STRING, " ");
1566                 reg_replace(local_preview_text, LF_STRING, " ");
1567                 reg_replace(local_preview_text, TAB_STRING, " ");
1568         }
1569
1570         if(local_preview_text) {
1571                 em_trim_left(local_preview_text);
1572                 if(encoding_type && strcasecmp(encoding_type, "UTF-8") != 0) {
1573                         EM_DEBUG_LOG("encoding_type [%s]", encoding_type);
1574
1575                         if (strcasecmp(encoding_type, "ks_c_5601-1987") == 0 ||
1576                                         strcasecmp(encoding_type, "ksc5601") == 0 ||
1577                                         strcasecmp(encoding_type, "cp949") == 0) {
1578                                 EM_SAFE_FREE(encoding_type);
1579                                 encoding_type = EM_SAFE_STRDUP("EUC-KR");
1580                         }
1581
1582                         utf8_encoded_string = (char*)g_convert (local_preview_text, -1, "UTF-8", encoding_type, &byte_read, &byte_written, &glib_error);
1583
1584                         if(utf8_encoded_string == NULL) {
1585                                 EM_DEBUG_EXCEPTION("g_convert failed : byte_read[%d], EM_SAFE_STRLEN : [%d]", byte_read, EM_SAFE_STRLEN(local_preview_text));
1586                                 EM_DEBUG_LOG("Error is G_CONVERT_ERROR_ILLEGAL_SEQUENCE");
1587
1588                                 if (!g_error_matches (glib_error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE)) {
1589                                         EM_DEBUG_EXCEPTION("g_convert failed");
1590                                         goto FINISH_OFF;
1591                                 }
1592
1593                                 EM_DEBUG_LOG("Extract the preview text, again");
1594
1595                                 utf8_encoded_string = (char *)g_convert(local_preview_text, byte_read, "UTF-8", encoding_type, &byte_read, &byte_written, &glib_error);
1596                                 if (utf8_encoded_string == NULL) {
1597                                         EM_DEBUG_EXCEPTION("g_convert failed : byte_read[%d]", byte_read);
1598                                         goto FINISH_OFF;
1599                                 }
1600
1601                         }
1602                         EM_SAFE_FREE(local_preview_text);
1603                         local_preview_text = utf8_encoded_string;
1604                 }
1605         }
1606
1607 FINISH_OFF:
1608
1609         if (local_preview_text != NULL)
1610                 *output_preview_buffer = EM_SAFE_STRDUP(local_preview_text);
1611
1612         EM_SAFE_FREE(local_preview_text);
1613         EM_SAFE_FREE(encoding_type);
1614
1615         if (fp_html != NULL)
1616                 fclose(fp_html);
1617
1618         if (fp_plain != NULL)
1619                 fclose(fp_plain);
1620
1621         EM_DEBUG_FUNC_END("err [%d]", err);
1622         return err;
1623 }
1624
1625 INTERNAL_FUNC int emcore_add_transaction_info(int mail_id, int handle , int *err_code)
1626 {
1627         EM_DEBUG_FUNC_BEGIN("mail_id[%d], handle[%d]", mail_id, handle);
1628
1629         int ret = false;
1630         int err = EMAIL_ERROR_NONE ;
1631         em_transaction_info_type_t  *pTransinfo = NULL ;
1632         em_transaction_info_type_t      *pTemp = NULL;
1633
1634         EM_DEBUG_LOG("g_transaction_info_list[%p]", g_transaction_info_list);
1635         pTransinfo = g_transaction_info_list ;
1636
1637         if (!(pTemp = em_malloc(sizeof(em_transaction_info_type_t))))  {
1638                 EM_DEBUG_EXCEPTION("malloc failed...");
1639                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1640                 goto FINISH_OFF;
1641         }
1642         pTemp->mail_id = mail_id ;
1643         pTemp->handle = handle;
1644
1645         if (!pTransinfo) {
1646                 pTransinfo = pTemp ;
1647                 g_transaction_info_list = pTransinfo ;
1648         }
1649         else {
1650                 while (pTransinfo->next)
1651                         pTransinfo = pTransinfo->next;
1652                 pTransinfo->next = pTemp;
1653         }
1654         ret = true ;
1655
1656 FINISH_OFF:
1657
1658         if (err_code)
1659                 *err_code = err;
1660         EM_DEBUG_FUNC_END("g_transaction_info_list[%p]", g_transaction_info_list);
1661         return ret;
1662 }
1663
1664 INTERNAL_FUNC int emcore_get_handle_by_mailId_from_transaction_info(int mail_id, int *pHandle)
1665 {
1666         EM_DEBUG_FUNC_BEGIN("mail_id[%d], handle[%p]", mail_id, pHandle);
1667
1668         int ret = false;
1669         em_transaction_info_type_t  *pTransinfo = NULL ;
1670
1671         if (g_transaction_info_list == NULL) {
1672                 EM_DEBUG_EXCEPTION("g_transaction_info_list NULL");
1673                 return false;
1674         }
1675         pTransinfo = g_transaction_info_list;
1676
1677         do {
1678                 EM_DEBUG_LOG("pTransinfo->mail_id[%d]", pTransinfo->mail_id);
1679                 if (pTransinfo->mail_id == mail_id) {
1680                         *pHandle = pTransinfo->handle;
1681                         ret = true;
1682                         EM_DEBUG_LOG("*pHandle[%d]", *pHandle);
1683                         break;
1684                 }
1685                 else
1686                         pTransinfo = pTransinfo->next ;
1687         }while (pTransinfo);
1688         EM_DEBUG_FUNC_END();
1689         return ret;
1690 }
1691
1692 INTERNAL_FUNC int emcore_delete_transaction_info_by_mailId(int mail_id )
1693 {
1694         EM_DEBUG_FUNC_BEGIN("mail_id[%d]", mail_id);
1695
1696         em_transaction_info_type_t  *pTransinfo ;
1697         em_transaction_info_type_t *pTemp = NULL;
1698
1699         if (g_transaction_info_list == NULL) {
1700                 EM_DEBUG_EXCEPTION("g_transaction_info_list NULL");
1701                 return false;
1702         }
1703         pTransinfo = g_transaction_info_list;
1704
1705         EM_DEBUG_LOG("pTransinfo[%p]", pTransinfo);
1706
1707         do {
1708                 EM_DEBUG_LOG("pTransinfo->mail_id[%d]", pTransinfo->mail_id);
1709                 if (pTransinfo->mail_id == mail_id) {
1710                         pTemp = pTransinfo->next ;
1711                         if (!pTemp) {
1712                                 EM_SAFE_FREE(pTransinfo) ;
1713                                 g_transaction_info_list = NULL;
1714                         }
1715                         else {
1716                                 pTransinfo->mail_id = pTransinfo->next->mail_id;
1717                                 pTransinfo->handle = pTransinfo->next->handle ;
1718                                 pTransinfo->next = pTransinfo->next->next;
1719
1720                                 EM_SAFE_FREE(pTemp);
1721                         }
1722                         break;
1723                 }
1724                 else {
1725                         pTransinfo = pTransinfo->next ;
1726                 }
1727
1728         }while (pTransinfo);
1729         EM_DEBUG_FUNC_END();
1730         return true;
1731 }
1732
1733
1734 #include <regex.h>
1735
1736 int reg_replace (char *input_source_text, char *input_old_pattern_string, char *input_new_string)
1737 {
1738         int         error_code = EMAIL_ERROR_NONE;
1739         char       *pos = NULL;
1740         int         so, n, nmatch, source_text_length, n_count = 1;
1741         regmatch_t *pmatch = NULL;
1742         regex_t     reg_pattern;
1743
1744         if(!input_source_text || !input_old_pattern_string || !input_new_string) {
1745                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1746                 error_code = EMAIL_ERROR_INVALID_PARAM;
1747                 goto FINISH_OFF;
1748         }
1749
1750         source_text_length = EM_SAFE_STRLEN(input_source_text);
1751
1752         if (regcomp(&reg_pattern, input_old_pattern_string, REG_EXTENDED | REG_ICASE) != 0) {
1753                 EM_DEBUG_EXCEPTION("regcomp failed");
1754                 goto FINISH_OFF;
1755         }
1756
1757         nmatch = reg_pattern.re_nsub + 1;
1758
1759         EM_DEBUG_LOG("nmatch [%d]", nmatch);
1760
1761         if(nmatch < 1) {
1762                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_DATA");
1763                 error_code = EMAIL_ERROR_INVALID_DATA;
1764                 goto FINISH_OFF;
1765         }
1766
1767         pmatch = (regmatch_t*)em_malloc(sizeof(regmatch_t) * nmatch);
1768
1769         if(pmatch == NULL) {
1770                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_OUT_OF_MEMORY");
1771                 error_code = EMAIL_ERROR_OUT_OF_MEMORY;
1772                 goto FINISH_OFF;
1773         }
1774
1775         for (pos = input_new_string; *pos ; pos++) {
1776                 if (*pos == '\\' && *(pos + 1) > '0' && *(pos + 1) <= '9') {
1777
1778                         so = pmatch[*(pos + 1) - 48].rm_so;
1779                         n  = pmatch[*(pos + 1) - 48].rm_eo - so;
1780
1781                         EM_DEBUG_LOG("so [%d], n [%d]", so, n);
1782
1783                         if (so < 0 || EM_SAFE_STRLEN (input_new_string) + n - 1 > source_text_length)
1784                                 break;
1785
1786                         memmove (pos + n, pos + 2, EM_SAFE_STRLEN (pos) - 1);
1787                         memmove (pos, input_source_text + so, n);
1788                         pos = pos + n - 2;
1789                 }
1790         }
1791
1792         for (pos = input_source_text; !regexec (&reg_pattern, pos, 1, pmatch, 0);) {
1793                 n = pmatch[0].rm_eo - pmatch[0].rm_so;
1794                 pos += pmatch[0].rm_so;
1795
1796                 memmove (pos + EM_SAFE_STRLEN (input_new_string), pos + n, EM_SAFE_STRLEN (pos) - n + 1);
1797                 memmove (pos, input_new_string, EM_SAFE_STRLEN (input_new_string));
1798                 pos += EM_SAFE_STRLEN (input_new_string);
1799                 n_count++;
1800         }
1801
1802 FINISH_OFF:
1803
1804         EM_SAFE_FREE(pmatch);
1805         regfree (&reg_pattern);
1806
1807         EM_DEBUG_FUNC_END("error_code [%d]", error_code);
1808         return error_code;
1809 }
1810
1811
1812 int emcore_strip_HTML(char *source_string)
1813 {
1814         EM_DEBUG_FUNC_BEGIN("source_string [%p]", source_string);
1815
1816         int result = EMAIL_ERROR_NONE;
1817
1818         /* strip out CR, LF */
1819         reg_replace(source_string, "(\r|\n)", "");
1820
1821         /* strip out HEAD */
1822         reg_replace(source_string, "<head[^>]*>.*<*/head>", "");
1823
1824         /* strip out STYLE */
1825         reg_replace(source_string, "<style[^>]*>.*<*/style>", "");
1826
1827         /* strip out SCRIPT */
1828         reg_replace(source_string, "<script[^>]*>.*<*/script>", "");
1829
1830         /* strip out ALL TAG & comment */
1831         reg_replace(source_string, "<[^>]*>", "");
1832
1833         /* for remaining comment in some cases */
1834         reg_replace(source_string, "-->", "");
1835
1836         /* strip out html entities */
1837         reg_replace(source_string, "&(quot|#34|#034);", "\"");
1838         reg_replace(source_string, "&(#35|#035);", "#");
1839         reg_replace(source_string, "&(#36|#036);", "$");
1840         reg_replace(source_string, "&(#37|#037);", "%");
1841         reg_replace(source_string, "&(amp|#38|#038);", "&");
1842         reg_replace(source_string, "&(lt|#60|#060);", "<");
1843         reg_replace(source_string, "&(gt|#62|#062);", ">");
1844         reg_replace(source_string, "&(#64|#064);", "@");
1845         reg_replace(source_string, "&(lsquo|rsquo);", "'");
1846         reg_replace(source_string, "&(ldquo|rdquo);", "\"");
1847         reg_replace(source_string, "&sbquo;", ",");
1848         reg_replace(source_string, "&(trade|#x2122);", "(TM)");
1849
1850         /* strip out all type of spaces */
1851         reg_replace(source_string, "&(nbsp|#160);", " ");
1852         reg_replace(source_string, "[[:space:]]", " ");
1853         reg_replace(source_string, "  +", " ");
1854
1855         EM_DEBUG_FUNC_END();
1856         return result;
1857 }
1858
1859 INTERNAL_FUNC int emcore_send_noti_for_new_mail(int account_id, char *mailbox_name, char *subject, char *from, char *uid, char *datetime)
1860 {
1861         EM_DEBUG_FUNC_BEGIN("mailbox_name(%s) subject(%s), from(%s), uid(%s), datetime(%s)", mailbox_name, subject, from, uid, datetime);
1862         int error_code = EMAIL_ERROR_NONE;
1863         int param_length = 0;
1864         char *param_string = NULL;
1865
1866         if (mailbox_name == NULL || subject == NULL || from == NULL || uid == NULL || datetime == NULL) {
1867                 error_code = EMAIL_ERROR_INVALID_PARAM;
1868                 EM_DEBUG_EXCEPTION("Invalid parameter, mailbox_name(%p), subject(%p), from(%p), uid(%p), datetime(%p)", mailbox_name, subject, from, uid, datetime);
1869                 goto FINISH_OFF;
1870         }
1871
1872         param_length = strlen(mailbox_name) + strlen(subject) + strlen(from) + strlen(uid) + strlen(datetime) + 5; /*prevent 34358*/
1873
1874         param_string = em_malloc(param_length);
1875         if (param_string == NULL) {
1876                 error_code = EMAIL_ERROR_OUT_OF_MEMORY;
1877                 EM_DEBUG_EXCEPTION("Memory allocation for 'param_string' is failed");
1878                 goto FINISH_OFF;
1879         }
1880
1881         memset(param_string, 0x00, param_length);
1882
1883         SNPRINTF(param_string, param_length, "%s%c%s%c%s%c%s%c%s", mailbox_name, 0x01, subject, 0x01, from, 0x01, uid, 0x01, datetime);
1884
1885         if (emcore_notify_network_event(NOTI_DOWNLOAD_NEW_MAIL, account_id, param_string, 0, 0) == 0) { /*  failed */
1886                 error_code = EMAIL_ERROR_UNKNOWN;
1887                 EM_DEBUG_EXCEPTION("emcore_notify_network_event is failed");
1888                 goto FINISH_OFF;
1889         }
1890
1891 FINISH_OFF:
1892
1893         EM_SAFE_FREE(param_string);
1894         EM_DEBUG_FUNC_END();
1895         return error_code;
1896 }
1897
1898 #define MAX_TITLE_LENGTH 1024
1899
1900 #ifdef __FEATURE_DRIVING_MODE__
1901 int convert_app_err_to_email_err(int app_error)
1902 {
1903         int err = EMAIL_ERROR_NONE;
1904
1905         switch(app_error) {
1906         case SERVICE_ERROR_NONE :
1907                 err = EMAIL_ERROR_NONE;
1908                 break;
1909         case SERVICE_ERROR_INVALID_PARAMETER :
1910                 err = EMAIL_ERROR_INVALID_PARAM;
1911                 break;
1912         case SERVICE_ERROR_OUT_OF_MEMORY :
1913                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1914                 break;
1915         default:
1916                 err = EMAIL_ERROR_UNKNOWN;
1917                 break;
1918         }
1919
1920         return err;
1921 }
1922
1923 int emcore_start_driving_mode(int account_id) {
1924         EM_DEBUG_FUNC_BEGIN();
1925         
1926         int err = SERVICE_ERROR_NONE;
1927         int tts_enable = 0;     
1928         char string_account[10] = { 0 };
1929         service_h service = NULL;
1930
1931         if (vconf_get_bool(VCONFKEY_SETAPPL_DRIVINGMODE_DRIVINGMODE, &tts_enable) != 0) {
1932                 EM_DEBUG_EXCEPTION("vconf_get_int failed");
1933                 goto FINISH_OFF;
1934         }
1935
1936         EM_DEBUG_LOG("Driving Mode : [%d]", tts_enable);
1937
1938         if (!tts_enable)
1939                 goto FINISH_OFF;
1940
1941
1942         if (vconf_get_bool(VCONFKEY_SETAPPL_DRIVINGMODE_NEWEMAILS, &tts_enable) != 0) {
1943                 EM_DEBUG_EXCEPTION("vconf_get_int failed");
1944                 goto FINISH_OFF;
1945         }
1946
1947         EM_DEBUG_LOG("New emails of driving Mode : [%d]", tts_enable);
1948
1949         if (!tts_enable)
1950                 goto FINISH_OFF;
1951
1952         SNPRINTF(string_account, sizeof(string_account), "%d", account_id);
1953
1954         err = service_create(&service);
1955         if (err != SERVICE_ERROR_NONE) {
1956                 EM_DEBUG_EXCEPTION("service_create failed : [%d]", err);
1957                 goto FINISH_OFF;
1958         }
1959
1960         err = service_set_package(service, "com.samsung.email-tts-play");
1961         if (err != SERVICE_ERROR_NONE) {
1962                 EM_DEBUG_EXCEPTION("service_set_package failed : [%d]", err);
1963                 goto FINISH_OFF;
1964         }
1965
1966         err = service_add_extra_data(service, "tts_email_account_id", string_account);
1967         if (err != SERVICE_ERROR_NONE) {
1968                 EM_DEBUG_EXCEPTION("service_add_extra_data failed : [%d]", err);
1969                 goto FINISH_OFF;
1970         }
1971
1972         err = service_send_launch_request(service, NULL, NULL);
1973         if (err != SERVICE_ERROR_NONE) {
1974                 EM_DEBUG_EXCEPTION("service_send_launch_request failed : [%d]", err);
1975                 goto FINISH_OFF;
1976         }
1977
1978 FINISH_OFF:
1979
1980         if (service)
1981                 service_destroy(service);
1982
1983         EM_DEBUG_FUNC_END();
1984         return convert_app_err_to_email_err(err);
1985 }
1986 #endif /* __FEATURE_DRIVING_MODE__ */
1987
1988 int emcore_update_notification_for_unread_mail(int account_id)
1989 {
1990         EM_DEBUG_FUNC_BEGIN("acccount_id[%d]", account_id);
1991         int ret = false;
1992         int i, account_count = 0;
1993         int err = EMAIL_ERROR_NONE;
1994         emstorage_account_tbl_t *p_account_tbl = NULL;
1995
1996         if (!emstorage_get_account_list(&account_count, &p_account_tbl, true, false, &err)) {
1997                 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [%d]", err);
1998                 goto FINISH_OFF;
1999         }
2000
2001         for (i = 0; i < account_count ; i++) {
2002                 if ((err = emcore_add_notification(p_account_tbl[i].account_id, 0, EMAIL_ACTION_NUM)) != EMAIL_ERROR_NONE) {
2003                         EM_DEBUG_EXCEPTION("emcore_add_notification failed : [%d]", err);
2004                         continue;
2005                 }
2006         
2007                 if ((err = emcore_start_driving_mode(p_account_tbl[i].account_id)) != EMAIL_ERROR_NONE) {
2008                         EM_DEBUG_EXCEPTION("emcore_start_driving_mode failed : [%d]", err);
2009                         continue;
2010                 }
2011         }
2012
2013         ret = true;
2014
2015 FINISH_OFF:
2016
2017         if (p_account_tbl)
2018                 emstorage_free_account(&p_account_tbl, account_count, NULL);
2019
2020         EM_DEBUG_FUNC_END();
2021         return ret;
2022 }
2023
2024 INTERNAL_FUNC int emcore_finalize_sync(int account_id, int *error)
2025 {
2026         EM_DEBUG_FUNC_BEGIN("account_id [%d], error [%p]", account_id, error);
2027         int err = EMAIL_ERROR_NONE, ret = true, result_sync_status = SYNC_STATUS_FINISHED;
2028
2029         if ((err = emcore_update_sync_status_of_account(account_id, SET_TYPE_MINUS, SYNC_STATUS_SYNCING)) != EMAIL_ERROR_NONE)
2030                 EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
2031
2032         if (!emstorage_get_sync_status_of_account(ALL_ACCOUNT, &result_sync_status, &err))
2033                 EM_DEBUG_EXCEPTION("emstorage_get_sync_status_of_account failed [%d]", err);
2034
2035         if (result_sync_status == SYNC_STATUS_HAVE_NEW_MAILS) {
2036                 if (!emcore_update_notification_for_unread_mail(ALL_ACCOUNT))
2037                         EM_DEBUG_EXCEPTION("emcore_update_notification_for_unread_mail failed");
2038
2039                 if (!emcore_display_unread_in_badge())
2040                         EM_DEBUG_EXCEPTION("emcore_display_unread_in_badge failed");
2041
2042                 emcore_start_alert();
2043
2044 #ifdef __FEATURE_BLOCKING_MODE__
2045                 emcore_set_blocking_mode_status(false);
2046 #endif /* __FEATURE_BLOCKING_MODE__ */
2047
2048                 if ((err = emcore_update_sync_status_of_account(account_id, SET_TYPE_MINUS, SYNC_STATUS_HAVE_NEW_MAILS)) != EMAIL_ERROR_NONE)
2049                         EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
2050         }
2051
2052         EM_DEBUG_FUNC_END();
2053         return ret;
2054 }
2055
2056 INTERNAL_FUNC int emcore_clear_all_notifications()
2057 {
2058         int account_count = 0, i;
2059         emstorage_account_tbl_t *account_list;
2060         int error_code = EMAIL_ERROR_NONE;
2061
2062         if(!emstorage_get_account_list(&account_count, &account_list, true, false, &error_code)) {
2063                 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed");
2064                 goto FINISH_OFF;
2065         }
2066
2067         for(i = 0; i < account_count; i++) {
2068                 emcore_delete_notification_by_account(account_list[i].account_id);
2069         }
2070
2071 FINISH_OFF:
2072         if(account_count) {
2073                 emstorage_free_account(&account_list, account_count, NULL);
2074         }
2075
2076         EM_DEBUG_FUNC_END("return[%d]", error_code);
2077         return error_code;
2078 }
2079
2080 #define EAS_EXECUTABLE_PATH "/usr/bin/eas-engine"
2081
2082 INTERNAL_FUNC int emcore_delete_notification_by_account(int account_id)
2083 {
2084         EM_DEBUG_FUNC_BEGIN("account_id [%d]", account_id);
2085         int error_code = EMAIL_ERROR_NONE;
2086         int private_id = 0;
2087         char vconf_private_id[MAX_PATH] = {0, };
2088
2089         SNPRINTF(vconf_private_id, sizeof(vconf_private_id), "%s/%d", VCONF_KEY_NOTI_PRIVATE_ID, account_id);
2090         if (vconf_get_int(vconf_private_id, &private_id) != 0) {
2091                 EM_DEBUG_EXCEPTION("vconf_get_int failed");
2092         }
2093         EM_DEBUG_FUNC_END();
2094         return error_code;
2095 }
2096
2097 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
2098
2099 /**
2100  * @fn emcore_convert_to_uid_range_set(email_id_set_t* id_set, int id_set_count, email_uid_range_set **uid_range_set, int range_len, int *err_code)
2101  * Prepare a linked list of uid ranges with each node having a uid_range and lowest and highest uid in it.
2102  *
2103  *@author                                       h.gahlaut@samsung.com
2104  * @param[in] id_set                    Specifies the array of mail_id and corresponding server_mail_id sorted by server_mail_ids in ascending order
2105  * @param[in] id_set_count              Specifies the no. of cells in id_set array i.e. no. of sets of mail_ids and server_mail_ids
2106  * @param[in] range_len         Specifies the maximum length of string of range allowed.
2107  * @param[out] uid_range_set    Returns the uid_ranges formed in the form of a linked list with head stored in uid_range_set pointer
2108  * @param[out] err_code         Returns the error code.
2109  * @remarks                                     An example of a uid_range formed is 2:6,8,10,14:15,89,
2110  *                                                      While using it the caller should remove the ending, (comma)
2111  * @return This function returns true on success or false on failure.
2112  */
2113
2114 INTERNAL_FUNC int emcore_convert_to_uid_range_set(email_id_set_t *id_set, int id_set_count, email_uid_range_set **uid_range_set, int range_len, int *err_code)
2115 {
2116         EM_DEBUG_FUNC_BEGIN();
2117
2118         int ret = false;
2119         int error = EMAIL_ERROR_NONE;
2120
2121         if (NULL == id_set || id_set_count  <= 0 || NULL == uid_range_set) {
2122                 EM_DEBUG_EXCEPTION(" Invalid Parameter id_set[%p] id_set_count[%d] uid_range_set[%p]", id_set, id_set_count, uid_range_set);
2123                 error = EMAIL_ERROR_INVALID_PARAM;
2124                 goto FINISH_OFF;
2125         }
2126
2127         int i = 0;
2128         unsigned long current_uid = 0;
2129         unsigned long first_uid = 0;
2130         unsigned long last_uid = 0;
2131         const int max_subset_string_size = MAX_SUBSET_STRING_SIZE;
2132         char subset_string[MAX_SUBSET_STRING_SIZE] = {0,};
2133         email_uid_range_set *current_node = NULL;       /* current_node denotes the current node under processing in the linked list of uid_range_set that is to be formed*/
2134
2135         if (range_len < (max_subset_string_size + 1))           /* 1 for ending NULL character */ {
2136                 EM_DEBUG_EXCEPTION(" Invalid Parameter range_len[%d]", range_len);
2137                 error = EMAIL_ERROR_INVALID_PARAM;
2138                 goto FINISH_OFF;
2139         }
2140
2141         EM_DEBUG_LOG("id set count[%d] range_len[%d]", id_set_count, range_len);
2142
2143         do {
2144                 first_uid = last_uid = current_uid = id_set[i].server_mail_id;
2145                 /* Start subset string by putting first server mail id in it from id_set*/
2146                 memset(subset_string, 0x00, max_subset_string_size);
2147                 SNPRINTF(subset_string, max_subset_string_size, "%lu", first_uid);
2148                 ++i;
2149
2150                 /* Check if only one server mail id was left in id_set */
2151                 if (i >= id_set_count) {
2152                         /* No more server mail id left in id_set */
2153                         if (false == emcore_append_subset_string_to_uid_range(subset_string, &current_node, uid_range_set, range_len, first_uid, last_uid)) {
2154                                 EM_DEBUG_EXCEPTION("emcore_append_subset_string_to_uid_range failed");
2155                                 goto FINISH_OFF;
2156                         }
2157                         break;
2158                 }
2159                 else {
2160                         /* More server mail id are present in id_set. Find out if first:last_uid is to be formed or only first_uid will be subset string */
2161                         do {
2162                                 current_uid = id_set[i].server_mail_id;
2163                                 if (current_uid == (last_uid + 1)) {
2164                                         last_uid = current_uid;
2165                                         ++i;
2166                                 }
2167                                 else {
2168                                         memset(subset_string, 0x00, max_subset_string_size);
2169                                         if (first_uid != last_uid)      /* Form subset string by first_uid:last_uid */
2170                                                 SNPRINTF(subset_string, max_subset_string_size, "%lu:%lu", first_uid, last_uid);
2171                                         else    /* Form subset string by first_uid */
2172                                                 SNPRINTF(subset_string, max_subset_string_size, "%lu", first_uid);
2173
2174                                         if (false == emcore_append_subset_string_to_uid_range(subset_string, &current_node, uid_range_set, range_len, first_uid, last_uid)) {
2175                                                 EM_DEBUG_EXCEPTION("emcore_append_subset_string_to_uid_range failed");
2176                                                 goto FINISH_OFF;
2177                                         }
2178                                         /* To Start formation of new subset string break out of inner loop */
2179                                         break;
2180                                 }
2181
2182                         } while (i < id_set_count);
2183
2184                         /* Flow comes here in two cases :
2185                         1. id_set ended and has continuous numbers at end of id_set so form subset string by first_uid:last_uid . in this  case last_uid == current_uid
2186                         2. due to break statement */
2187
2188                         if (last_uid == current_uid) {
2189                                 /* Case 1 */
2190
2191                                 memset(subset_string, 0x00, max_subset_string_size);
2192                                 SNPRINTF(subset_string, max_subset_string_size, "%lu:%lu", first_uid, last_uid);
2193
2194                                 if (false == emcore_append_subset_string_to_uid_range(subset_string, &current_node, uid_range_set, range_len, first_uid, last_uid)) {
2195                                         EM_DEBUG_EXCEPTION("emcore_append_subset_string_to_uid_range failed");
2196                                         goto FINISH_OFF;
2197                                 }
2198                         }
2199                         else {
2200                                 /* Case 2: Do Nothing */
2201                         }
2202
2203                 }
2204         } while (i < id_set_count);
2205
2206         ret = true;
2207
2208 FINISH_OFF:
2209         if (NULL != err_code)
2210                 *err_code = error;
2211         EM_DEBUG_FUNC_END();
2212         return ret;
2213
2214 }
2215
2216 /**
2217  * @fn emcore_append_subset_string_to_uid_range(char *subset_string, email_uid_range_set **uid_range_set, int range_len, unsigned long luid, unsigned long huid)
2218  * Appends the subset_string to uid range if the uid range has not exceeded maximum length(range_len), otherwise creates a new node in linked list of uid range set
2219  * and stores the subset_string in its uid_range. Also sets the lowest and highest uids for the corresponsing uid_range
2220  *
2221  * @author                                      h.gahlaut@samsung.com
2222  * @param[in] subset_string     Specifies the subset string to be appended. A subset string can be like X:Y or X where X and Y are uids.
2223  * @param[in] range_len         Specifies the maximum length of range string allowed.
2224  * @param[in] luid                      Specifies the lowest uid in subset string
2225  * @param[in] huid                      Specifies the highest uid in subset string
2226  * @param[out] uid_range_set    Returns the uid_ranges formed in the form of a linked list with head stored in uid_range_set pointer
2227  * @param[out] err_code         Returns the error code.
2228  * @remarks
2229  * @return This function returns true on success or false on failure.
2230  */
2231
2232 int emcore_append_subset_string_to_uid_range(char *subset_string, email_uid_range_set **current_node_adr, email_uid_range_set **uid_range_set, int range_len, unsigned long luid, unsigned long huid)
2233 {
2234         EM_DEBUG_FUNC_BEGIN();
2235         email_uid_range_set *current_node = NULL;
2236
2237         if (NULL == (*uid_range_set)) {
2238                 /*This happens only once when list  creation starts. Head Node is allocated */
2239                 current_node = (email_uid_range_set *)em_malloc(sizeof(email_uid_range_set));
2240                 if (NULL == current_node) {
2241                         EM_DEBUG_EXCEPTION("em_malloc failed");
2242                         return false;
2243                 }
2244
2245                 current_node->uid_range = (char *)em_malloc(range_len);
2246
2247                 if (NULL == current_node->uid_range) {
2248                         EM_DEBUG_EXCEPTION("em_malloc failed");
2249                         EM_SAFE_FREE(current_node);
2250                         return false;
2251                 }
2252
2253                 SNPRINTF(current_node->uid_range, range_len, "%s,", subset_string);
2254
2255                 current_node->lowest_uid = luid;
2256                 current_node->highest_uid = huid;
2257                 (*uid_range_set) = current_node;
2258
2259                 (*current_node_adr) = current_node;
2260
2261         }
2262         else {
2263                 /* Apart from first call to this function flow will always come here */
2264                 current_node = (*current_node_adr);
2265                 int len_sub_string = EM_SAFE_STRLEN(subset_string);
2266                 int space_left_in_buffer = range_len - EM_SAFE_STRLEN(current_node->uid_range);
2267
2268                 if ((len_sub_string + 1 + 1) <= space_left_in_buffer)   /* 1 for comma + 1 for ending null character */ {
2269                         SNPRINTF(current_node->uid_range + EM_SAFE_STRLEN(current_node->uid_range), space_left_in_buffer, "%s,", subset_string);
2270                         current_node->highest_uid = huid;
2271                 }
2272                 else {
2273                         /* No more space left in uid_range string.If continued on it, it will exceeded max size of range_len */
2274                         /* Allocate new node in Uid Range set */
2275                         email_uid_range_set *new_node = NULL;
2276
2277                         new_node = (email_uid_range_set *)em_malloc(sizeof(email_uid_range_set));
2278
2279                         if (NULL == new_node) {
2280                                 EM_DEBUG_EXCEPTION("em_malloc failed");
2281                                 return false;
2282                         }
2283
2284                         /* Allocate uid_range of new node */
2285
2286                         new_node->uid_range =  (char *)em_malloc(range_len);
2287
2288                         if (NULL == new_node->uid_range) {
2289                                 EM_DEBUG_EXCEPTION("em_malloc failed");
2290                                 EM_SAFE_FREE(new_node);
2291                                 return false;
2292                         }
2293
2294                         SNPRINTF(new_node->uid_range, range_len, "%s, ", subset_string);
2295
2296                         new_node->lowest_uid = luid;
2297                         new_node->highest_uid = huid;
2298
2299                         current_node->next = new_node;
2300
2301                         (*current_node_adr) = new_node;
2302                 }
2303         }
2304         EM_DEBUG_FUNC_END();
2305         return true;
2306 }
2307
2308 /**
2309  * void emcore_free_uid_range_set(email_uid_range_set **uid_range_head)
2310  * Frees the linked list of uid ranges
2311  *
2312  * @author                                      h.gahlaut@samsung.com
2313  * @param[in] uid_range_head    Head pointer of linked list of uid ranges
2314  * @remarks
2315  * @return This function does not return anything.
2316  */
2317
2318 INTERNAL_FUNC
2319 void emcore_free_uid_range_set(email_uid_range_set **uid_range_set)
2320 {
2321         EM_DEBUG_FUNC_BEGIN();
2322
2323         email_uid_range_set *current_node = NULL;
2324         email_uid_range_set *uid_range_head = NULL;
2325
2326         current_node = uid_range_head = (*uid_range_set);       /* Make the current node and head ptr point to starting of  uid_range_set */
2327
2328         while (current_node) {
2329                 uid_range_head = current_node->next;            /* Move the head ptr to next node*/
2330
2331                 EM_SAFE_FREE(current_node->uid_range);
2332                 EM_SAFE_FREE(current_node);                             /* Free the current node */
2333
2334                 current_node = uid_range_head;                  /* Make the current node point to head ptr */
2335         }
2336
2337         (*uid_range_set) = NULL;
2338         EM_DEBUG_FUNC_END();
2339 }
2340
2341
2342 /**
2343  * @fn emcore_form_comma_separated_strings(int numbers[], int num_count, int max_string_len, char *** strings, int *string_count, int *err_code)
2344  * Forms comma separated strings of a give max_string_len from an array of numbers
2345  *
2346  * @author                                      h.gahlaut@samsung.com
2347  * @param[in] numbers                   Specifies the array of numbers to be converted into comma separated strings.
2348  * @param[in] num_count         Specifies the count of numbers in numbers array.
2349  * @param[in] max_string_len    Specifies the maximum length of comma separated strings that are to be formed.
2350  * @param[out] strings                  Returns the base address of a double dimension array which stores the strings.
2351  * @param[out] string_count             Returns the number of strings formed.
2352  * @param[out] err_code         Returns the error code.
2353  * @remarks                                     If Input to the function is five numbers like 2755 2754 2748 2749 2750 and a given max_string_len is 20.
2354  *                                                      Then this function will form two comma separated strings as follows -
2355  *                                                      "2755, 2754, 2748"
2356  *                                                      "2749, 2750"
2357  * @return This function returns true on success or false on failure.
2358  */
2359
2360 INTERNAL_FUNC int emcore_form_comma_separated_strings(int numbers[], int num_count, int max_string_len, char *** strings, int *string_count, int *err_code)
2361 {
2362         EM_DEBUG_FUNC_BEGIN();
2363
2364         int error = EMAIL_ERROR_NONE;
2365         int ret = false;
2366
2367         char **string_list = NULL;
2368         int num_of_strings = 0;
2369         int i = 0;
2370         int j =0;
2371         char num[MAX_INTEGER_LENGTH + 1] = {0, };
2372         int num_len = 0;
2373         int space_in_buffer = 0;
2374         int len_of_string_formed = 0;
2375
2376         if (NULL == numbers || num_count <= 0 || \
2377                 max_string_len < (MAX_INTEGER_LENGTH + 2)|| NULL == strings || NULL == string_count)                    /*  32767, is the highest integer possible in string.This requires 7 bytes of storage in character type array (1 byte for ending NULL and 1 byte for ending comma) so max_string_len should not be less than worst case possible.  */ {
2378                 EM_DEBUG_EXCEPTION("Invalid Parameter numbers[%p] num_count [%d] max_string_len [%d] strings [%p] string_count[%p]", \
2379                         numbers, num_count, max_string_len, strings, string_count);
2380                 error = EMAIL_ERROR_INVALID_PARAM;
2381                 goto FINISH_OFF;
2382         }
2383
2384         EM_DEBUG_LOG("num_count [%d] max_string_len [%d]", num_count, max_string_len);
2385
2386         string_list = em_malloc(sizeof(char *));
2387
2388         if (NULL == string_list) {
2389                 EM_DEBUG_EXCEPTION("em_malloc failed ");
2390                 goto FINISH_OFF;
2391         }
2392
2393         string_list[num_of_strings] = em_malloc(max_string_len);
2394
2395         if (NULL == string_list[num_of_strings]) {
2396                 EM_DEBUG_EXCEPTION("em_malloc failed ");
2397                 goto FINISH_OFF;
2398         }
2399
2400         ++num_of_strings;
2401         space_in_buffer = max_string_len;
2402
2403         for (j = 0; j < num_count;++j) {
2404                 memset(num, 0x00, MAX_INTEGER_LENGTH + 1);
2405                 SNPRINTF(num, MAX_INTEGER_LENGTH + 1, "%d", numbers[j]);
2406
2407                 num_len = EM_SAFE_STRLEN(num);
2408
2409                 len_of_string_formed = EM_SAFE_STRLEN(string_list[num_of_strings - 1]);
2410
2411                 space_in_buffer = max_string_len - len_of_string_formed ;
2412
2413                 if (space_in_buffer >= (num_len+1+1))                   /*  1 for comma and 1 for ending NULL */ {
2414                         SNPRINTF(string_list[num_of_strings - 1] + len_of_string_formed, max_string_len, "%d,", numbers[j]);
2415                 }
2416                 else {  /*  Removing comma at end of string  */
2417                         string_list[num_of_strings - 1][len_of_string_formed-1] = '\0';
2418                         char **temp = NULL;
2419                         temp = (char **)realloc(string_list, sizeof(char *) * (num_of_strings + 1));    /*  Allocate new buffer to store a pointer to a new string */
2420
2421                         if (NULL == temp) {
2422                                 EM_DEBUG_EXCEPTION("realloc failed");
2423                                 goto FINISH_OFF;
2424                         }
2425
2426                         memset(temp + num_of_strings, 0X00, sizeof(char *));
2427
2428                         string_list = temp;
2429                         temp = NULL;
2430                         string_list[num_of_strings] = em_malloc(max_string_len);/*  Allocate new buffer to store the string */
2431
2432                         if (NULL == string_list[num_of_strings]) {
2433                                 EM_DEBUG_EXCEPTION(" em_malloc failed ");
2434                                 goto FINISH_OFF;
2435                         }
2436                         ++num_of_strings;
2437                         SNPRINTF(string_list[num_of_strings - 1] , max_string_len, "%d,", numbers[j]);/*  Start making new string */
2438                 }
2439         }
2440
2441         /*  Removing comma at end of string  */
2442         len_of_string_formed = EM_SAFE_STRLEN(string_list[num_of_strings - 1]);
2443         string_list[num_of_strings - 1][len_of_string_formed-1] = '\0';
2444         ret = true;
2445
2446 FINISH_OFF:
2447
2448         if (false == ret)
2449                 emcore_free_comma_separated_strings(&string_list, &num_of_strings);
2450
2451         if (true == ret) {
2452                 for (i = 0; i < num_of_strings;++i)
2453                         EM_DEBUG_LOG("%s", string_list[i]);
2454                 *strings = string_list;
2455                 *string_count = num_of_strings;
2456         }
2457
2458
2459         if (NULL != err_code)
2460                 *err_code = error;
2461
2462         EM_DEBUG_FUNC_END("ret [%d]", ret);
2463         return ret;
2464 }
2465 /**
2466  * @fn emcore_free_comma_separated_strings(char *** string_list, int *string_count)
2467  * Frees the double dimensional array of strings.
2468  *
2469  * @author                                      h.gahlaut@samsung.com
2470  * @param[in] uid_range_head    Address of base address of double dimensional array of strings.
2471  * @param[in] string_count              Address of variable holding the count of strings.
2472  * @remarks
2473  * @return This function does not return anything.
2474  */
2475 INTERNAL_FUNC void emcore_free_comma_separated_strings(char *** string_list, int *string_count)
2476 {
2477         EM_DEBUG_FUNC_BEGIN();
2478         int i = 0;
2479         char **str_list = NULL;
2480         int count = 0;
2481
2482         if (NULL != string_list) {
2483                 str_list = *string_list;
2484
2485                 if (0 != *string_count) {
2486                         count = *string_count;
2487                         for (i = 0; i < count; ++i)
2488                                 EM_SAFE_FREE(str_list[i]);
2489                 }
2490
2491                 EM_SAFE_FREE(str_list);
2492                 *string_list = NULL;    /*  This makes the pointer to be freed as NULL in calling function and saves it from being a dangling pointer for sometime in calling function */
2493                 *string_count = 0;
2494         }
2495         EM_DEBUG_FUNC_END();
2496 }
2497
2498
2499 #endif
2500
2501
2502
2503
2504 int emcore_make_attachment_file_name_with_extension(char *source_file_name, char *sub_type, char *result_file_name, int result_file_name_buffer_length, int *err_code)
2505 {
2506         EM_DEBUG_FUNC_BEGIN("source_file_name[%s], sub_type[%s], result_file_name_buffer_length[%d] ", source_file_name, sub_type, result_file_name_buffer_length);
2507         int ret = false, err = EMAIL_ERROR_NONE;
2508         char *extcheck = NULL;
2509         char attachment_file_name[MAX_PATH + 1] = { 0, };
2510
2511         if (!source_file_name || !result_file_name) {
2512                 EM_DEBUG_EXCEPTION("Invalid Parameter");
2513                 err  = EMAIL_ERROR_INVALID_PARAM;
2514                 goto FINISH_OFF;
2515         }
2516
2517         strncpy(attachment_file_name, source_file_name, MAX_PATH);
2518         extcheck = strchr(attachment_file_name, '.');
2519
2520         if (extcheck)
2521                 EM_DEBUG_LOG("Extension Exist in the Attachment [%s] ", extcheck);
2522         else  { /* No extension attached, So add the Extension based on the subtype */
2523                 if (sub_type) {
2524                         strcat(attachment_file_name, ".");
2525                         strcat(attachment_file_name, sub_type);
2526                         EM_DEBUG_LOG("attachment_file_name with extension[%s] ", attachment_file_name);
2527                 }
2528                 else
2529                         EM_DEBUG_LOG("UnKnown Extesnsion");
2530
2531         }
2532         memset(result_file_name, 0 , result_file_name_buffer_length);
2533         EM_SAFE_STRNCPY(result_file_name, attachment_file_name, result_file_name_buffer_length - 1);
2534         EM_DEBUG_LOG("*result_file_name[%s]", result_file_name);
2535         ret = true;
2536
2537 FINISH_OFF:
2538         if (err_code)
2539                 *err_code = err;
2540         EM_DEBUG_FUNC_END();
2541         return ret;
2542 }
2543
2544 #ifdef __FEATURE_LOCAL_ACTIVITY__
2545 INTERNAL_FUNC int emcore_add_activity(emstorage_activity_tbl_t *new_activity, int *err_code)
2546 {
2547         EM_DEBUG_FUNC_BEGIN();
2548
2549         EM_DEBUG_LOG("\t new_activity[%p], err_code[%p]", new_activity, err_code);
2550
2551         /*  default variable */
2552         int ret = false;
2553         int err = EMAIL_ERROR_NONE;
2554
2555         if (!new_activity) {
2556                 EM_DEBUG_LOG("\t new_activity[%p]\n", new_activity);
2557                 err = EMAIL_ERROR_INVALID_PARAM;
2558                 goto FINISH_OFF;
2559         }
2560         if (!emstorage_add_activity(new_activity, false, &err)) {
2561                 EM_DEBUG_LOG("\t emstorage_add_activity falied - %d\n", err);
2562
2563                 goto FINISH_OFF;
2564         }
2565         ret = true;
2566
2567 FINISH_OFF:
2568         if (err_code)
2569                 *err_code = err;
2570
2571         return ret;
2572 }
2573
2574 INTERNAL_FUNC int emcore_delete_activity(emstorage_activity_tbl_t *activity, int *err_code)
2575 {
2576         EM_DEBUG_FUNC_BEGIN();
2577
2578         EM_DEBUG_LOG("\t new_activity[%p], err_code[%p]", activity, err_code);
2579
2580         /*  default variable */
2581         int ret = false;
2582         int err = EMAIL_ERROR_NONE;
2583         if (!activity) {
2584                 EM_DEBUG_LOG("\t new_activity[%p]\n", activity);
2585
2586                 err = EMAIL_ERROR_INVALID_PARAM;
2587                 goto FINISH_OFF;
2588         }
2589         if (!emstorage_delete_local_activity(activity, true, &err)) {
2590                 EM_DEBUG_LOG("\t emstorage_delete_local_activity falied - %d\n", err);
2591
2592                 goto FINISH_OFF;
2593         }
2594         ret = true;
2595
2596 FINISH_OFF:
2597         if (err_code)
2598                 *err_code = err;
2599
2600         return ret;
2601 }
2602
2603 INTERNAL_FUNC int emcore_get_next_activity_id(int *activity_id, int *err_code)
2604 {
2605         EM_DEBUG_FUNC_BEGIN();
2606
2607         int ret = false;
2608         int err = EMAIL_ERROR_NONE;
2609
2610         if (NULL == activity_id)
2611         {
2612                 EM_DEBUG_EXCEPTION("\t activity_id[%p]", activity_id);
2613
2614                 err = EMAIL_ERROR_INVALID_PARAM;
2615                 goto FINISH_OFF;
2616         }
2617
2618         if (false == emstorage_get_next_activity_id(activity_id, &err)) {
2619                 EM_DEBUG_LOG("\t emstorage_get_next_activity_id failed - %d\n", err);
2620                 goto FINISH_OFF;
2621         }
2622
2623         ret = true;
2624
2625         FINISH_OFF:
2626         if (NULL != err_code) {
2627                 *err_code = err;
2628         }
2629
2630         return ret;
2631
2632 }
2633
2634 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
2635
2636
2637 INTERNAL_FUNC void emcore_free_rule(email_rule_t* rule)
2638 {
2639         EM_DEBUG_FUNC_BEGIN();
2640
2641         if (!rule)
2642                 return;
2643
2644         EM_SAFE_FREE(rule->value);
2645
2646         EM_DEBUG_FUNC_END();
2647 }
2648
2649 INTERNAL_FUNC int emcore_search_string_from_file(char *file_path, char *search_string, int *result)
2650 {
2651         EM_DEBUG_FUNC_BEGIN("file_path : [%s], search_string : [%s]", file_path, search_string);
2652         int error = EMAIL_ERROR_NONE;
2653         int file_size = 0;
2654         int p_result = false;
2655         FILE *fp = NULL;
2656         char *buf = NULL;
2657         char *stripped = NULL;
2658
2659         if (!search_string || !file_path || !result) {
2660                 EM_DEBUG_EXCEPTION("Invalid parameter");
2661                 error = EMAIL_ERROR_INVALID_PARAM;
2662                 return error;
2663         }
2664
2665         fp = fopen(file_path, "r");
2666         if (fp == NULL) {
2667                 EM_DEBUG_EXCEPTION("fopen failed");
2668                 error = EMAIL_ERROR_SYSTEM_FAILURE;
2669                 goto FINISH_OFF;
2670         }
2671
2672         if (!emcore_get_file_size(file_path, &file_size, &error)) {
2673                 EM_DEBUG_EXCEPTION("emcore_get_file_size failed");
2674                 goto FINISH_OFF;
2675         }
2676
2677         buf = em_malloc(file_size);
2678         if (buf == NULL) {
2679                 EM_DEBUG_EXCEPTION("em_malloc failed");
2680                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2681                 goto FINISH_OFF;
2682         }
2683
2684         if (fread(buf, sizeof(char), file_size, fp) != file_size) {
2685                 EM_DEBUG_EXCEPTION("Get the data from file : failed");
2686                 error = EMAIL_ERROR_SYSTEM_FAILURE;
2687                 goto FINISH_OFF;
2688         }
2689
2690         /*prevent 35586*/
2691         stripped = em_replace_all_string(buf, CRLF_STRING, "");
2692         if (strstr(stripped, search_string))
2693                 p_result = true;
2694
2695 FINISH_OFF:
2696         if(!p_result)
2697                 EM_DEBUG_LOG("Search string[%s] not found",search_string);
2698
2699         *result = p_result;
2700
2701         if (fp)
2702                 fclose(fp);
2703
2704         EM_SAFE_FREE(buf);
2705         EM_SAFE_FREE(stripped);  /*prevent 35586*/
2706
2707         EM_DEBUG_FUNC_END("error:[%d]", error);
2708         return error;
2709 }
2710
2711 INTERNAL_FUNC int emcore_load_query_from_file(char *file_path, char ***query_array, int *array_len)
2712 {
2713         EM_DEBUG_FUNC_BEGIN("file_path : [%s], query_array : [%p]", file_path, *query_array);
2714         int error = EMAIL_ERROR_NONE;
2715         int file_size = 0;
2716         int vector_len = 0;
2717         int i = 0;
2718         FILE *fp = NULL;
2719         char *buf = NULL;
2720         char **result_vector = NULL;
2721
2722         if (!file_path) {
2723                 EM_DEBUG_EXCEPTION("Invalid parameter");
2724                 error = EMAIL_ERROR_INVALID_PARAM;
2725                 return error;
2726         }
2727
2728         fp = fopen(file_path, "r");
2729         if (fp == NULL) {
2730                 EM_DEBUG_EXCEPTION("fopen failed");
2731                 error = EMAIL_ERROR_SYSTEM_FAILURE;
2732                 goto FINISH_OFF;
2733         }
2734
2735         if (!emcore_get_file_size(file_path, &file_size, &error)) {
2736                 EM_DEBUG_EXCEPTION("emcore_get_file_size failed");
2737                 goto FINISH_OFF;
2738         }
2739
2740         buf = em_malloc(file_size);
2741         if (buf == NULL) {
2742                 EM_DEBUG_EXCEPTION("em_malloc failed");
2743                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2744                 goto FINISH_OFF;
2745         }
2746
2747         if (fread(buf, sizeof(char), file_size, fp) != file_size) {
2748                 EM_DEBUG_EXCEPTION("Get the data from file : failed");
2749                 error = EMAIL_ERROR_SYSTEM_FAILURE;
2750                 goto FINISH_OFF;
2751         }
2752
2753         result_vector = g_strsplit(buf, ";", -1);
2754         if (!result_vector || g_strv_length(result_vector) <= 0) {
2755                 EM_DEBUG_EXCEPTION("Parsing sql file failed");
2756                 error = EMAIL_ERROR_SYSTEM_FAILURE;
2757                 goto FINISH_OFF;
2758         }
2759
2760         vector_len = g_strv_length(result_vector);
2761         if (vector_len <= 0) {
2762                 EM_DEBUG_EXCEPTION("vector length : [%d]", vector_len);
2763                 error = EMAIL_ERROR_SYSTEM_FAILURE;
2764                 goto FINISH_OFF;
2765         }
2766
2767         *query_array = (char **)calloc(vector_len, sizeof(char *));
2768
2769         for (i = 0; i < vector_len; i++) {
2770                 if (result_vector[i]) {
2771                         char *str = g_strconcat(result_vector[i], ";", NULL);
2772                         (*query_array)[i] = str;
2773                 }
2774         }
2775
2776         if (array_len) {
2777                 *array_len = vector_len;
2778                 EM_DEBUG_LOG("SQL string array length : [%d]", vector_len);
2779         }
2780
2781 FINISH_OFF:
2782
2783         if (fp)
2784                 fclose(fp);
2785
2786         EM_SAFE_FREE(buf);
2787
2788         if (result_vector) {
2789                 g_strfreev(result_vector);
2790         }
2791
2792         EM_DEBUG_FUNC_END("error:[%d]", error);
2793         return error;
2794 }
2795
2796 static int convert_contact_err_to_email_err(int contact_err)
2797 {
2798         int err = EMAIL_ERROR_NONE;
2799
2800         switch (contact_err) {
2801         case CONTACTS_ERROR_NONE :
2802                 err = EMAIL_ERROR_NONE;
2803                 break;
2804         case CONTACTS_ERROR_OUT_OF_MEMORY :
2805                 err = EMAIL_ERROR_OUT_OF_MEMORY;
2806                 break;
2807         case CONTACTS_ERROR_INVALID_PARAMETER :
2808                 err = EMAIL_ERROR_INVALID_PARAM;
2809                 break;
2810         case CONTACTS_ERROR_NO_DATA :
2811                 err = EMAIL_ERROR_DATA_NOT_FOUND;
2812                 break;
2813         case CONTACTS_ERROR_DB :
2814                 err = EMAIL_ERROR_DB_FAILURE;
2815                 break;
2816         case CONTACTS_ERROR_IPC :
2817                 err = EMAIL_ERROR_IPC_CONNECTION_FAILURE;
2818                 break;
2819         case CONTACTS_ERROR_SYSTEM:
2820                 err = EMAIL_ERROR_SYSTEM_FAILURE;
2821                 break;
2822         case CONTACTS_ERROR_INTERNAL:
2823         default:
2824                 err = EMAIL_ERROR_UNKNOWN;
2825                 break;
2826         }
2827         return err;
2828 }
2829
2830 int emcore_get_mail_contact_info(email_mail_contact_info_t *contact_info, char *full_address, int *err_code)
2831 {
2832         EM_DEBUG_FUNC_BEGIN("contact_info[%p], full_address[%s], err_code[%p]", contact_info, full_address, err_code);
2833         
2834         int ret = false;
2835         int err = EMAIL_ERROR_NONE;
2836         
2837         if (!emcore_get_mail_contact_info_with_update(contact_info, full_address, 0, &err))
2838                 EM_DEBUG_EXCEPTION("emcore_get_mail_contact_info_with_update failed [%d]", err);
2839         else
2840                 ret = true;
2841         
2842         if (err_code != NULL)
2843                 *err_code = err;
2844         
2845         return ret;
2846 }
2847
2848 int emcore_search_contact_info(const char *contact_uri, int address_property_id, char *address, int favorite_property_id, bool is_favorite, int limit, contacts_record_h *contacts_record)
2849 {
2850         EM_DEBUG_FUNC_BEGIN();
2851         int contact_err = CONTACTS_ERROR_NONE;
2852         
2853         contacts_query_h query = NULL;
2854         contacts_filter_h filter = NULL;
2855         contacts_list_h list = NULL;
2856
2857         if ((contact_err = contacts_query_create(contact_uri, &query)) != CONTACTS_ERROR_NONE) {
2858                 EM_DEBUG_EXCEPTION("contacts_query_create failed");
2859                 goto FINISH_OFF;
2860         }
2861         
2862         if ((contact_err = contacts_filter_create(contact_uri, &filter)) != CONTACTS_ERROR_NONE) {
2863                 EM_DEBUG_EXCEPTION("contacts_filter_create failed");
2864                 goto FINISH_OFF;
2865         }
2866
2867         if ((contact_err = contacts_filter_add_str(filter, address_property_id, CONTACTS_MATCH_EXACTLY, address)) != CONTACTS_ERROR_NONE) {
2868                 EM_DEBUG_EXCEPTION("contacts_filter_add_str failed");
2869                 goto FINISH_OFF;
2870         }
2871
2872         if ((contact_err = contacts_filter_add_operator(filter, CONTACTS_FILTER_OPERATOR_AND)) != CONTACTS_ERROR_NONE) {
2873                 EM_DEBUG_EXCEPTION("contacts_filter_add_operator failed");
2874                 goto FINISH_OFF;
2875         }
2876
2877         if ((contact_err = contacts_filter_add_bool(filter, favorite_property_id, is_favorite)) != CONTACTS_ERROR_NONE) {
2878                 EM_DEBUG_EXCEPTION("contacts_filter_add_bool failed");
2879                 goto FINISH_OFF;
2880         }
2881
2882         if ((contact_err = contacts_query_set_filter(query, filter)) != CONTACTS_ERROR_NONE) {
2883                 EM_DEBUG_EXCEPTION("contacts_query_set_filter failed");
2884                 goto FINISH_OFF;
2885         }
2886
2887         if ((contact_err = contacts_db_get_records_with_query(query, 0, limit, &list)) != CONTACTS_ERROR_NONE) {
2888                 EM_DEBUG_EXCEPTION("contacts_db_get_record_with_query failed");
2889                 goto FINISH_OFF;
2890         }
2891
2892         if ((contact_err = contacts_list_get_current_record_p(list, contacts_record)) != CONTACTS_ERROR_NONE) {
2893                 EM_DEBUG_EXCEPTION("contacts_list_get_current_record_p failed");
2894                 goto FINISH_OFF;
2895         }
2896
2897 FINISH_OFF:
2898
2899         if (query != NULL)
2900                 contacts_query_destroy(query);
2901
2902         if (filter != NULL)
2903                 contacts_filter_destroy(filter);
2904
2905         if (list != NULL)
2906                 contacts_list_destroy(list, false);
2907
2908         return contact_err;
2909 }
2910
2911 int emcore_search_contact_info_by_address(const char *contact_uri, int property_id, char *address, int limit, contacts_record_h *contacts_record)
2912 {
2913         EM_DEBUG_FUNC_BEGIN();
2914         int contact_err = CONTACTS_ERROR_NONE;
2915         
2916         contacts_query_h query = NULL;
2917         contacts_filter_h filter = NULL;
2918         contacts_list_h list = NULL;
2919
2920         if ((contact_err = contacts_query_create(contact_uri, &query)) != CONTACTS_ERROR_NONE) {
2921                 EM_DEBUG_EXCEPTION("contacts_query_create failed");
2922                 goto FINISH_OFF;
2923         }
2924         
2925         if ((contact_err = contacts_filter_create(contact_uri, &filter)) != CONTACTS_ERROR_NONE) {
2926                 EM_DEBUG_EXCEPTION("contacts_filter_create failed");
2927                 goto FINISH_OFF;
2928         }
2929
2930         if ((contact_err = contacts_filter_add_str(filter, property_id, CONTACTS_MATCH_EXACTLY, address)) != CONTACTS_ERROR_NONE) {
2931                 EM_DEBUG_EXCEPTION("contacts_filter_add_str failed");
2932                 goto FINISH_OFF;
2933         }
2934
2935         if ((contact_err = contacts_query_set_filter(query, filter)) != CONTACTS_ERROR_NONE) {
2936                 EM_DEBUG_EXCEPTION("contacts_query_set_filter failed");
2937                 goto FINISH_OFF;
2938         }
2939
2940         if ((contact_err = contacts_db_get_records_with_query(query, 0, limit, &list)) != CONTACTS_ERROR_NONE) {
2941                 EM_DEBUG_EXCEPTION("contacts_db_get_record_with_query failed");
2942                 goto FINISH_OFF;
2943         }
2944
2945         if ((contact_err = contacts_list_get_current_record_p(list, contacts_record)) != CONTACTS_ERROR_NONE) {
2946                 EM_DEBUG_EXCEPTION("contacts_list_get_current_record_p failed");
2947                 goto FINISH_OFF;
2948         }
2949
2950 FINISH_OFF:
2951
2952         if (query != NULL)
2953                 contacts_query_destroy(query);
2954
2955         if (filter != NULL)
2956                 contacts_filter_destroy(filter);
2957
2958         if (list != NULL)
2959                 contacts_list_destroy(list, false);
2960
2961         return contact_err;
2962 }
2963
2964 int emcore_set_contacts_log(int account_id, char *email_address, char *subject, time_t date_time, email_action_t action)
2965 {
2966         EM_DEBUG_FUNC_BEGIN("account_id : [%d], address : [%p], subject : [%s], action : [%d], date_time : [%d]", account_id, email_address, subject, action, (int)date_time);
2967         
2968         int err = EMAIL_ERROR_NONE;
2969         int contacts_error = CONTACTS_ERROR_NONE;
2970         int person_id = 0;
2971         int action_type = 0;
2972         
2973         contacts_record_h phone_record = NULL;
2974         contacts_record_h person_record = NULL; 
2975
2976         if ((contacts_error = contacts_connect2()) != CONTACTS_ERROR_NONE) {
2977                 EM_DEBUG_EXCEPTION("Open connect service failed [%d]", contacts_error);
2978                 goto FINISH_OFF;
2979         }
2980
2981         if ((contacts_error = contacts_record_create(_contacts_phone_log._uri, &phone_record)) != CONTACTS_ERROR_NONE) {
2982                 EM_DEBUG_EXCEPTION("contacts_query_create failed [%d]", contacts_error);
2983                 goto FINISH_OFF;
2984         }
2985
2986         /* Set email address */
2987         if ((contacts_error = contacts_record_set_str(phone_record, _contacts_phone_log.address, email_address)) != CONTACTS_ERROR_NONE) {
2988                 EM_DEBUG_EXCEPTION("contacts_record_set_str [address] failed [%d]", contacts_error);
2989                 goto FINISH_OFF;
2990         }
2991
2992         /* Search contact person info */
2993         if ((contacts_error = emcore_search_contact_info_by_address(_contacts_person_email._uri, _contacts_person_email.email, email_address, 1, &person_record)) != CONTACTS_ERROR_NONE) {
2994                 EM_DEBUG_EXCEPTION("emcore_search_contact_info_by_address failed [%d]", contacts_error);
2995                 EM_DEBUG_LOG("Not match person");
2996         } else {
2997                 /* Get person_id in contacts_person_email record  */
2998                 if (person_record  && (contacts_error = contacts_record_get_int(person_record, _contacts_person_email.person_id, &person_id)) != CONTACTS_ERROR_NONE) {
2999                         EM_DEBUG_EXCEPTION("contacts_record_get_str failed [%d]", contacts_error);
3000                         goto FINISH_OFF;
3001                 }
3002
3003                 /* Set the person id */
3004                 if ((contacts_error = contacts_record_set_int(phone_record, _contacts_phone_log.person_id, person_id)) != CONTACTS_ERROR_NONE) {
3005                         EM_DEBUG_EXCEPTION("contacts_record_set_int [person id] failed [%d]", contacts_error);
3006                         goto FINISH_OFF;
3007                 }
3008         }
3009
3010         switch (action) {
3011         case EMAIL_ACTION_SEND_MAIL :
3012                 action_type = CONTACTS_PLOG_TYPE_EMAIL_SENT;
3013                 break;
3014         case EMAIL_ACTION_SYNC_HEADER :
3015                 action_type = CONTACTS_PLOG_TYPE_EMAIL_RECEIVED;
3016                 break;
3017         default :
3018                 EM_DEBUG_EXCEPTION("Unknown action type");
3019                 goto FINISH_OFF;
3020         }
3021
3022         /* Set log type */
3023         if ((contacts_error = contacts_record_set_int(phone_record, _contacts_phone_log.log_type, action_type)) != CONTACTS_ERROR_NONE) {
3024                 EM_DEBUG_EXCEPTION("contacts_record_set_int [log_type] failed [%d]", contacts_error);
3025                 goto FINISH_OFF;
3026         }
3027
3028         /* Set log time */
3029         if ((contacts_error = contacts_record_set_int(phone_record, _contacts_phone_log.log_time, (int)date_time)) != CONTACTS_ERROR_NONE) {
3030                 EM_DEBUG_EXCEPTION("contacts_record_set_str [address] failed [%d]", contacts_error);
3031                 goto FINISH_OFF;
3032         }
3033
3034         /* Set subject */
3035         if ((contacts_error = contacts_record_set_str(phone_record, _contacts_phone_log.extra_data2, subject)) != CONTACTS_ERROR_NONE) {
3036                 EM_DEBUG_EXCEPTION("contacts_record_set_str [subject] failed [%d]", contacts_error);
3037                 goto FINISH_OFF;
3038         }
3039
3040         /* Set Mail id */
3041         if ((contacts_error = contacts_record_set_int(phone_record, _contacts_phone_log.extra_data1, account_id)) != CONTACTS_ERROR_NONE) {
3042                 EM_DEBUG_EXCEPTION("contacts_record_set_int [mail id] failed [%d]", contacts_error);
3043                 goto FINISH_OFF;
3044         }
3045
3046         /* Insert the record in DB */
3047         if ((contacts_error = contacts_db_insert_record(phone_record, NULL)) != CONTACTS_ERROR_NONE) {
3048                 EM_DEBUG_EXCEPTION("contacts_db_insert_record failed [%d]",contacts_error );
3049                 goto FINISH_OFF;
3050         }
3051
3052 FINISH_OFF:
3053
3054         if (phone_record != NULL)
3055                 contacts_record_destroy(phone_record, false);
3056
3057         if (person_record != NULL)
3058                 contacts_record_destroy(person_record, false);
3059
3060         err = convert_contact_err_to_email_err(contacts_error);
3061
3062         if ((contacts_error = contacts_disconnect2()) != CONTACTS_ERROR_NONE) {
3063                 EM_DEBUG_EXCEPTION("Open connect service failed [%d]", contacts_error);
3064                 err = convert_contact_err_to_email_err(contacts_error);
3065         }
3066
3067         EM_DEBUG_FUNC_END("err [%d]", err);
3068         return err;
3069 }
3070
3071 INTERNAL_FUNC int emcore_set_sent_contacts_log(emstorage_mail_tbl_t *input_mail_data)
3072 {
3073         EM_DEBUG_FUNC_BEGIN("input_mail_data : [%p]", input_mail_data);
3074         
3075         int i = 0;
3076         int err = EMAIL_ERROR_NONE;
3077         char email_address[MAX_EMAIL_ADDRESS_LENGTH];
3078         char *address_array[3] = {input_mail_data->full_address_to, input_mail_data->full_address_cc, input_mail_data->full_address_bcc};
3079         ADDRESS *addr = NULL;
3080         ADDRESS *p_addr = NULL;
3081
3082         for (i = 0; i < 3; i++) {
3083                 if (address_array[i] && address_array[i][0] != 0) {
3084                         rfc822_parse_adrlist(&addr, address_array[i], NULL);
3085                         for (p_addr = addr ; p_addr ;p_addr = p_addr->next) {
3086                                 SNPRINTF(email_address, MAX_EMAIL_ADDRESS_LENGTH, "%s@%s", addr->mailbox, addr->host);
3087                                 if ((err = emcore_set_contacts_log(input_mail_data->account_id, email_address, input_mail_data->subject, input_mail_data->date_time, EMAIL_ACTION_SEND_MAIL)) != EMAIL_ERROR_NONE) {
3088                                         EM_DEBUG_EXCEPTION("emcore_set_contacts_log failed : [%d]", err);
3089                                         goto FINISH_OFF;
3090                                 }
3091                         }
3092                 
3093                         if (addr) {     
3094                                 mail_free_address(&addr);
3095                                 addr = NULL;
3096                         }
3097                 }
3098         }
3099
3100 FINISH_OFF:
3101
3102         if (addr) 
3103                 mail_free_address(&addr);
3104
3105         EM_DEBUG_FUNC_END("err [%d]", err);
3106         return err;
3107 }
3108
3109 INTERNAL_FUNC int emcore_set_received_contacts_log(emstorage_mail_tbl_t *input_mail_data)
3110 {
3111         EM_DEBUG_FUNC_BEGIN("input_mail_data : [%p]", input_mail_data);
3112         int err = EMAIL_ERROR_NONE;
3113
3114         if ((err = emcore_set_contacts_log(input_mail_data->account_id, input_mail_data->email_address_sender, input_mail_data->subject, input_mail_data->date_time, EMAIL_ACTION_SYNC_HEADER)) != EMAIL_ERROR_NONE) {
3115                 EM_DEBUG_EXCEPTION("emcore_set_contacts_log failed [%d]", err);
3116         }
3117
3118         EM_DEBUG_FUNC_END("err [%d]", err);
3119         return err;
3120 }
3121
3122 INTERNAL_FUNC int emcore_delete_contacts_log(int account_id)
3123 {
3124         EM_DEBUG_FUNC_BEGIN("account_id [%d]", account_id);
3125
3126         int err = EMAIL_ERROR_NONE;
3127         int contacts_error = CONTACTS_ERROR_NONE;
3128
3129         if ((contacts_error = contacts_connect2()) != CONTACTS_ERROR_NONE) {
3130                 EM_DEBUG_EXCEPTION("Open connect service failed [%d]", contacts_error);
3131                 goto FINISH_OFF;
3132         }
3133         
3134         /* Delete record of the account id */
3135         if ((contacts_error = contacts_phone_log_delete(CONTACTS_PHONE_LOG_DELETE_BY_EMAIL_EXTRA_DATA1, account_id)) != CONTACTS_ERROR_NONE) {
3136                 EM_DEBUG_EXCEPTION("contacts_phone_log_delete failed [%d]", contacts_error);
3137         }
3138         
3139
3140 FINISH_OFF:
3141         err = convert_contact_err_to_email_err(contacts_error);
3142
3143         if ((contacts_error = contacts_disconnect2()) != CONTACTS_ERROR_NONE) {
3144                 EM_DEBUG_EXCEPTION("Open connect service failed [%d]", contacts_error);
3145                 err = convert_contact_err_to_email_err(contacts_error);
3146         }
3147
3148         EM_DEBUG_FUNC_END("err [%d]", err);
3149         return err;
3150 }
3151
3152 INTERNAL_FUNC int emcore_get_mail_display_name(char *email_address, char **contact_display_name, int *err_code)
3153 {
3154         EM_DEBUG_FUNC_BEGIN("contact_name_value[%s], contact_display_name[%p]", email_address, contact_display_name);
3155
3156         int contact_err = 0;
3157         int ret = false;
3158         char *display = NULL;
3159         /* Contact variable */
3160         contacts_record_h record = NULL;
3161
3162         if ((contact_err = contacts_connect2()) != CONTACTS_ERROR_NONE) {
3163                 EM_DEBUG_EXCEPTION("Open connect service failed [%d]", contact_err);
3164                 goto FINISH_OFF;
3165         }
3166
3167         if ((contact_err = emcore_search_contact_info_by_address(_contacts_contact_email._uri, _contacts_contact_email.email, email_address, 1, &record)) != CONTACTS_ERROR_NONE) {
3168                 EM_DEBUG_EXCEPTION("emcore_search_contact_info_by_address failed");
3169                 goto FINISH_OFF;
3170         }
3171 /*
3172         if ((contact_err = contacts_list_get_count(list, &list_count)) != CONTACTS_ERROR_NONE) {
3173                 EM_DEBUG_EXCEPTION("contacts_list_get_count failed");
3174                 goto FINISH_OFF;
3175         }
3176
3177         if (list_count > 1) {
3178                 EM_DEBUG_EXCEPTION("Duplicated contacts info");
3179                 contact_err = CONTACTS_ERROR_INTERNAL;
3180                 goto FINISH_OFF;
3181         } else if (list_count == 0) {
3182                 EM_DEBUG_EXCEPTION("Not found contact info");
3183                 contact_err = CONTACTS_ERROR_NO_DATA;
3184                 goto FINISH_OFF;
3185         }
3186 */
3187
3188         if (contacts_record_get_str(record, _contacts_contact_email.display_name, &display) != CONTACTS_ERROR_NONE) {
3189                 EM_DEBUG_EXCEPTION("contacts_record_get_str failed");
3190                 goto FINISH_OFF;
3191         }
3192
3193         ret = true;
3194
3195 FINISH_OFF:
3196
3197         contacts_disconnect2();
3198
3199         if (record != NULL)
3200                 contacts_record_destroy(record, false);
3201
3202         if (ret) {
3203                 *contact_display_name = display;
3204         } else {
3205                 *contact_display_name = NULL;
3206                 EM_SAFE_FREE(display);
3207         }
3208
3209         if (err_code != NULL)
3210                 *err_code = convert_contact_err_to_email_err(contact_err);
3211
3212         return ret;
3213 }
3214
3215
3216 #ifdef __FEATURE_BLOCKING_MODE__
3217
3218 #define ALLOWED_CONTACT_TYPE_NONE      0
3219 #define ALLOWED_CONTACT_TYPE_ALL       1
3220 #define ALLOWED_CONTACT_TYPE_FAVORITES 2
3221 #define ALLOWED_CONTACT_TYPE_CUSTOM    3
3222
3223 static int blocking_mode_status = false;
3224 static int blocking_mode_of_setting = false;
3225
3226 INTERNAL_FUNC bool emcore_init_blocking_mode_status()
3227 {
3228         EM_DEBUG_FUNC_BEGIN("blocking_mode_of_setting : [%d]", blocking_mode_of_setting);
3229
3230         if (vconf_get_bool(VCONFKEY_SETAPPL_BLOCKINGMODE_NOTIFICATIONS, &blocking_mode_of_setting) != 0) {
3231                 EM_DEBUG_EXCEPTION("vconf_get_bool failed");
3232                 return false;
3233         }
3234
3235         EM_DEBUG_FUNC_END();
3236         return true;
3237 }
3238
3239 INTERNAL_FUNC void emcore_set_blocking_mode_of_setting(int input_blocking_mode_of_setting)
3240 {
3241         blocking_mode_of_setting = input_blocking_mode_of_setting;
3242 }
3243
3244 INTERNAL_FUNC int emcore_get_blocking_mode_status()
3245 {
3246         EM_DEBUG_FUNC_BEGIN("blocking_mode_status : [%d]", blocking_mode_status);
3247         EM_DEBUG_FUNC_END();
3248
3249         return blocking_mode_status;
3250 }
3251
3252 INTERNAL_FUNC void emcore_set_blocking_mode_status(int blocking_mode)
3253 {
3254         blocking_mode_status = blocking_mode;
3255 }
3256
3257 INTERNAL_FUNC int emcore_check_blocking_mode(char *sender_address, int *blocking_status)
3258 {
3259         EM_DEBUG_FUNC_BEGIN();
3260         int err = EMAIL_ERROR_NONE;
3261         int contact_error = 0;
3262         int person_id = 0;
3263         int allowed_contact_type = 0; /* 0 : NONE, 1 : All contacts, 2 : Favorites, 3 : Custom */
3264         char *person_id_string = NULL;
3265         char *str = NULL;
3266         char *token = NULL;
3267         contacts_record_h record = NULL;
3268
3269         if (!blocking_mode_of_setting) {
3270                 EM_DEBUG_LOG("Blocking mode is OFF");
3271                 return err;             
3272         }
3273
3274         if (vconf_get_int(VCONFKEY_SETAPPL_BLOCKINGMODE_ALLOWED_CONTACT_TYPE, &allowed_contact_type) != 0) {
3275                 EM_DEBUG_EXCEPTION("vconf_get_int failed");
3276                 err = EMAIL_ERROR_SYSTEM_FAILURE;
3277                 goto FINISH_OFF;
3278         }
3279
3280         if ((contact_error = contacts_connect2()) != CONTACTS_ERROR_NONE) {
3281                 EM_DEBUG_EXCEPTION("Open connect service failed [%d]", contact_error);
3282                 goto FINISH_OFF;
3283         }
3284
3285         switch (allowed_contact_type) {
3286         case ALLOWED_CONTACT_TYPE_NONE :
3287                 EM_DEBUG_LOG("allowed_contact_type is none : bloacking all(notification)");
3288                 *blocking_status = true;
3289                 break;
3290         case ALLOWED_CONTACT_TYPE_ALL :
3291                 EM_DEBUG_LOG("allowed_contact_type is ALL");
3292                 /* Search the allowed contact type in contact DB */
3293                 if ((contact_error = emcore_search_contact_info_by_address(_contacts_person_email._uri, _contacts_person_email.email, sender_address, 1, &record)) != CONTACTS_ERROR_NONE) {
3294                         EM_DEBUG_EXCEPTION("emcore_search_contact_info_by_address failed : [%d]", contact_error);
3295                         goto FINISH_OFF;
3296                 }
3297
3298                 if (record == NULL) {
3299                         EM_DEBUG_LOG("No search contact info");
3300                         goto FINISH_OFF; 
3301                 }
3302
3303                 *blocking_status = false;
3304                 break;
3305
3306         case ALLOWED_CONTACT_TYPE_FAVORITES :
3307                 if ((contact_error = emcore_search_contact_info(_contacts_person_email._uri, _contacts_person_email.email, sender_address, _contacts_person_email.is_favorite, true, 1, &record)) != CONTACTS_ERROR_NONE) {
3308                         EM_DEBUG_EXCEPTION("emcore_search_contact_info failed : [%d]", contact_error);
3309                         goto FINISH_OFF;
3310                 }
3311
3312                 if (record == NULL) {
3313                         EM_DEBUG_LOG("No search contact info");
3314                         goto FINISH_OFF; 
3315                 }
3316
3317                 *blocking_status = false;
3318                 break;
3319
3320         case ALLOWED_CONTACT_TYPE_CUSTOM :
3321                 person_id_string = vconf_get_str(VCONFKEY_SETAPPL_BLOCKINGMODE_ALLOWED_CONTACT_LIST);
3322                 if (person_id_string == NULL) {
3323                         EM_DEBUG_LOG("Custom allowed contact type is NULL");
3324                         goto FINISH_OFF;
3325                 }       
3326
3327                 if ((contact_error = emcore_search_contact_info_by_address(_contacts_person_email._uri, _contacts_person_email.email, sender_address, 1, &record)) != CONTACTS_ERROR_NONE) {
3328                         EM_DEBUG_EXCEPTION("emcore_search_contact_info_by_address failed : [%d]", contact_error);
3329                         goto FINISH_OFF;
3330                 }
3331
3332                 if (record == NULL) {
3333                         EM_DEBUG_LOG("No search contact info");
3334                         goto FINISH_OFF; 
3335                 }       
3336
3337                 if (contacts_record_get_int(record, _contacts_contact_email.person_id, &person_id) != CONTACTS_ERROR_NONE) {
3338                         EM_DEBUG_EXCEPTION("contacts_record_get_str failed");
3339                         goto FINISH_OFF;
3340                 }
3341
3342                 token = strtok_r(person_id_string, ",", &str);
3343                 do {
3344                         if (person_id == atoi(token)) {
3345                                 *blocking_status = false;
3346                                 break;                  
3347                         }
3348                 } while ((token = strtok_r(NULL, ",", &str)));
3349
3350                 break;
3351
3352         default:
3353                 EM_DEBUG_EXCEPTION("Invalid parameter : [%d]", allowed_contact_type);
3354                 *blocking_status = true;
3355                 err = EMAIL_ERROR_INVALID_PARAM;
3356         }
3357         
3358 FINISH_OFF :
3359
3360         contacts_disconnect2();
3361
3362         err = convert_contact_err_to_email_err(contact_error);
3363
3364         EM_DEBUG_FUNC_END();
3365         return err;
3366 }
3367
3368 #endif     /* __FEATURE_BLOCKING_MODE__ */
3369 /* EOF */