b8172e32557395666a31db97a2c968bf2b7b477f
[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_MEETING_REQUEST_STATUS || 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                                         goto FINISH_OFF;
1407                                 }
1408
1409                                 EM_DEBUG_LOG("Extract the preview text, again");
1410
1411                                 utf8_encoded_string = (char *)g_convert(buf, byte_read, "UTF-8", encoding_type, &byte_read, &byte_written, &glib_error);
1412                                 if (utf8_encoded_string == NULL) {
1413                                         EM_DEBUG_EXCEPTION("g_convert failed : byte_read[%d]", byte_read);
1414                                         goto FINISH_OFF;
1415                                 }
1416                         }
1417                         EM_SAFE_FREE(buf);
1418
1419                         *stripped_text = EM_SAFE_STRDUP(utf8_encoded_string);
1420                 } else {
1421                         *stripped_text = EM_SAFE_STRDUP(buf);
1422                 }
1423         }
1424
1425         ret = true;
1426
1427 FINISH_OFF:
1428
1429         if (err_code)
1430                 *err_code = err;
1431
1432         EM_SAFE_FREE(buf);
1433         EM_SAFE_FREE(encoding_type);
1434         EM_SAFE_FREE(utf8_encoded_string);
1435
1436         if (fp_html != NULL)
1437                 fclose(fp_html);
1438
1439         if (fp_plain != NULL)
1440                 fclose(fp_plain);
1441
1442         EM_DEBUG_FUNC_END("ret [%d]", ret);
1443         return ret;
1444 }
1445 #endif
1446
1447
1448 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)
1449 {
1450         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);
1451
1452         int          err = EMAIL_ERROR_NONE;
1453         unsigned int byte_read = 0;
1454         unsigned int byte_written = 0;
1455         int          local_preview_buffer_length = 0;
1456         char        *local_preview_text = NULL;
1457         char        *encoding_type = NULL;
1458         char        *utf8_encoded_string = NULL;
1459         FILE        *fp_html = NULL;
1460         FILE        *fp_plain = NULL;
1461         GError      *glib_error = NULL;
1462         struct stat  st_buf;
1463
1464         if (!output_preview_buffer) {
1465                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1466                 err = EMAIL_ERROR_INVALID_PARAM;
1467                 goto FINISH_OFF;
1468         }
1469
1470         local_preview_buffer_length = input_preview_buffer_length * 2;
1471
1472         if ( input_html_path ) { /*prevent 26249*/
1473                 /*      get preview text from html file */
1474                 if( (err = em_get_encoding_type_from_file_path(input_html_path, &encoding_type)) != EMAIL_ERROR_NONE) {
1475                         EM_DEBUG_EXCEPTION("em_get_encoding_type_from_file_path failed [%s]", err);
1476                         goto FINISH_OFF;
1477                 }
1478
1479                 if (stat(input_html_path, &st_buf) < 0)  {
1480                         EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", input_html_path);
1481                         err = EMAIL_ERROR_FILE_NOT_FOUND;
1482                         goto FINISH_OFF;
1483                 }
1484
1485                 if (!(fp_html = fopen(input_html_path, "r")))   {
1486                         EM_DEBUG_EXCEPTION("fopen failed [%s]", input_html_path);
1487                         err = EMAIL_ERROR_SYSTEM_FAILURE;
1488                         goto FINISH_OFF;
1489                 }
1490
1491                 if (S_ISREG(st_buf.st_mode) && st_buf.st_size == 0) {
1492                         EM_DEBUG_LOG("input_html_file is empty size");
1493                         err = EMAIL_ERROR_EMPTY_FILE;
1494                         goto FINISH_OFF;
1495                 }
1496
1497                 if (!(local_preview_text = (char*)em_malloc(sizeof(char) * (st_buf.st_size + 1)))) {
1498                         EM_DEBUG_EXCEPTION("em_malloc failed");
1499                         err = EMAIL_ERROR_OUT_OF_MEMORY;
1500                         goto FINISH_OFF;
1501                 }
1502
1503                 byte_read = fread(local_preview_text, sizeof(char), st_buf.st_size, fp_html);
1504
1505                 if(byte_read <= 0) { /*prevent 26249*/
1506                         EM_SAFE_FREE(local_preview_text);
1507                         if (ferror(fp_html)) {
1508                                 EM_DEBUG_EXCEPTION("fread failed [%s]", input_html_path);
1509                                 err = EMAIL_ERROR_SYSTEM_FAILURE;
1510                                 goto FINISH_OFF;
1511                         }
1512                 }
1513                 else {
1514                         if ( (err = emcore_strip_HTML(local_preview_text)) != EMAIL_ERROR_NONE) {
1515                                 EM_DEBUG_EXCEPTION("emcore_strip failed");
1516                                 goto FINISH_OFF;
1517                         }
1518                 }
1519         }
1520
1521         if ( !local_preview_text && input_plain_path) { /*prevent 26249*/
1522                 /*  get preview text from plain text file */
1523                 if( (err = em_get_encoding_type_from_file_path(input_plain_path, &encoding_type)) != EMAIL_ERROR_NONE) {
1524                         EM_DEBUG_EXCEPTION("em_get_encoding_type_from_file_path failed [%s]", err);
1525                         goto FINISH_OFF;
1526                 }
1527
1528                 memset(&st_buf, 0, sizeof(struct stat));
1529                 if (stat(input_plain_path, &st_buf) < 0)  {
1530                         EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", input_plain_path);
1531                         err = EMAIL_ERROR_INVALID_MAIL;
1532                         goto FINISH_OFF;
1533                 }
1534
1535                 if (!(fp_plain = fopen(input_plain_path, "r")))  {
1536                         EM_DEBUG_EXCEPTION("fopen failed [%s]", input_plain_path);
1537                         err = EMAIL_ERROR_SYSTEM_FAILURE;
1538                         goto FINISH_OFF;
1539                 }
1540
1541                 if (S_ISREG(st_buf.st_mode) && st_buf.st_size == 0) {
1542                         EM_DEBUG_LOG("input_text_file is empty size");
1543                         err = EMAIL_ERROR_EMPTY_FILE;
1544                         goto FINISH_OFF;
1545                 }
1546
1547                 if (!(local_preview_text = (char*)em_malloc(sizeof(char) * local_preview_buffer_length))) {
1548                         EM_DEBUG_EXCEPTION("em_malloc failed");
1549                         goto FINISH_OFF;
1550                 }
1551
1552                 byte_read = fread(local_preview_text, sizeof(char), local_preview_buffer_length - 1, fp_plain);
1553
1554                 if(byte_read <=0) { /*prevent 26249*/
1555                         EM_SAFE_FREE(local_preview_text);
1556                         err = EMAIL_ERROR_NULL_VALUE;
1557                         if (ferror(fp_plain)) {
1558                                 EM_DEBUG_EXCEPTION("fread failed [%s]", input_plain_path);
1559                                 err = EMAIL_ERROR_SYSTEM_FAILURE;
1560                         }
1561                         goto FINISH_OFF;
1562                 }
1563                 reg_replace(local_preview_text, CR_STRING, " ");
1564                 reg_replace(local_preview_text, LF_STRING, " ");
1565                 reg_replace(local_preview_text, TAB_STRING, " ");
1566         }
1567
1568         if(local_preview_text) {
1569                 em_trim_left(local_preview_text);
1570                 if(encoding_type && strcasecmp(encoding_type, "UTF-8") != 0) {
1571                         EM_DEBUG_LOG("encoding_type [%s]", encoding_type);
1572
1573                         if (strcasecmp(encoding_type, "ks_c_5601-1987") == 0 ||
1574                                         strcasecmp(encoding_type, "ksc5601") == 0 ||
1575                                         strcasecmp(encoding_type, "cp949") == 0) {
1576                                 EM_SAFE_FREE(encoding_type);
1577                                 encoding_type = EM_SAFE_STRDUP("EUC-KR");
1578                         }
1579
1580                         utf8_encoded_string = (char*)g_convert (local_preview_text, -1, "UTF-8", encoding_type, &byte_read, &byte_written, &glib_error);
1581
1582                         if(utf8_encoded_string == NULL) {
1583                                 EM_DEBUG_EXCEPTION("g_convert failed : byte_read[%d], EM_SAFE_STRLEN : [%d]", byte_read, EM_SAFE_STRLEN(local_preview_text));
1584                                 EM_DEBUG_LOG("Error is G_CONVERT_ERROR_ILLEGAL_SEQUENCE");
1585
1586                                 if (!g_error_matches (glib_error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE)) {
1587                                         EM_DEBUG_EXCEPTION("g_convert failed");
1588                                         goto FINISH_OFF;
1589                                 }
1590
1591                                 EM_DEBUG_LOG("Extract the preview text, again");
1592
1593                                 utf8_encoded_string = (char *)g_convert(local_preview_text, byte_read, "UTF-8", encoding_type, &byte_read, &byte_written, &glib_error);
1594                                 if (utf8_encoded_string == NULL) {
1595                                         EM_DEBUG_EXCEPTION("g_convert failed : byte_read[%d]", byte_read);
1596                                         goto FINISH_OFF;
1597                                 }
1598
1599                         }
1600                         EM_SAFE_FREE(local_preview_text);
1601                         local_preview_text = utf8_encoded_string;
1602                 }
1603         }
1604
1605 FINISH_OFF:
1606
1607         if (local_preview_text != NULL)
1608                 *output_preview_buffer = EM_SAFE_STRDUP(local_preview_text);
1609
1610         EM_SAFE_FREE(local_preview_text);
1611         EM_SAFE_FREE(encoding_type);
1612
1613         if (fp_html != NULL)
1614                 fclose(fp_html);
1615
1616         if (fp_plain != NULL)
1617                 fclose(fp_plain);
1618
1619         EM_DEBUG_FUNC_END("err [%d]", err);
1620         return err;
1621 }
1622
1623 INTERNAL_FUNC int emcore_add_transaction_info(int mail_id, int handle , int *err_code)
1624 {
1625         EM_DEBUG_FUNC_BEGIN("mail_id[%d], handle[%d]", mail_id, handle);
1626
1627         int ret = false;
1628         int err = EMAIL_ERROR_NONE ;
1629         em_transaction_info_type_t  *pTransinfo = NULL ;
1630         em_transaction_info_type_t      *pTemp = NULL;
1631
1632         EM_DEBUG_LOG("g_transaction_info_list[%p]", g_transaction_info_list);
1633         pTransinfo = g_transaction_info_list ;
1634
1635         if (!(pTemp = em_malloc(sizeof(em_transaction_info_type_t))))  {
1636                 EM_DEBUG_EXCEPTION("malloc failed...");
1637                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1638                 goto FINISH_OFF;
1639         }
1640         pTemp->mail_id = mail_id ;
1641         pTemp->handle = handle;
1642
1643         if (!pTransinfo) {
1644                 pTransinfo = pTemp ;
1645                 g_transaction_info_list = pTransinfo ;
1646         }
1647         else {
1648                 while (pTransinfo->next)
1649                         pTransinfo = pTransinfo->next;
1650                 pTransinfo->next = pTemp;
1651         }
1652         ret = true ;
1653
1654 FINISH_OFF:
1655
1656         if (err_code)
1657                 *err_code = err;
1658         EM_DEBUG_FUNC_END("g_transaction_info_list[%p]", g_transaction_info_list);
1659         return ret;
1660 }
1661
1662 INTERNAL_FUNC int emcore_get_handle_by_mailId_from_transaction_info(int mail_id, int *pHandle)
1663 {
1664         EM_DEBUG_FUNC_BEGIN("mail_id[%d], handle[%p]", mail_id, pHandle);
1665
1666         int ret = false;
1667         em_transaction_info_type_t  *pTransinfo = NULL ;
1668
1669         if (g_transaction_info_list == NULL) {
1670                 EM_DEBUG_EXCEPTION("g_transaction_info_list NULL");
1671                 return false;
1672         }
1673         pTransinfo = g_transaction_info_list;
1674
1675         do {
1676                 EM_DEBUG_LOG("pTransinfo->mail_id[%d]", pTransinfo->mail_id);
1677                 if (pTransinfo->mail_id == mail_id) {
1678                         *pHandle = pTransinfo->handle;
1679                         ret = true;
1680                         EM_DEBUG_LOG("*pHandle[%d]", *pHandle);
1681                         break;
1682                 }
1683                 else
1684                         pTransinfo = pTransinfo->next ;
1685         }while (pTransinfo);
1686         EM_DEBUG_FUNC_END();
1687         return ret;
1688 }
1689
1690 INTERNAL_FUNC int emcore_delete_transaction_info_by_mailId(int mail_id )
1691 {
1692         EM_DEBUG_FUNC_BEGIN("mail_id[%d]", mail_id);
1693
1694         em_transaction_info_type_t  *pTransinfo ;
1695         em_transaction_info_type_t *pTemp = NULL;
1696
1697         if (g_transaction_info_list == NULL) {
1698                 EM_DEBUG_EXCEPTION("g_transaction_info_list NULL");
1699                 return false;
1700         }
1701         pTransinfo = g_transaction_info_list;
1702
1703         EM_DEBUG_LOG("pTransinfo[%p]", pTransinfo);
1704
1705         do {
1706                 EM_DEBUG_LOG("pTransinfo->mail_id[%d]", pTransinfo->mail_id);
1707                 if (pTransinfo->mail_id == mail_id) {
1708                         pTemp = pTransinfo->next ;
1709                         if (!pTemp) {
1710                                 EM_SAFE_FREE(pTransinfo) ;
1711                                 g_transaction_info_list = NULL;
1712                         }
1713                         else {
1714                                 pTransinfo->mail_id = pTransinfo->next->mail_id;
1715                                 pTransinfo->handle = pTransinfo->next->handle ;
1716                                 pTransinfo->next = pTransinfo->next->next;
1717
1718                                 EM_SAFE_FREE(pTemp);
1719                         }
1720                         break;
1721                 }
1722                 else {
1723                         pTransinfo = pTransinfo->next ;
1724                 }
1725
1726         }while (pTransinfo);
1727         EM_DEBUG_FUNC_END();
1728         return true;
1729 }
1730
1731
1732 #include <regex.h>
1733
1734 int reg_replace (char *input_source_text, char *input_old_pattern_string, char *input_new_string)
1735 {
1736         int         error_code = EMAIL_ERROR_NONE;
1737         char       *pos = NULL;
1738         int         so, n, nmatch, source_text_length, n_count = 1;
1739         regmatch_t *pmatch = NULL;
1740         regex_t     reg_pattern;
1741
1742         if(!input_source_text || !input_old_pattern_string || !input_new_string) {
1743                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1744                 error_code = EMAIL_ERROR_INVALID_PARAM;
1745                 goto FINISH_OFF;
1746         }
1747
1748         source_text_length = EM_SAFE_STRLEN(input_source_text);
1749
1750         if (regcomp(&reg_pattern, input_old_pattern_string, REG_ICASE) != 0) {
1751                 EM_DEBUG_EXCEPTION("regcomp failed");
1752                 goto FINISH_OFF;
1753         }
1754
1755         nmatch = reg_pattern.re_nsub + 1;
1756
1757         EM_DEBUG_LOG("nmatch [%d]", nmatch);
1758
1759         if(nmatch < 1) {
1760                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_DATA");
1761                 error_code = EMAIL_ERROR_INVALID_DATA;
1762                 goto FINISH_OFF;
1763         }
1764
1765         pmatch = (regmatch_t*)em_malloc(sizeof(regmatch_t) * nmatch);
1766
1767         if(pmatch == NULL) {
1768                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_OUT_OF_MEMORY");
1769                 error_code = EMAIL_ERROR_OUT_OF_MEMORY;
1770                 goto FINISH_OFF;
1771         }
1772
1773         for (pos = input_new_string; *pos ; pos++) {
1774                 if (*pos == '\\' && *(pos + 1) > '0' && *(pos + 1) <= '9') {
1775
1776                         so = pmatch[*(pos + 1) - 48].rm_so;
1777                         n  = pmatch[*(pos + 1) - 48].rm_eo - so;
1778
1779                         EM_DEBUG_LOG("so [%d], n [%d]", so, n);
1780
1781                         if (so < 0 || EM_SAFE_STRLEN (input_new_string) + n - 1 > source_text_length)
1782                                 break;
1783
1784                         memmove (pos + n, pos + 2, EM_SAFE_STRLEN (pos) - 1);
1785                         memmove (pos, input_source_text + so, n);
1786                         pos = pos + n - 2;
1787                 }
1788         }
1789
1790         for (pos = input_source_text; !regexec (&reg_pattern, pos, 1, pmatch, 0);) {
1791                 n = pmatch[0].rm_eo - pmatch[0].rm_so;
1792                 pos += pmatch[0].rm_so;
1793
1794                 memmove (pos + EM_SAFE_STRLEN (input_new_string), pos + n, EM_SAFE_STRLEN (pos) - n + 1);
1795                 memmove (pos, input_new_string, EM_SAFE_STRLEN (input_new_string));
1796                 pos += EM_SAFE_STRLEN (input_new_string);
1797                 n_count++;
1798         }
1799
1800 FINISH_OFF:
1801
1802         EM_SAFE_FREE(pmatch);
1803         regfree (&reg_pattern);
1804
1805         EM_DEBUG_FUNC_END("error_code [%d]", error_code);
1806         return error_code;
1807 }
1808
1809
1810 int emcore_strip_HTML(char *source_string)
1811 {
1812         EM_DEBUG_FUNC_BEGIN("source_string [%p]", source_string);
1813
1814         int result = EMAIL_ERROR_NONE;
1815
1816         /* strip out CR */
1817         reg_replace(source_string, "\r", "");
1818
1819         /* strip out LF */
1820         reg_replace(source_string, "\n", "");
1821
1822         /* strip out HEAD */
1823         reg_replace(source_string, "<head[^>]*>", "<head>");
1824         reg_replace(source_string, "<*/head>", "</head>");
1825         reg_replace(source_string, "<head>.*</head>", "");
1826
1827         /* strip out STYLE */
1828         reg_replace(source_string, "<style[^>]*>", "<style>");
1829         reg_replace(source_string, "<*/style>", "</style>");
1830         reg_replace(source_string, "<style>.*</style>", "");
1831
1832         /* strip out SCRIPT */
1833         reg_replace(source_string, "<script[^>]*>", "<script>");
1834         reg_replace(source_string, "<*/script>", "</script>");
1835         reg_replace(source_string, "<script>.*</script>", "");
1836
1837         /* strip out ALL TAG & comment */
1838         reg_replace(source_string, "<[^>]*>", "");
1839
1840         /* for remaining comment in some cases */
1841         reg_replace(source_string, "-->", "");
1842
1843         /* strip out html entities */
1844         /*"&(quot|#34);""&(amp|#38);""&(lt|#60);""&(gt|#62);""&(nbsp|#160);"
1845           "&(iexcl|#161);""&(cent|#162);""&(pound|#163);""&(copy|#169);"*/
1846         reg_replace(source_string, "&lt;", "<");
1847         reg_replace(source_string, "&gt;", ">");
1848         reg_replace(source_string, "&quot;", "\'");
1849         reg_replace(source_string, "&nbsp;", " ");
1850
1851         reg_replace(source_string, "  +", " ");
1852
1853         EM_DEBUG_FUNC_END();
1854         return result;
1855 }
1856
1857 INTERNAL_FUNC int emcore_send_noti_for_new_mail(int account_id, char *mailbox_name, char *subject, char *from, char *uid, char *datetime)
1858 {
1859         EM_DEBUG_FUNC_BEGIN("mailbox_name(%s) subject(%s), from(%s), uid(%s), datetime(%s)", mailbox_name, subject, from, uid, datetime);
1860         int error_code = EMAIL_ERROR_NONE;
1861         int param_length = 0;
1862         char *param_string = NULL;
1863
1864         if (mailbox_name == NULL || subject == NULL || from == NULL || uid == NULL || datetime == NULL) {
1865                 error_code = EMAIL_ERROR_INVALID_PARAM;
1866                 EM_DEBUG_EXCEPTION("Invalid parameter, mailbox_name(%p), subject(%p), from(%p), uid(%p), datetime(%p)", mailbox_name, subject, from, uid, datetime);
1867                 goto FINISH_OFF;
1868         }
1869
1870         param_length = strlen(mailbox_name) + strlen(subject) + strlen(from) + strlen(uid) + strlen(datetime) + 5; /*prevent 34358*/
1871
1872         param_string = em_malloc(param_length);
1873         if (param_string == NULL) {
1874                 error_code = EMAIL_ERROR_OUT_OF_MEMORY;
1875                 EM_DEBUG_EXCEPTION("Memory allocation for 'param_string' is failed");
1876                 goto FINISH_OFF;
1877         }
1878
1879         memset(param_string, 0x00, param_length);
1880
1881         SNPRINTF(param_string, param_length, "%s%c%s%c%s%c%s%c%s", mailbox_name, 0x01, subject, 0x01, from, 0x01, uid, 0x01, datetime);
1882
1883         if (emcore_notify_network_event(NOTI_DOWNLOAD_NEW_MAIL, account_id, param_string, 0, 0) == 0) { /*  failed */
1884                 error_code = EMAIL_ERROR_UNKNOWN;
1885                 EM_DEBUG_EXCEPTION("emcore_notify_network_event is failed");
1886                 goto FINISH_OFF;
1887         }
1888
1889 FINISH_OFF:
1890
1891         EM_SAFE_FREE(param_string);
1892         EM_DEBUG_FUNC_END();
1893         return error_code;
1894 }
1895
1896 #define MAX_TITLE_LENGTH 1024
1897
1898 #ifdef __FEATURE_DRIVING_MODE__
1899 int convert_app_err_to_email_err(int app_error)
1900 {
1901         int err = EMAIL_ERROR_NONE;
1902
1903         switch(app_error) {
1904         case SERVICE_ERROR_NONE :
1905                 err = EMAIL_ERROR_NONE;
1906                 break;
1907         case SERVICE_ERROR_INVALID_PARAMETER :
1908                 err = EMAIL_ERROR_INVALID_PARAM;
1909                 break;
1910         case SERVICE_ERROR_OUT_OF_MEMORY :
1911                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1912                 break;
1913         default:
1914                 err = EMAIL_ERROR_UNKNOWN;
1915                 break;
1916         }
1917
1918         return err;
1919 }
1920
1921 int emcore_start_driving_mode(int account_id) {
1922         EM_DEBUG_FUNC_BEGIN();
1923         
1924         int err = SERVICE_ERROR_NONE;
1925         int tts_enable = 0;     
1926         char string_account[10] = { 0 };
1927         service_h service = NULL;
1928
1929         if (vconf_get_bool(VCONFKEY_SETAPPL_DRIVINGMODE_DRIVINGMODE, &tts_enable) != 0) {
1930                 EM_DEBUG_EXCEPTION("vconf_get_int failed");
1931                 goto FINISH_OFF;
1932         }
1933
1934         EM_DEBUG_LOG("Driving Mode : [%d]", tts_enable);
1935
1936         if (!tts_enable)
1937                 goto FINISH_OFF;
1938
1939
1940         if (vconf_get_bool(VCONFKEY_SETAPPL_DRIVINGMODE_NEWEMAILS, &tts_enable) != 0) {
1941                 EM_DEBUG_EXCEPTION("vconf_get_int failed");
1942                 goto FINISH_OFF;
1943         }
1944
1945         EM_DEBUG_LOG("New emails of driving Mode : [%d]", tts_enable);
1946
1947         if (!tts_enable)
1948                 goto FINISH_OFF;
1949
1950         SNPRINTF(string_account, sizeof(string_account), "%d", account_id);
1951
1952         err = service_create(&service);
1953         if (err != SERVICE_ERROR_NONE) {
1954                 EM_DEBUG_EXCEPTION("service_create failed : [%d]", err);
1955                 goto FINISH_OFF;
1956         }
1957
1958         err = service_set_package(service, "com.samsung.email-tts-play");
1959         if (err != SERVICE_ERROR_NONE) {
1960                 EM_DEBUG_EXCEPTION("service_set_package failed : [%d]", err);
1961                 goto FINISH_OFF;
1962         }
1963
1964         err = service_add_extra_data(service, "tts_email_account_id", string_account);
1965         if (err != SERVICE_ERROR_NONE) {
1966                 EM_DEBUG_EXCEPTION("service_add_extra_data failed : [%d]", err);
1967                 goto FINISH_OFF;
1968         }
1969
1970         err = service_send_launch_request(service, NULL, NULL);
1971         if (err != SERVICE_ERROR_NONE) {
1972                 EM_DEBUG_EXCEPTION("service_send_launch_request failed : [%d]", err);
1973                 goto FINISH_OFF;
1974         }
1975
1976 FINISH_OFF:
1977
1978         if (service)
1979                 service_destroy(service);
1980
1981         EM_DEBUG_FUNC_END();
1982         return convert_app_err_to_email_err(err);
1983 }
1984 #endif /* __FEATURE_DRIVING_MODE__ */
1985
1986 int emcore_update_notification_for_unread_mail(int account_id)
1987 {
1988         EM_DEBUG_FUNC_BEGIN("acccount_id[%d]", account_id);
1989         int ret = false;
1990         int i, account_count = 0;
1991         int err = EMAIL_ERROR_NONE;
1992         emstorage_account_tbl_t *p_account_tbl = NULL;
1993
1994         if (!emstorage_get_account_list(&account_count, &p_account_tbl, true, false, &err)) {
1995                 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [%d]", err);
1996                 goto FINISH_OFF;
1997         }
1998
1999         for (i = 0; i < account_count ; i++) {
2000                 if ((err = emcore_add_notification(p_account_tbl[i].account_id, 0, EMAIL_ACTION_NUM)) != EMAIL_ERROR_NONE) {
2001                         EM_DEBUG_EXCEPTION("emcore_add_notification failed : [%d]", err);
2002                         continue;
2003                 }
2004         
2005                 if ((err = emcore_start_driving_mode(p_account_tbl[i].account_id)) != EMAIL_ERROR_NONE) {
2006                         EM_DEBUG_EXCEPTION("emcore_start_driving_mode failed : [%d]", err);
2007                         continue;
2008                 }
2009         }
2010
2011         ret = true;
2012
2013 FINISH_OFF:
2014
2015         if (p_account_tbl)
2016                 emstorage_free_account(&p_account_tbl, account_count, NULL);
2017
2018         EM_DEBUG_FUNC_END();
2019         return ret;
2020 }
2021
2022 INTERNAL_FUNC int emcore_finalize_sync(int account_id, int *error)
2023 {
2024         EM_DEBUG_FUNC_BEGIN("account_id [%d], error [%p]", account_id, error);
2025         int err = EMAIL_ERROR_NONE, ret = true, result_sync_status = SYNC_STATUS_FINISHED;
2026
2027         if ((err = emcore_update_sync_status_of_account(account_id, SET_TYPE_MINUS, SYNC_STATUS_SYNCING)) != EMAIL_ERROR_NONE)
2028                 EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
2029
2030         if (!emstorage_get_sync_status_of_account(ALL_ACCOUNT, &result_sync_status, &err))
2031                 EM_DEBUG_EXCEPTION("emstorage_get_sync_status_of_account failed [%d]", err);
2032
2033         if (result_sync_status == SYNC_STATUS_HAVE_NEW_MAILS) {
2034                 if (!emcore_update_notification_for_unread_mail(ALL_ACCOUNT))
2035                         EM_DEBUG_EXCEPTION("emcore_update_notification_for_unread_mail failed");
2036
2037                 if (!emcore_display_unread_in_badge())
2038                         EM_DEBUG_EXCEPTION("emcore_display_unread_in_badge failed");
2039
2040                 emcore_start_alert();
2041
2042 #ifdef __FEATURE_BLOCKING_MODE__
2043                 emcore_set_blocking_mode_status(false);
2044 #endif /* __FEATURE_BLOCKING_MODE__ */
2045
2046                 if ((err = emcore_update_sync_status_of_account(account_id, SET_TYPE_MINUS, SYNC_STATUS_HAVE_NEW_MAILS)) != EMAIL_ERROR_NONE)
2047                         EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
2048         }
2049
2050         EM_DEBUG_FUNC_END();
2051         return ret;
2052 }
2053
2054 INTERNAL_FUNC int emcore_clear_all_notifications()
2055 {
2056         int account_count = 0, i;
2057         emstorage_account_tbl_t *account_list;
2058         int error_code = EMAIL_ERROR_NONE;
2059
2060         if(!emstorage_get_account_list(&account_count, &account_list, true, false, &error_code)) {
2061                 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed");
2062                 goto FINISH_OFF;
2063         }
2064
2065         for(i = 0; i < account_count; i++) {
2066                 emcore_delete_notification_by_account(account_list[i].account_id);
2067         }
2068
2069 FINISH_OFF:
2070         if(account_count) {
2071                 emstorage_free_account(&account_list, account_count, NULL);
2072         }
2073
2074         EM_DEBUG_FUNC_END("return[%d]", error_code);
2075         return error_code;
2076 }
2077
2078 #define EAS_EXECUTABLE_PATH "/usr/bin/eas-engine"
2079
2080 INTERNAL_FUNC int emcore_delete_notification_by_account(int account_id)
2081 {
2082         EM_DEBUG_FUNC_BEGIN("account_id [%d]", account_id);
2083         int error_code = EMAIL_ERROR_NONE;
2084         int private_id = 0;
2085         char vconf_private_id[MAX_PATH] = {0, };
2086
2087         SNPRINTF(vconf_private_id, sizeof(vconf_private_id), "%s/%d", VCONF_KEY_NOTI_PRIVATE_ID, account_id);
2088         if (vconf_get_int(vconf_private_id, &private_id) != 0) {
2089                 EM_DEBUG_EXCEPTION("vconf_get_int failed");
2090         }
2091         EM_DEBUG_FUNC_END();
2092         return error_code;
2093 }
2094
2095 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
2096
2097 /**
2098  * @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)
2099  * Prepare a linked list of uid ranges with each node having a uid_range and lowest and highest uid in it.
2100  *
2101  *@author                                       h.gahlaut@samsung.com
2102  * @param[in] id_set                    Specifies the array of mail_id and corresponding server_mail_id sorted by server_mail_ids in ascending order
2103  * @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
2104  * @param[in] range_len         Specifies the maximum length of string of range allowed.
2105  * @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
2106  * @param[out] err_code         Returns the error code.
2107  * @remarks                                     An example of a uid_range formed is 2:6,8,10,14:15,89,
2108  *                                                      While using it the caller should remove the ending, (comma)
2109  * @return This function returns true on success or false on failure.
2110  */
2111
2112 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)
2113 {
2114         EM_DEBUG_FUNC_BEGIN();
2115
2116         int ret = false;
2117         int error = EMAIL_ERROR_NONE;
2118
2119         if (NULL == id_set || id_set_count  <= 0 || NULL == uid_range_set) {
2120                 EM_DEBUG_EXCEPTION(" Invalid Parameter id_set[%p] id_set_count[%d] uid_range_set[%p]", id_set, id_set_count, uid_range_set);
2121                 error = EMAIL_ERROR_INVALID_PARAM;
2122                 goto FINISH_OFF;
2123         }
2124
2125         int i = 0;
2126         unsigned long current_uid = 0;
2127         unsigned long first_uid = 0;
2128         unsigned long last_uid = 0;
2129         const int max_subset_string_size = MAX_SUBSET_STRING_SIZE;
2130         char subset_string[MAX_SUBSET_STRING_SIZE] = {0,};
2131         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*/
2132
2133         if (range_len < (max_subset_string_size + 1))           /* 1 for ending NULL character */ {
2134                 EM_DEBUG_EXCEPTION(" Invalid Parameter range_len[%d]", range_len);
2135                 error = EMAIL_ERROR_INVALID_PARAM;
2136                 goto FINISH_OFF;
2137         }
2138
2139         EM_DEBUG_LOG("id set count[%d] range_len[%d]", id_set_count, range_len);
2140
2141         do {
2142                 first_uid = last_uid = current_uid = id_set[i].server_mail_id;
2143                 /* Start subset string by putting first server mail id in it from id_set*/
2144                 memset(subset_string, 0x00, max_subset_string_size);
2145                 SNPRINTF(subset_string, max_subset_string_size, "%lu", first_uid);
2146                 ++i;
2147
2148                 /* Check if only one server mail id was left in id_set */
2149                 if (i >= id_set_count) {
2150                         /* No more server mail id left in id_set */
2151                         if (false == emcore_append_subset_string_to_uid_range(subset_string, &current_node, uid_range_set, range_len, first_uid, last_uid)) {
2152                                 EM_DEBUG_EXCEPTION("emcore_append_subset_string_to_uid_range failed");
2153                                 goto FINISH_OFF;
2154                         }
2155                         break;
2156                 }
2157                 else {
2158                         /* 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 */
2159                         do {
2160                                 current_uid = id_set[i].server_mail_id;
2161                                 if (current_uid == (last_uid + 1)) {
2162                                         last_uid = current_uid;
2163                                         ++i;
2164                                 }
2165                                 else {
2166                                         memset(subset_string, 0x00, max_subset_string_size);
2167                                         if (first_uid != last_uid)      /* Form subset string by first_uid:last_uid */
2168                                                 SNPRINTF(subset_string, max_subset_string_size, "%lu:%lu", first_uid, last_uid);
2169                                         else    /* Form subset string by first_uid */
2170                                                 SNPRINTF(subset_string, max_subset_string_size, "%lu", first_uid);
2171
2172                                         if (false == emcore_append_subset_string_to_uid_range(subset_string, &current_node, uid_range_set, range_len, first_uid, last_uid)) {
2173                                                 EM_DEBUG_EXCEPTION("emcore_append_subset_string_to_uid_range failed");
2174                                                 goto FINISH_OFF;
2175                                         }
2176                                         /* To Start formation of new subset string break out of inner loop */
2177                                         break;
2178                                 }
2179
2180                         } while (i < id_set_count);
2181
2182                         /* Flow comes here in two cases :
2183                         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
2184                         2. due to break statement */
2185
2186                         if (last_uid == current_uid) {
2187                                 /* Case 1 */
2188
2189                                 memset(subset_string, 0x00, max_subset_string_size);
2190                                 SNPRINTF(subset_string, max_subset_string_size, "%lu:%lu", first_uid, last_uid);
2191
2192                                 if (false == emcore_append_subset_string_to_uid_range(subset_string, &current_node, uid_range_set, range_len, first_uid, last_uid)) {
2193                                         EM_DEBUG_EXCEPTION("emcore_append_subset_string_to_uid_range failed");
2194                                         goto FINISH_OFF;
2195                                 }
2196                         }
2197                         else {
2198                                 /* Case 2: Do Nothing */
2199                         }
2200
2201                 }
2202         } while (i < id_set_count);
2203
2204         ret = true;
2205
2206 FINISH_OFF:
2207         if (NULL != err_code)
2208                 *err_code = error;
2209         EM_DEBUG_FUNC_END();
2210         return ret;
2211
2212 }
2213
2214 /**
2215  * @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)
2216  * 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
2217  * and stores the subset_string in its uid_range. Also sets the lowest and highest uids for the corresponsing uid_range
2218  *
2219  * @author                                      h.gahlaut@samsung.com
2220  * @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.
2221  * @param[in] range_len         Specifies the maximum length of range string allowed.
2222  * @param[in] luid                      Specifies the lowest uid in subset string
2223  * @param[in] huid                      Specifies the highest uid in subset string
2224  * @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
2225  * @param[out] err_code         Returns the error code.
2226  * @remarks
2227  * @return This function returns true on success or false on failure.
2228  */
2229
2230 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)
2231 {
2232         EM_DEBUG_FUNC_BEGIN();
2233         email_uid_range_set *current_node = NULL;
2234
2235         if (NULL == (*uid_range_set)) {
2236                 /*This happens only once when list  creation starts. Head Node is allocated */
2237                 current_node = (email_uid_range_set *)em_malloc(sizeof(email_uid_range_set));
2238                 if (NULL == current_node) {
2239                         EM_DEBUG_EXCEPTION("em_malloc failed");
2240                         return false;
2241                 }
2242
2243                 current_node->uid_range = (char *)em_malloc(range_len);
2244
2245                 if (NULL == current_node->uid_range) {
2246                         EM_DEBUG_EXCEPTION("em_malloc failed");
2247                         EM_SAFE_FREE(current_node);
2248                         return false;
2249                 }
2250
2251                 SNPRINTF(current_node->uid_range, range_len, "%s,", subset_string);
2252
2253                 current_node->lowest_uid = luid;
2254                 current_node->highest_uid = huid;
2255                 (*uid_range_set) = current_node;
2256
2257                 (*current_node_adr) = current_node;
2258
2259         }
2260         else {
2261                 /* Apart from first call to this function flow will always come here */
2262                 current_node = (*current_node_adr);
2263                 int len_sub_string = EM_SAFE_STRLEN(subset_string);
2264                 int space_left_in_buffer = range_len - EM_SAFE_STRLEN(current_node->uid_range);
2265
2266                 if ((len_sub_string + 1 + 1) <= space_left_in_buffer)   /* 1 for comma + 1 for ending null character */ {
2267                         SNPRINTF(current_node->uid_range + EM_SAFE_STRLEN(current_node->uid_range), space_left_in_buffer, "%s,", subset_string);
2268                         current_node->highest_uid = huid;
2269                 }
2270                 else {
2271                         /* No more space left in uid_range string.If continued on it, it will exceeded max size of range_len */
2272                         /* Allocate new node in Uid Range set */
2273                         email_uid_range_set *new_node = NULL;
2274
2275                         new_node = (email_uid_range_set *)em_malloc(sizeof(email_uid_range_set));
2276
2277                         if (NULL == new_node) {
2278                                 EM_DEBUG_EXCEPTION("em_malloc failed");
2279                                 return false;
2280                         }
2281
2282                         /* Allocate uid_range of new node */
2283
2284                         new_node->uid_range =  (char *)em_malloc(range_len);
2285
2286                         if (NULL == new_node->uid_range) {
2287                                 EM_DEBUG_EXCEPTION("em_malloc failed");
2288                                 EM_SAFE_FREE(new_node);
2289                                 return false;
2290                         }
2291
2292                         SNPRINTF(new_node->uid_range, range_len, "%s, ", subset_string);
2293
2294                         new_node->lowest_uid = luid;
2295                         new_node->highest_uid = huid;
2296
2297                         current_node->next = new_node;
2298
2299                         (*current_node_adr) = new_node;
2300                 }
2301         }
2302         EM_DEBUG_FUNC_END();
2303         return true;
2304 }
2305
2306 /**
2307  * void emcore_free_uid_range_set(email_uid_range_set **uid_range_head)
2308  * Frees the linked list of uid ranges
2309  *
2310  * @author                                      h.gahlaut@samsung.com
2311  * @param[in] uid_range_head    Head pointer of linked list of uid ranges
2312  * @remarks
2313  * @return This function does not return anything.
2314  */
2315
2316 INTERNAL_FUNC
2317 void emcore_free_uid_range_set(email_uid_range_set **uid_range_set)
2318 {
2319         EM_DEBUG_FUNC_BEGIN();
2320
2321         email_uid_range_set *current_node = NULL;
2322         email_uid_range_set *uid_range_head = NULL;
2323
2324         current_node = uid_range_head = (*uid_range_set);       /* Make the current node and head ptr point to starting of  uid_range_set */
2325
2326         while (current_node) {
2327                 uid_range_head = current_node->next;            /* Move the head ptr to next node*/
2328
2329                 EM_SAFE_FREE(current_node->uid_range);
2330                 EM_SAFE_FREE(current_node);                             /* Free the current node */
2331
2332                 current_node = uid_range_head;                  /* Make the current node point to head ptr */
2333         }
2334
2335         (*uid_range_set) = NULL;
2336         EM_DEBUG_FUNC_END();
2337 }
2338
2339
2340 /**
2341  * @fn emcore_form_comma_separated_strings(int numbers[], int num_count, int max_string_len, char *** strings, int *string_count, int *err_code)
2342  * Forms comma separated strings of a give max_string_len from an array of numbers
2343  *
2344  * @author                                      h.gahlaut@samsung.com
2345  * @param[in] numbers                   Specifies the array of numbers to be converted into comma separated strings.
2346  * @param[in] num_count         Specifies the count of numbers in numbers array.
2347  * @param[in] max_string_len    Specifies the maximum length of comma separated strings that are to be formed.
2348  * @param[out] strings                  Returns the base address of a double dimension array which stores the strings.
2349  * @param[out] string_count             Returns the number of strings formed.
2350  * @param[out] err_code         Returns the error code.
2351  * @remarks                                     If Input to the function is five numbers like 2755 2754 2748 2749 2750 and a given max_string_len is 20.
2352  *                                                      Then this function will form two comma separated strings as follows -
2353  *                                                      "2755, 2754, 2748"
2354  *                                                      "2749, 2750"
2355  * @return This function returns true on success or false on failure.
2356  */
2357
2358 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)
2359 {
2360         EM_DEBUG_FUNC_BEGIN();
2361
2362         int error = EMAIL_ERROR_NONE;
2363         int ret = false;
2364
2365         char **string_list = NULL;
2366         int num_of_strings = 0;
2367         int i = 0;
2368         int j =0;
2369         char num[MAX_INTEGER_LENGTH + 1] = {0, };
2370         int num_len = 0;
2371         int space_in_buffer = 0;
2372         int len_of_string_formed = 0;
2373
2374         if (NULL == numbers || num_count <= 0 || \
2375                 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.  */ {
2376                 EM_DEBUG_EXCEPTION("Invalid Parameter numbers[%p] num_count [%d] max_string_len [%d] strings [%p] string_count[%p]", \
2377                         numbers, num_count, max_string_len, strings, string_count);
2378                 error = EMAIL_ERROR_INVALID_PARAM;
2379                 goto FINISH_OFF;
2380         }
2381
2382         EM_DEBUG_LOG("num_count [%d] max_string_len [%d]", num_count, max_string_len);
2383
2384         string_list = em_malloc(sizeof(char *));
2385
2386         if (NULL == string_list) {
2387                 EM_DEBUG_EXCEPTION("em_malloc failed ");
2388                 goto FINISH_OFF;
2389         }
2390
2391         string_list[num_of_strings] = em_malloc(max_string_len);
2392
2393         if (NULL == string_list[num_of_strings]) {
2394                 EM_DEBUG_EXCEPTION("em_malloc failed ");
2395                 goto FINISH_OFF;
2396         }
2397
2398         ++num_of_strings;
2399         space_in_buffer = max_string_len;
2400
2401         for (j = 0; j < num_count;++j) {
2402                 memset(num, 0x00, MAX_INTEGER_LENGTH + 1);
2403                 SNPRINTF(num, MAX_INTEGER_LENGTH + 1, "%d", numbers[j]);
2404
2405                 num_len = EM_SAFE_STRLEN(num);
2406
2407                 len_of_string_formed = EM_SAFE_STRLEN(string_list[num_of_strings - 1]);
2408
2409                 space_in_buffer = max_string_len - len_of_string_formed ;
2410
2411                 if (space_in_buffer >= (num_len+1+1))                   /*  1 for comma and 1 for ending NULL */ {
2412                         SNPRINTF(string_list[num_of_strings - 1] + len_of_string_formed, max_string_len, "%d,", numbers[j]);
2413                 }
2414                 else {  /*  Removing comma at end of string  */
2415                         string_list[num_of_strings - 1][len_of_string_formed-1] = '\0';
2416                         char **temp = NULL;
2417                         temp = (char **)realloc(string_list, sizeof(char *) * (num_of_strings + 1));    /*  Allocate new buffer to store a pointer to a new string */
2418
2419                         if (NULL == temp) {
2420                                 EM_DEBUG_EXCEPTION("realloc failed");
2421                                 goto FINISH_OFF;
2422                         }
2423
2424                         memset(temp + num_of_strings, 0X00, sizeof(char *));
2425
2426                         string_list = temp;
2427                         temp = NULL;
2428                         string_list[num_of_strings] = em_malloc(max_string_len);/*  Allocate new buffer to store the string */
2429
2430                         if (NULL == string_list[num_of_strings]) {
2431                                 EM_DEBUG_EXCEPTION(" em_malloc failed ");
2432                                 goto FINISH_OFF;
2433                         }
2434                         ++num_of_strings;
2435                         SNPRINTF(string_list[num_of_strings - 1] , max_string_len, "%d,", numbers[j]);/*  Start making new string */
2436                 }
2437         }
2438
2439         /*  Removing comma at end of string  */
2440         len_of_string_formed = EM_SAFE_STRLEN(string_list[num_of_strings - 1]);
2441         string_list[num_of_strings - 1][len_of_string_formed-1] = '\0';
2442         ret = true;
2443
2444 FINISH_OFF:
2445
2446         if (false == ret)
2447                 emcore_free_comma_separated_strings(&string_list, &num_of_strings);
2448
2449         if (true == ret) {
2450                 for (i = 0; i < num_of_strings;++i)
2451                         EM_DEBUG_LOG("%s", string_list[i]);
2452                 *strings = string_list;
2453                 *string_count = num_of_strings;
2454         }
2455
2456
2457         if (NULL != err_code)
2458                 *err_code = error;
2459
2460         EM_DEBUG_FUNC_END("ret [%d]", ret);
2461         return ret;
2462 }
2463 /**
2464  * @fn emcore_free_comma_separated_strings(char *** string_list, int *string_count)
2465  * Frees the double dimensional array of strings.
2466  *
2467  * @author                                      h.gahlaut@samsung.com
2468  * @param[in] uid_range_head    Address of base address of double dimensional array of strings.
2469  * @param[in] string_count              Address of variable holding the count of strings.
2470  * @remarks
2471  * @return This function does not return anything.
2472  */
2473 INTERNAL_FUNC void emcore_free_comma_separated_strings(char *** string_list, int *string_count)
2474 {
2475         EM_DEBUG_FUNC_BEGIN();
2476         int i = 0;
2477         char **str_list = NULL;
2478         int count = 0;
2479
2480         if (NULL != string_list) {
2481                 str_list = *string_list;
2482
2483                 if (0 != *string_count) {
2484                         count = *string_count;
2485                         for (i = 0; i < count; ++i)
2486                                 EM_SAFE_FREE(str_list[i]);
2487                 }
2488
2489                 EM_SAFE_FREE(str_list);
2490                 *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 */
2491                 *string_count = 0;
2492         }
2493         EM_DEBUG_FUNC_END();
2494 }
2495
2496
2497 #endif
2498
2499
2500
2501
2502 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)
2503 {
2504         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);
2505         int ret = false, err = EMAIL_ERROR_NONE;
2506         char *extcheck = NULL;
2507         char attachment_file_name[MAX_PATH + 1] = { 0, };
2508
2509         if (!source_file_name || !result_file_name) {
2510                 EM_DEBUG_EXCEPTION("Invalid Parameter");
2511                 err  = EMAIL_ERROR_INVALID_PARAM;
2512                 goto FINISH_OFF;
2513         }
2514
2515         strncpy(attachment_file_name, source_file_name, MAX_PATH);
2516         extcheck = strchr(attachment_file_name, '.');
2517
2518         if (extcheck)
2519                 EM_DEBUG_LOG("Extension Exist in the Attachment [%s] ", extcheck);
2520         else  { /* No extension attached, So add the Extension based on the subtype */
2521                 if (sub_type) {
2522                         strcat(attachment_file_name, ".");
2523                         strcat(attachment_file_name, sub_type);
2524                         EM_DEBUG_LOG("attachment_file_name with extension[%s] ", attachment_file_name);
2525                 }
2526                 else
2527                         EM_DEBUG_LOG("UnKnown Extesnsion");
2528
2529         }
2530         memset(result_file_name, 0 , result_file_name_buffer_length);
2531         EM_SAFE_STRNCPY(result_file_name, attachment_file_name, result_file_name_buffer_length - 1);
2532         EM_DEBUG_LOG("*result_file_name[%s]", result_file_name);
2533         ret = true;
2534
2535 FINISH_OFF:
2536         if (err_code)
2537                 *err_code = err;
2538         EM_DEBUG_FUNC_END();
2539         return ret;
2540 }
2541
2542 #ifdef __FEATURE_LOCAL_ACTIVITY__
2543 INTERNAL_FUNC int emcore_add_activity(emstorage_activity_tbl_t *new_activity, int *err_code)
2544 {
2545         EM_DEBUG_FUNC_BEGIN();
2546
2547         EM_DEBUG_LOG("\t new_activity[%p], err_code[%p]", new_activity, err_code);
2548
2549         /*  default variable */
2550         int ret = false;
2551         int err = EMAIL_ERROR_NONE;
2552
2553         if (!new_activity) {
2554                 EM_DEBUG_LOG("\t new_activity[%p]\n", new_activity);
2555                 err = EMAIL_ERROR_INVALID_PARAM;
2556                 goto FINISH_OFF;
2557         }
2558         if (!emstorage_add_activity(new_activity, false, &err)) {
2559                 EM_DEBUG_LOG("\t emstorage_add_activity falied - %d\n", err);
2560
2561                 goto FINISH_OFF;
2562         }
2563         ret = true;
2564
2565 FINISH_OFF:
2566         if (err_code)
2567                 *err_code = err;
2568
2569         return ret;
2570 }
2571
2572 INTERNAL_FUNC int emcore_delete_activity(emstorage_activity_tbl_t *activity, int *err_code)
2573 {
2574         EM_DEBUG_FUNC_BEGIN();
2575
2576         EM_DEBUG_LOG("\t new_activity[%p], err_code[%p]", activity, err_code);
2577
2578         /*  default variable */
2579         int ret = false;
2580         int err = EMAIL_ERROR_NONE;
2581         if (!activity) {
2582                 EM_DEBUG_LOG("\t new_activity[%p]\n", activity);
2583
2584                 err = EMAIL_ERROR_INVALID_PARAM;
2585                 goto FINISH_OFF;
2586         }
2587         if (!emstorage_delete_local_activity(activity, true, &err)) {
2588                 EM_DEBUG_LOG("\t emstorage_delete_local_activity falied - %d\n", err);
2589
2590                 goto FINISH_OFF;
2591         }
2592         ret = true;
2593
2594 FINISH_OFF:
2595         if (err_code)
2596                 *err_code = err;
2597
2598         return ret;
2599 }
2600
2601 INTERNAL_FUNC int emcore_get_next_activity_id(int *activity_id, int *err_code)
2602 {
2603         EM_DEBUG_FUNC_BEGIN();
2604
2605         int ret = false;
2606         int err = EMAIL_ERROR_NONE;
2607
2608         if (NULL == activity_id)
2609         {
2610                 EM_DEBUG_EXCEPTION("\t activity_id[%p]", activity_id);
2611
2612                 err = EMAIL_ERROR_INVALID_PARAM;
2613                 goto FINISH_OFF;
2614         }
2615
2616         if (false == emstorage_get_next_activity_id(activity_id, &err)) {
2617                 EM_DEBUG_LOG("\t emstorage_get_next_activity_id failed - %d\n", err);
2618                 goto FINISH_OFF;
2619         }
2620
2621         ret = true;
2622
2623         FINISH_OFF:
2624         if (NULL != err_code) {
2625                 *err_code = err;
2626         }
2627
2628         return ret;
2629
2630 }
2631
2632 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
2633
2634
2635 INTERNAL_FUNC void emcore_free_rule(email_rule_t* rule)
2636 {
2637         EM_DEBUG_FUNC_BEGIN();
2638
2639         if (!rule)
2640                 return;
2641
2642         EM_SAFE_FREE(rule->value);
2643
2644         EM_DEBUG_FUNC_END();
2645 }
2646
2647 INTERNAL_FUNC int emcore_search_string_from_file(char *file_path, char *search_string, int *result)
2648 {
2649         EM_DEBUG_FUNC_BEGIN("file_path : [%s], search_string : [%s]", file_path, search_string);
2650         int error = EMAIL_ERROR_NONE;
2651         int file_size = 0;
2652         int p_result = false;
2653         FILE *fp = NULL;
2654         char *buf = NULL;
2655         char *stripped = NULL;
2656
2657         if (!search_string || !file_path || !result) {
2658                 EM_DEBUG_EXCEPTION("Invalid parameter");
2659                 error = EMAIL_ERROR_INVALID_PARAM;
2660                 return error;
2661         }
2662
2663         fp = fopen(file_path, "r");
2664         if (fp == NULL) {
2665                 EM_DEBUG_EXCEPTION("fopen failed");
2666                 error = EMAIL_ERROR_SYSTEM_FAILURE;
2667                 goto FINISH_OFF;
2668         }
2669
2670         if (!emcore_get_file_size(file_path, &file_size, &error)) {
2671                 EM_DEBUG_EXCEPTION("emcore_get_file_size failed");
2672                 goto FINISH_OFF;
2673         }
2674
2675         buf = em_malloc(file_size);
2676         if (buf == NULL) {
2677                 EM_DEBUG_EXCEPTION("em_malloc failed");
2678                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2679                 goto FINISH_OFF;
2680         }
2681
2682         if (fread(buf, sizeof(char), file_size, fp) != file_size) {
2683                 EM_DEBUG_EXCEPTION("Get the data from file : failed");
2684                 error = EMAIL_ERROR_SYSTEM_FAILURE;
2685                 goto FINISH_OFF;
2686         }
2687
2688         /*prevent 35586*/
2689         stripped = em_replace_all_string(buf, CRLF_STRING, "");
2690         if (strstr(stripped, search_string))
2691                 p_result = true;
2692
2693 FINISH_OFF:
2694         if(!p_result)
2695                 EM_DEBUG_LOG("Search string[%s] not found",search_string);
2696
2697         *result = p_result;
2698
2699         if (fp)
2700                 fclose(fp);
2701
2702         EM_SAFE_FREE(buf);
2703         EM_SAFE_FREE(stripped);  /*prevent 35586*/
2704
2705         EM_DEBUG_FUNC_END("error:[%d]", error);
2706         return error;
2707 }
2708
2709 INTERNAL_FUNC int emcore_load_query_from_file(char *file_path, char ***query_array, int *array_len)
2710 {
2711         EM_DEBUG_FUNC_BEGIN("file_path : [%s], query_array : [%p]", file_path, *query_array);
2712         int error = EMAIL_ERROR_NONE;
2713         int file_size = 0;
2714         int vector_len = 0;
2715         int i = 0;
2716         FILE *fp = NULL;
2717         char *buf = NULL;
2718         char **result_vector = NULL;
2719
2720         if (!file_path) {
2721                 EM_DEBUG_EXCEPTION("Invalid parameter");
2722                 error = EMAIL_ERROR_INVALID_PARAM;
2723                 return error;
2724         }
2725
2726         fp = fopen(file_path, "r");
2727         if (fp == NULL) {
2728                 EM_DEBUG_EXCEPTION("fopen failed");
2729                 error = EMAIL_ERROR_SYSTEM_FAILURE;
2730                 goto FINISH_OFF;
2731         }
2732
2733         if (!emcore_get_file_size(file_path, &file_size, &error)) {
2734                 EM_DEBUG_EXCEPTION("emcore_get_file_size failed");
2735                 goto FINISH_OFF;
2736         }
2737
2738         buf = em_malloc(file_size);
2739         if (buf == NULL) {
2740                 EM_DEBUG_EXCEPTION("em_malloc failed");
2741                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2742                 goto FINISH_OFF;
2743         }
2744
2745         if (fread(buf, sizeof(char), file_size, fp) != file_size) {
2746                 EM_DEBUG_EXCEPTION("Get the data from file : failed");
2747                 error = EMAIL_ERROR_SYSTEM_FAILURE;
2748                 goto FINISH_OFF;
2749         }
2750
2751         result_vector = g_strsplit(buf, ";", -1);
2752         if (!result_vector || g_strv_length(result_vector) <= 0) {
2753                 EM_DEBUG_EXCEPTION("Parsing sql file failed");
2754                 error = EMAIL_ERROR_SYSTEM_FAILURE;
2755                 goto FINISH_OFF;
2756         }
2757
2758         vector_len = g_strv_length(result_vector);
2759         if (vector_len <= 0) {
2760                 EM_DEBUG_EXCEPTION("vector length : [%d]", vector_len);
2761                 error = EMAIL_ERROR_SYSTEM_FAILURE;
2762                 goto FINISH_OFF;
2763         }
2764
2765         *query_array = (char **)calloc(vector_len, sizeof(char *));
2766
2767         for (i = 0; i < vector_len; i++) {
2768                 if (result_vector[i]) {
2769                         char *str = g_strconcat(result_vector[i], ";", NULL);
2770                         (*query_array)[i] = str;
2771                 }
2772         }
2773
2774         if (array_len) {
2775                 *array_len = vector_len;
2776                 EM_DEBUG_LOG("SQL string array length : [%d]", vector_len);
2777         }
2778
2779 FINISH_OFF:
2780
2781         if (fp)
2782                 fclose(fp);
2783
2784         EM_SAFE_FREE(buf);
2785
2786         if (result_vector) {
2787                 g_strfreev(result_vector);
2788         }
2789
2790         EM_DEBUG_FUNC_END("error:[%d]", error);
2791         return error;
2792 }
2793
2794 static int convert_contact_err_to_email_err(int contact_err)
2795 {
2796         int err = EMAIL_ERROR_NONE;
2797
2798         switch (contact_err) {
2799         case CONTACTS_ERROR_NONE :
2800                 err = EMAIL_ERROR_NONE;
2801                 break;
2802         case CONTACTS_ERROR_OUT_OF_MEMORY :
2803                 err = EMAIL_ERROR_OUT_OF_MEMORY;
2804                 break;
2805         case CONTACTS_ERROR_INVALID_PARAMETER :
2806                 err = EMAIL_ERROR_INVALID_PARAM;
2807                 break;
2808         case CONTACTS_ERROR_NO_DATA :
2809                 err = EMAIL_ERROR_DATA_NOT_FOUND;
2810                 break;
2811         case CONTACTS_ERROR_DB :
2812                 err = EMAIL_ERROR_DB_FAILURE;
2813                 break;
2814         case CONTACTS_ERROR_IPC :
2815                 err = EMAIL_ERROR_IPC_CONNECTION_FAILURE;
2816                 break;
2817         case CONTACTS_ERROR_SYSTEM:
2818                 err = EMAIL_ERROR_SYSTEM_FAILURE;
2819                 break;
2820         case CONTACTS_ERROR_INTERNAL:
2821         default:
2822                 err = EMAIL_ERROR_UNKNOWN;
2823                 break;
2824         }
2825         return err;
2826 }
2827
2828 int emcore_get_mail_contact_info(email_mail_contact_info_t *contact_info, char *full_address, int *err_code)
2829 {
2830         EM_DEBUG_FUNC_BEGIN("contact_info[%p], full_address[%s], err_code[%p]", contact_info, full_address, err_code);
2831         
2832         int ret = false;
2833         int err = EMAIL_ERROR_NONE;
2834         
2835         if (!emcore_get_mail_contact_info_with_update(contact_info, full_address, 0, &err))
2836                 EM_DEBUG_EXCEPTION("emcore_get_mail_contact_info_with_update failed [%d]", err);
2837         else
2838                 ret = true;
2839         
2840         if (err_code != NULL)
2841                 *err_code = err;
2842         
2843         return ret;
2844 }
2845
2846 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)
2847 {
2848         EM_DEBUG_FUNC_BEGIN();
2849         int contact_err = CONTACTS_ERROR_NONE;
2850         
2851         contacts_query_h query = NULL;
2852         contacts_filter_h filter = NULL;
2853         contacts_list_h list = NULL;
2854
2855         if ((contact_err = contacts_query_create(contact_uri, &query)) != CONTACTS_ERROR_NONE) {
2856                 EM_DEBUG_EXCEPTION("contacts_query_create failed");
2857                 goto FINISH_OFF;
2858         }
2859         
2860         if ((contact_err = contacts_filter_create(contact_uri, &filter)) != CONTACTS_ERROR_NONE) {
2861                 EM_DEBUG_EXCEPTION("contacts_filter_create failed");
2862                 goto FINISH_OFF;
2863         }
2864
2865         if ((contact_err = contacts_filter_add_str(filter, address_property_id, CONTACTS_MATCH_EXACTLY, address)) != CONTACTS_ERROR_NONE) {
2866                 EM_DEBUG_EXCEPTION("contacts_filter_add_str failed");
2867                 goto FINISH_OFF;
2868         }
2869
2870         if ((contact_err = contacts_filter_add_operator(filter, CONTACTS_FILTER_OPERATOR_AND)) != CONTACTS_ERROR_NONE) {
2871                 EM_DEBUG_EXCEPTION("contacts_filter_add_operator failed");
2872                 goto FINISH_OFF;
2873         }
2874
2875         if ((contact_err = contacts_filter_add_bool(filter, favorite_property_id, is_favorite)) != CONTACTS_ERROR_NONE) {
2876                 EM_DEBUG_EXCEPTION("contacts_filter_add_bool failed");
2877                 goto FINISH_OFF;
2878         }
2879
2880         if ((contact_err = contacts_query_set_filter(query, filter)) != CONTACTS_ERROR_NONE) {
2881                 EM_DEBUG_EXCEPTION("contacts_query_set_filter failed");
2882                 goto FINISH_OFF;
2883         }
2884
2885         if ((contact_err = contacts_db_get_records_with_query(query, 0, limit, &list)) != CONTACTS_ERROR_NONE) {
2886                 EM_DEBUG_EXCEPTION("contacts_db_get_record_with_query failed");
2887                 goto FINISH_OFF;
2888         }
2889
2890         if ((contact_err = contacts_list_get_current_record_p(list, contacts_record)) != CONTACTS_ERROR_NONE) {
2891                 EM_DEBUG_EXCEPTION("contacts_list_get_current_record_p failed");
2892                 goto FINISH_OFF;
2893         }
2894
2895 FINISH_OFF:
2896
2897         if (query != NULL)
2898                 contacts_query_destroy(query);
2899
2900         if (filter != NULL)
2901                 contacts_filter_destroy(filter);
2902
2903         if (list != NULL)
2904                 contacts_list_destroy(list, false);
2905
2906         return contact_err;
2907 }
2908
2909 int emcore_search_contact_info_by_address(const char *contact_uri, int property_id, char *address, int limit, contacts_record_h *contacts_record)
2910 {
2911         EM_DEBUG_FUNC_BEGIN();
2912         int contact_err = CONTACTS_ERROR_NONE;
2913         
2914         contacts_query_h query = NULL;
2915         contacts_filter_h filter = NULL;
2916         contacts_list_h list = NULL;
2917
2918         if ((contact_err = contacts_query_create(contact_uri, &query)) != CONTACTS_ERROR_NONE) {
2919                 EM_DEBUG_EXCEPTION("contacts_query_create failed");
2920                 goto FINISH_OFF;
2921         }
2922         
2923         if ((contact_err = contacts_filter_create(contact_uri, &filter)) != CONTACTS_ERROR_NONE) {
2924                 EM_DEBUG_EXCEPTION("contacts_filter_create failed");
2925                 goto FINISH_OFF;
2926         }
2927
2928         if ((contact_err = contacts_filter_add_str(filter, property_id, CONTACTS_MATCH_EXACTLY, address)) != CONTACTS_ERROR_NONE) {
2929                 EM_DEBUG_EXCEPTION("contacts_filter_add_str failed");
2930                 goto FINISH_OFF;
2931         }
2932
2933         if ((contact_err = contacts_query_set_filter(query, filter)) != CONTACTS_ERROR_NONE) {
2934                 EM_DEBUG_EXCEPTION("contacts_query_set_filter failed");
2935                 goto FINISH_OFF;
2936         }
2937
2938         if ((contact_err = contacts_db_get_records_with_query(query, 0, limit, &list)) != CONTACTS_ERROR_NONE) {
2939                 EM_DEBUG_EXCEPTION("contacts_db_get_record_with_query failed");
2940                 goto FINISH_OFF;
2941         }
2942
2943         if ((contact_err = contacts_list_get_current_record_p(list, contacts_record)) != CONTACTS_ERROR_NONE) {
2944                 EM_DEBUG_EXCEPTION("contacts_list_get_current_record_p failed");
2945                 goto FINISH_OFF;
2946         }
2947
2948 FINISH_OFF:
2949
2950         if (query != NULL)
2951                 contacts_query_destroy(query);
2952
2953         if (filter != NULL)
2954                 contacts_filter_destroy(filter);
2955
2956         if (list != NULL)
2957                 contacts_list_destroy(list, false);
2958
2959         return contact_err;
2960 }
2961
2962 int emcore_set_contacts_log(int account_id, char *email_address, char *subject, time_t date_time, email_action_t action)
2963 {
2964         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);
2965         
2966         int err = EMAIL_ERROR_NONE;
2967         int contacts_error = CONTACTS_ERROR_NONE;
2968         int person_id = 0;
2969         int action_type = 0;
2970         
2971         contacts_record_h phone_record = NULL;
2972         contacts_record_h person_record = NULL; 
2973
2974         if ((contacts_error = contacts_connect2()) != CONTACTS_ERROR_NONE) {
2975                 EM_DEBUG_EXCEPTION("Open connect service failed [%d]", contacts_error);
2976                 goto FINISH_OFF;
2977         }
2978
2979         if ((contacts_error = contacts_record_create(_contacts_phone_log._uri, &phone_record)) != CONTACTS_ERROR_NONE) {
2980                 EM_DEBUG_EXCEPTION("contacts_query_create failed [%d]", contacts_error);
2981                 goto FINISH_OFF;
2982         }
2983
2984         /* Set email address */
2985         if ((contacts_error = contacts_record_set_str(phone_record, _contacts_phone_log.address, email_address)) != CONTACTS_ERROR_NONE) {
2986                 EM_DEBUG_EXCEPTION("contacts_record_set_str [address] failed [%d]", contacts_error);
2987                 goto FINISH_OFF;
2988         }
2989
2990         /* Search contact person info */
2991         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) {
2992                 EM_DEBUG_EXCEPTION("emcore_search_contact_info_by_address failed [%d]", contacts_error);
2993                 EM_DEBUG_LOG("Not match person");
2994         } else {
2995                 /* Get person_id in contacts_person_email record  */
2996                 if (person_record  && (contacts_error = contacts_record_get_int(person_record, _contacts_person_email.person_id, &person_id)) != CONTACTS_ERROR_NONE) {
2997                         EM_DEBUG_EXCEPTION("contacts_record_get_str failed [%d]", contacts_error);
2998                         goto FINISH_OFF;
2999                 }
3000
3001                 /* Set the person id */
3002                 if ((contacts_error = contacts_record_set_int(phone_record, _contacts_phone_log.person_id, person_id)) != CONTACTS_ERROR_NONE) {
3003                         EM_DEBUG_EXCEPTION("contacts_record_set_int [person id] failed [%d]", contacts_error);
3004                         goto FINISH_OFF;
3005                 }
3006         }
3007
3008         switch (action) {
3009         case EMAIL_ACTION_SEND_MAIL :
3010                 action_type = CONTACTS_PLOG_TYPE_EMAIL_SENT;
3011                 break;
3012         case EMAIL_ACTION_SYNC_HEADER :
3013                 action_type = CONTACTS_PLOG_TYPE_EMAIL_RECEIVED;
3014                 break;
3015         default :
3016                 EM_DEBUG_EXCEPTION("Unknown action type");
3017                 goto FINISH_OFF;
3018         }
3019
3020         /* Set log type */
3021         if ((contacts_error = contacts_record_set_int(phone_record, _contacts_phone_log.log_type, action_type)) != CONTACTS_ERROR_NONE) {
3022                 EM_DEBUG_EXCEPTION("contacts_record_set_int [log_type] failed [%d]", contacts_error);
3023                 goto FINISH_OFF;
3024         }
3025
3026         /* Set log time */
3027         if ((contacts_error = contacts_record_set_int(phone_record, _contacts_phone_log.log_time, (int)date_time)) != CONTACTS_ERROR_NONE) {
3028                 EM_DEBUG_EXCEPTION("contacts_record_set_str [address] failed [%d]", contacts_error);
3029                 goto FINISH_OFF;
3030         }
3031
3032         /* Set subject */
3033         if ((contacts_error = contacts_record_set_str(phone_record, _contacts_phone_log.extra_data2, subject)) != CONTACTS_ERROR_NONE) {
3034                 EM_DEBUG_EXCEPTION("contacts_record_set_str [subject] failed [%d]", contacts_error);
3035                 goto FINISH_OFF;
3036         }
3037
3038         /* Set Mail id */
3039         if ((contacts_error = contacts_record_set_int(phone_record, _contacts_phone_log.extra_data1, account_id)) != CONTACTS_ERROR_NONE) {
3040                 EM_DEBUG_EXCEPTION("contacts_record_set_int [mail id] failed [%d]", contacts_error);
3041                 goto FINISH_OFF;
3042         }
3043
3044         /* Insert the record in DB */
3045         if ((contacts_error = contacts_db_insert_record(phone_record, NULL)) != CONTACTS_ERROR_NONE) {
3046                 EM_DEBUG_EXCEPTION("contacts_db_insert_record failed [%d]",contacts_error );
3047                 goto FINISH_OFF;
3048         }
3049
3050 FINISH_OFF:
3051
3052         if (phone_record != NULL)
3053                 contacts_record_destroy(phone_record, false);
3054
3055         if (person_record != NULL)
3056                 contacts_record_destroy(person_record, false);
3057
3058         err = convert_contact_err_to_email_err(contacts_error);
3059
3060         if ((contacts_error = contacts_disconnect2()) != CONTACTS_ERROR_NONE) {
3061                 EM_DEBUG_EXCEPTION("Open connect service failed [%d]", contacts_error);
3062                 err = convert_contact_err_to_email_err(contacts_error);
3063         }
3064
3065         EM_DEBUG_FUNC_END("err [%d]", err);
3066         return err;
3067 }
3068
3069 INTERNAL_FUNC int emcore_set_sent_contacts_log(emstorage_mail_tbl_t *input_mail_data)
3070 {
3071         EM_DEBUG_FUNC_BEGIN("input_mail_data : [%p]", input_mail_data);
3072         
3073         int i = 0;
3074         int err = EMAIL_ERROR_NONE;
3075         char email_address[MAX_EMAIL_ADDRESS_LENGTH];
3076         char *address_array[3] = {input_mail_data->full_address_to, input_mail_data->full_address_cc, input_mail_data->full_address_bcc};
3077         ADDRESS *addr = NULL;
3078         ADDRESS *p_addr = NULL;
3079
3080         for (i = 0; i < 3; i++) {
3081                 if (address_array[i] && address_array[i][0] != 0) {
3082                         rfc822_parse_adrlist(&addr, address_array[i], NULL);
3083                         for (p_addr = addr ; p_addr ;p_addr = p_addr->next) {
3084                                 SNPRINTF(email_address, MAX_EMAIL_ADDRESS_LENGTH, "%s@%s", addr->mailbox, addr->host);
3085                                 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) {
3086                                         EM_DEBUG_EXCEPTION("emcore_set_contacts_log failed : [%d]", err);
3087                                         goto FINISH_OFF;
3088                                 }
3089                         }
3090                 
3091                         if (addr) {     
3092                                 mail_free_address(&addr);
3093                                 addr = NULL;
3094                         }
3095                 }
3096         }
3097
3098 FINISH_OFF:
3099
3100         if (addr) 
3101                 mail_free_address(&addr);
3102
3103         EM_DEBUG_FUNC_END("err [%d]", err);
3104         return err;
3105 }
3106
3107 INTERNAL_FUNC int emcore_set_received_contacts_log(emstorage_mail_tbl_t *input_mail_data)
3108 {
3109         EM_DEBUG_FUNC_BEGIN("input_mail_data : [%p]", input_mail_data);
3110         int err = EMAIL_ERROR_NONE;
3111
3112         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) {
3113                 EM_DEBUG_EXCEPTION("emcore_set_contacts_log failed [%d]", err);
3114         }
3115
3116         EM_DEBUG_FUNC_END("err [%d]", err);
3117         return err;
3118 }
3119
3120 INTERNAL_FUNC int emcore_delete_contacts_log(int account_id)
3121 {
3122         EM_DEBUG_FUNC_BEGIN("account_id [%d]", account_id);
3123
3124         int err = EMAIL_ERROR_NONE;
3125         int contacts_error = CONTACTS_ERROR_NONE;
3126
3127         if ((contacts_error = contacts_connect2()) != CONTACTS_ERROR_NONE) {
3128                 EM_DEBUG_EXCEPTION("Open connect service failed [%d]", contacts_error);
3129                 goto FINISH_OFF;
3130         }
3131         
3132         /* Delete record of the account id */
3133         if ((contacts_error = contacts_phone_log_delete(CONTACTS_PHONE_LOG_DELETE_BY_EMAIL_EXTRA_DATA1, account_id)) != CONTACTS_ERROR_NONE) {
3134                 EM_DEBUG_EXCEPTION("contacts_phone_log_delete failed [%d]", contacts_error);
3135         }
3136         
3137
3138 FINISH_OFF:
3139         err = convert_contact_err_to_email_err(contacts_error);
3140
3141         if ((contacts_error = contacts_disconnect2()) != CONTACTS_ERROR_NONE) {
3142                 EM_DEBUG_EXCEPTION("Open connect service failed [%d]", contacts_error);
3143                 err = convert_contact_err_to_email_err(contacts_error);
3144         }
3145
3146         EM_DEBUG_FUNC_END("err [%d]", err);
3147         return err;
3148 }
3149
3150 INTERNAL_FUNC int emcore_get_mail_display_name(char *email_address, char **contact_display_name, int *err_code)
3151 {
3152         EM_DEBUG_FUNC_BEGIN("contact_name_value[%s], contact_display_name[%p]", email_address, contact_display_name);
3153
3154         int contact_err = 0;
3155         int ret = false;
3156         char *display = NULL;
3157         /* Contact variable */
3158         contacts_record_h record = NULL;
3159
3160         if ((contact_err = contacts_connect2()) != CONTACTS_ERROR_NONE) {
3161                 EM_DEBUG_EXCEPTION("Open connect service failed [%d]", contact_err);
3162                 goto FINISH_OFF;
3163         }
3164
3165         if ((contact_err = emcore_search_contact_info_by_address(_contacts_contact_email._uri, _contacts_contact_email.email, email_address, 1, &record)) != CONTACTS_ERROR_NONE) {
3166                 EM_DEBUG_EXCEPTION("emcore_search_contact_info_by_address failed");
3167                 goto FINISH_OFF;
3168         }
3169 /*
3170         if ((contact_err = contacts_list_get_count(list, &list_count)) != CONTACTS_ERROR_NONE) {
3171                 EM_DEBUG_EXCEPTION("contacts_list_get_count failed");
3172                 goto FINISH_OFF;
3173         }
3174
3175         if (list_count > 1) {
3176                 EM_DEBUG_EXCEPTION("Duplicated contacts info");
3177                 contact_err = CONTACTS_ERROR_INTERNAL;
3178                 goto FINISH_OFF;
3179         } else if (list_count == 0) {
3180                 EM_DEBUG_EXCEPTION("Not found contact info");
3181                 contact_err = CONTACTS_ERROR_NO_DATA;
3182                 goto FINISH_OFF;
3183         }
3184 */
3185
3186         if (contacts_record_get_str(record, _contacts_contact_email.display_name, &display) != CONTACTS_ERROR_NONE) {
3187                 EM_DEBUG_EXCEPTION("contacts_record_get_str failed");
3188                 goto FINISH_OFF;
3189         }
3190
3191         ret = true;
3192
3193 FINISH_OFF:
3194
3195         contacts_disconnect2();
3196
3197         if (record != NULL)
3198                 contacts_record_destroy(record, false);
3199
3200         if (ret) {
3201                 *contact_display_name = display;
3202         } else {
3203                 *contact_display_name = NULL;
3204                 EM_SAFE_FREE(display);
3205         }
3206
3207         if (err_code != NULL)
3208                 *err_code = convert_contact_err_to_email_err(contact_err);
3209
3210         return ret;
3211 }
3212
3213
3214 #ifdef __FEATURE_BLOCKING_MODE__
3215
3216 #define ALLOWED_CONTACT_TYPE_NONE      0
3217 #define ALLOWED_CONTACT_TYPE_ALL       1
3218 #define ALLOWED_CONTACT_TYPE_FAVORITES 2
3219 #define ALLOWED_CONTACT_TYPE_CUSTOM    3
3220
3221 static int blocking_mode_status = false;
3222 static int blocking_mode_of_setting = false;
3223
3224 INTERNAL_FUNC bool emcore_init_blocking_mode_status()
3225 {
3226         EM_DEBUG_FUNC_BEGIN("blocking_mode_of_setting : [%d]", blocking_mode_of_setting);
3227
3228         if (vconf_get_bool(VCONFKEY_SETAPPL_BLOCKINGMODE_NOTIFICATIONS, &blocking_mode_of_setting) != 0) {
3229                 EM_DEBUG_EXCEPTION("vconf_get_bool failed");
3230                 return false;
3231         }
3232
3233         EM_DEBUG_FUNC_END();
3234         return true;
3235 }
3236
3237 INTERNAL_FUNC void emcore_set_blocking_mode_of_setting(int input_blocking_mode_of_setting)
3238 {
3239         blocking_mode_of_setting = input_blocking_mode_of_setting;
3240 }
3241
3242 INTERNAL_FUNC int emcore_get_blocking_mode_status()
3243 {
3244         return blocking_mode_status;
3245 }
3246
3247 INTERNAL_FUNC void emcore_set_blocking_mode_status(int blocking_mode)
3248 {
3249         blocking_mode_status = blocking_mode;
3250 }
3251
3252 INTERNAL_FUNC int emcore_check_blocking_mode(char *sender_address, int *blocking_status)
3253 {
3254         EM_DEBUG_FUNC_BEGIN();
3255         int err = EMAIL_ERROR_NONE;
3256         int contact_error = 0;
3257         int person_id = 0;
3258         int allowed_contact_type = 0; /* 0 : NONE, 1 : All contacts, 2 : Favorites, 3 : Custom */
3259         char *person_id_string = NULL;
3260         char *str = NULL;
3261         char *token = NULL;
3262         contacts_record_h record = NULL;
3263
3264         if (!blocking_mode_of_setting) {
3265                 EM_DEBUG_LOG("Blocking mode is OFF");
3266                 return err;             
3267         }
3268
3269         if (vconf_get_int(VCONFKEY_SETAPPL_BLOCKINGMODE_ALLOWED_CONTACT_TYPE, &allowed_contact_type) != 0) {
3270                 EM_DEBUG_EXCEPTION("vconf_get_int failed");
3271                 err = EMAIL_ERROR_SYSTEM_FAILURE;
3272                 goto FINISH_OFF;
3273         }
3274
3275         if ((contact_error = contacts_connect2()) != CONTACTS_ERROR_NONE) {
3276                 EM_DEBUG_EXCEPTION("Open connect service failed [%d]", contact_error);
3277                 goto FINISH_OFF;
3278         }
3279
3280         switch (allowed_contact_type) {
3281         case ALLOWED_CONTACT_TYPE_NONE :
3282                 EM_DEBUG_LOG("allowed_contact_type is none : bloacking all(notification)");
3283                 *blocking_status = true;
3284                 break;
3285         case ALLOWED_CONTACT_TYPE_ALL :
3286                 EM_DEBUG_LOG("allowed_contact_type is ALL");
3287                 /* Search the allowed contact type in contact DB */
3288                 if ((contact_error = emcore_search_contact_info_by_address(_contacts_person_email._uri, _contacts_person_email.email, sender_address, 1, &record)) != CONTACTS_ERROR_NONE) {
3289                         EM_DEBUG_EXCEPTION("emcore_search_contact_info_by_address failed : [%d]", contact_error);
3290                         goto FINISH_OFF;
3291                 }
3292
3293                 if (record == NULL) {
3294                         EM_DEBUG_LOG("No search contact info");
3295                         goto FINISH_OFF; 
3296                 }
3297
3298                 *blocking_status = false;
3299                 break;
3300
3301         case ALLOWED_CONTACT_TYPE_FAVORITES :
3302                 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) {
3303                         EM_DEBUG_EXCEPTION("emcore_search_contact_info failed : [%d]", contact_error);
3304                         goto FINISH_OFF;
3305                 }
3306
3307                 if (record == NULL) {
3308                         EM_DEBUG_LOG("No search contact info");
3309                         goto FINISH_OFF; 
3310                 }
3311
3312                 *blocking_status = false;
3313                 break;
3314
3315         case ALLOWED_CONTACT_TYPE_CUSTOM :
3316                 person_id_string = vconf_get_str(VCONFKEY_SETAPPL_BLOCKINGMODE_ALLOWED_CONTACT_LIST);
3317                 if (person_id_string == NULL) {
3318                         EM_DEBUG_LOG("Custom allowed contact type is NULL");
3319                         goto FINISH_OFF;
3320                 }       
3321
3322                 if ((contact_error = emcore_search_contact_info_by_address(_contacts_person_email._uri, _contacts_person_email.email, sender_address, 1, &record)) != CONTACTS_ERROR_NONE) {
3323                         EM_DEBUG_EXCEPTION("emcore_search_contact_info_by_address failed : [%d]", contact_error);
3324                         goto FINISH_OFF;
3325                 }
3326
3327                 if (record == NULL) {
3328                         EM_DEBUG_LOG("No search contact info");
3329                         goto FINISH_OFF; 
3330                 }       
3331
3332                 if (contacts_record_get_int(record, _contacts_contact_email.person_id, &person_id) != CONTACTS_ERROR_NONE) {
3333                         EM_DEBUG_EXCEPTION("contacts_record_get_str failed");
3334                         goto FINISH_OFF;
3335                 }
3336
3337                 token = strtok_r(person_id_string, ",", &str);
3338                 do {
3339                         if (person_id == atoi(token)) {
3340                                 *blocking_status = false;
3341                                 break;                  
3342                         }
3343                 } while ((token = strtok_r(NULL, ",", &str)));
3344
3345                 break;
3346
3347         default:
3348                 EM_DEBUG_EXCEPTION("Invalid parameter : [%d]", allowed_contact_type);
3349                 *blocking_status = true;
3350                 err = EMAIL_ERROR_INVALID_PARAM;
3351         }
3352         
3353 FINISH_OFF :
3354
3355         contacts_disconnect2();
3356
3357         err = convert_contact_err_to_email_err(contact_error);
3358
3359         EM_DEBUG_FUNC_END();
3360         return err;
3361 }
3362
3363 #endif     /* __FEATURE_BLOCKING_MODE__ */
3364 /* EOF */