Tizen 2.0 Release
[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
66 #define LED_TIMEOUT_SECS          12
67 #define G_DISPLAY_LENGTH          256
68
69 #define DIR_SEPERATOR_CH          '/'
70 #define EMAIL_CH_QUOT             '"'
71 #define EMAIL_CH_BRACKET_S        '<'
72 #define EMAIL_CH_BRACKET_E        '>'
73 #define EMAIL_CH_COMMA            ','
74 #define EMAIL_CH_SEMICOLON        ';'
75 #define EMAIL_CH_ROUND_BRACKET_S  '('
76 #define EMAIL_CH_ROUND_BRACKET_E  ')'
77 #define EMAIL_CH_SQUARE_BRACKET_S '['
78 #define EMAIL_CH_SQUARE_BRACKET_E ']'
79 #define EMAIL_CH_SPACE            ' '
80 #define EMAIL_NOTI_ICON_PATH      EMAILPATH"/res/image/Q02_Notification_email.png"
81 #define VCONF_KEY_UNREAD_MAIL_COUNT "db/badge/com.samsung.email"
82
83 typedef struct  _em_transaction_info_type_t {
84         int mail_id;
85         int     handle;
86         struct _em_transaction_info_type_t *next;
87
88 } em_transaction_info_type_t;
89
90 em_transaction_info_type_t  *g_transaction_info_list;
91
92 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},};
93
94 typedef struct emcore_account_list_t emcore_account_list_t;
95 struct emcore_account_list_t {
96         email_account_t *account;
97         emcore_account_list_t *next;
98 };
99
100 INTERNAL_FUNC char *emcore_convert_mutf7_to_utf8(char *mailbox_name)
101 {
102         EM_DEBUG_FUNC_BEGIN();
103         return (char *)(utf8_from_mutf7((unsigned char *)mailbox_name));
104 }
105
106 /*  in smtp case, path argument must be ENCODED_PATH_SMTP */
107 int emcore_get_long_encoded_path_with_account_info(email_account_t *account, char *path, int delimiter, char **long_enc_path, int *err_code)
108 {
109         EM_PROFILE_BEGIN(emCorelongEncodedpath);
110         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);
111
112         int ret = false;
113         int error = EMAIL_ERROR_NONE;
114         char *p = NULL;
115
116         size_t long_enc_path_len = 0;
117
118         if (path == NULL || (path && strncmp(path, ENCODED_PATH_SMTP, EM_SAFE_STRLEN(ENCODED_PATH_SMTP)) != 0)) {               /*  imap or pop3 */
119                 EM_DEBUG_LOG("account->incoming_server_address[%p]", account->incoming_server_address);
120                 EM_DEBUG_LOG("account->incoming_server_address[%s]", account->incoming_server_address);
121
122                 if (!account->incoming_server_address) {
123                         EM_DEBUG_EXCEPTION("account->incoming_server_address is null");
124                         error = EMAIL_ERROR_INVALID_ACCOUNT;
125                         goto FINISH_OFF;
126                 }
127
128                 long_enc_path_len = EM_SAFE_STRLEN(account->incoming_server_address) + EM_SAFE_STRLEN(path) + 64; /*prevent 34357*/
129
130                 *long_enc_path = em_malloc(long_enc_path_len);
131                 if (!*long_enc_path)  {
132                         EM_DEBUG_EXCEPTION("malloc failed...");
133                         error = EMAIL_ERROR_OUT_OF_MEMORY;
134                         goto FINISH_OFF;
135                 }
136
137                 p = *long_enc_path;
138
139                 /*  ex:"{mai.test.com:143/imap} or {mai.test.com:143/imap/tls}my-mailbox" */
140
141                 SNPRINTF(p, long_enc_path_len, "{%s:%d/%s/user=%d",
142                         account->incoming_server_address,
143                         account->incoming_server_port_number,
144                         account->incoming_server_type == EMAIL_SERVER_TYPE_POP3 ? "pop3" : "imap", account->account_id);
145
146                 if (account->incoming_server_secure_connection & 0x01)  {
147                         strncat(p, "/ssl", long_enc_path_len-(EM_SAFE_STRLEN(p)+1));
148                         /* strcat(p, "/tryssl"); */
149                 }
150
151                 /* Currently, receiving servers doesn't require tls.
152                 if (account->incoming_server_secure_connection & 0x02)
153                         strncat(p, "/tls", long_enc_path_len-(EM_SAFE_STRLEN(p)+1));
154                 else
155                         strncat(p, "/notls", long_enc_path_len-(EM_SAFE_STRLEN(p)+1));
156                 */
157
158                 if (account->incoming_server_requires_apop) {
159                         EM_DEBUG_LOG("emcore_get_long_encoded_path - incoming_server_requires_apop - %d", account->incoming_server_requires_apop);
160                         strncat(p, "/apop", long_enc_path_len-(EM_SAFE_STRLEN(p)+1));
161                         EM_DEBUG_LOG("long_enc_path - %s", p);
162                 }
163         }
164         else  {         /*  smtp */
165                 long_enc_path_len = EM_SAFE_STRLEN(account->outgoing_server_address) + 64;
166
167                 *long_enc_path = em_malloc(EM_SAFE_STRLEN(account->outgoing_server_address) + 64);
168                 if (!*long_enc_path) {
169                         EM_DEBUG_EXCEPTION("\t malloc failed...\n");
170
171                         error = EMAIL_ERROR_OUT_OF_MEMORY;
172                         goto FINISH_OFF;
173                 }
174
175                 p = *long_enc_path;
176
177                 /*  ex:"mail.test.com:25/smtp" */
178
179                 SNPRINTF(p, long_enc_path_len, "%s:%d/%s",
180                         account->outgoing_server_address,
181                         account->outgoing_server_port_number,
182                         "smtp");
183
184                 if (account->outgoing_server_need_authentication) {
185                         SNPRINTF(p + EM_SAFE_STRLEN(p), long_enc_path_len-(EM_SAFE_STRLEN(p)), "/user=%d", account->account_id);
186                 }
187
188                 if (account->outgoing_server_secure_connection & 0x01) {
189                         strncat(p, "/ssl", long_enc_path_len-(EM_SAFE_STRLEN(p)+1));
190                         /* strcat(p, "/tryssl"); */
191                 }
192                 if (account->outgoing_server_secure_connection & 0x02)
193                         strncat(p, "/tls", long_enc_path_len-(EM_SAFE_STRLEN(p)+1));
194                 else
195                         strncat(p, "/notls", long_enc_path_len-(EM_SAFE_STRLEN(p)+1));
196         }
197
198         if (path == NULL || (path && strncmp(path, ENCODED_PATH_SMTP, EM_SAFE_STRLEN(ENCODED_PATH_SMTP)) != 0)) {
199                 strncat(p, "}", long_enc_path_len-(EM_SAFE_STRLEN(p)+1));
200
201                 if (path != NULL) {
202                         char *enc_name = NULL;
203
204                         if (!emcore_get_encoded_mailbox_name(path, &enc_name, &error))  {
205                                 EM_DEBUG_EXCEPTION("emcore_get_encoded_mailbox_name failed - %d", error);
206                                 *long_enc_path = NULL;
207                                 goto FINISH_OFF;
208                         }
209
210                         if (enc_name)  {
211                                 strncat(p, enc_name, long_enc_path_len-(EM_SAFE_STRLEN(p)+1));
212                                 EM_SAFE_FREE(enc_name);
213                         }
214                 }
215         }
216
217         ret = true;
218
219 FINISH_OFF:
220         if (ret != true)
221                 EM_SAFE_FREE(p);
222
223         if (err_code != NULL)
224                 *err_code = error;
225         EM_PROFILE_END(emCorelongEncodedpath);
226         return ret;
227 }
228
229 int emcore_get_long_encoded_path(int account_id, char *path, int delimiter, char **long_enc_path, int *err_code)
230 {
231         EM_PROFILE_BEGIN(emCorelongEncodedpath);
232         EM_DEBUG_FUNC_BEGIN("account_id[%d], delimiter[%d], long_enc_path[%p], err_code[%p]", account_id, delimiter, long_enc_path, err_code);
233
234         int ret = false;
235         int error = EMAIL_ERROR_NONE;
236         email_account_t *ref_account = NULL;
237
238         ref_account = emcore_get_account_reference(account_id);
239         if (!ref_account)  {
240                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
241                 error = EMAIL_ERROR_INVALID_ACCOUNT;
242                 goto FINISH_OFF;
243         }
244
245         if (emcore_get_long_encoded_path_with_account_info(ref_account, path, delimiter, long_enc_path, &error) == false) {
246                 EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path_with_account_info failed [%d]", error);
247                 goto FINISH_OFF;
248         }
249
250         ret = true;
251
252 FINISH_OFF:
253
254         if (ref_account) {
255                 emcore_free_account(ref_account);
256                 EM_SAFE_FREE(ref_account);
257         }
258
259         if (err_code != NULL)
260                 *err_code = error;
261         EM_PROFILE_END(emCorelongEncodedpath);
262         return ret;
263 }
264
265 int emcore_get_encoded_mailbox_name(char *name, char **enc_name, int *err_code)
266 {
267         EM_DEBUG_FUNC_BEGIN("name[%s], enc_name[%p], err_code[%p]", name, enc_name, err_code);
268
269         if (!name || !enc_name)  {
270                 if (err_code != NULL)
271                         *err_code = EMAIL_ERROR_INVALID_PARAM;
272                 EM_DEBUG_FUNC_END();
273                 return false;
274         }
275
276         /* encoding mailbox name (Charset->UTF8->UTF7) */
277
278         *enc_name = em_malloc(EM_SAFE_STRLEN(name)+1);
279         if (*enc_name == NULL) {
280                 EM_DEBUG_EXCEPTION("malloc failed...");
281                 if (err_code != NULL)
282                         *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
283                 EM_DEBUG_FUNC_END();
284                 return false;
285         }
286
287         strcpy(*enc_name, name);
288
289         if (err_code != NULL)
290                 *err_code = EMAIL_ERROR_NONE;
291
292         EM_DEBUG_FUNC_END();
293         return true;
294 }
295
296 int emcore_get_temp_file_name(char **filename, int *err_code)
297 {
298         EM_DEBUG_FUNC_BEGIN("filename[%p], err_code[%p]", filename, err_code);
299
300         int ret = false;
301         int error = EMAIL_ERROR_NONE;
302
303         if (filename == NULL) {
304                 EM_DEBUG_EXCEPTION("\t filename[%p]\n", filename);
305                 error = EMAIL_ERROR_INVALID_PARAM;
306                 goto FINISH_OFF;
307         }
308
309         char tempname[512] = {0x00, };
310         struct timeval tv;
311
312
313         gettimeofday(&tv, NULL);
314         srand(tv.tv_usec);
315
316         /* Create Directory If deleted by user*/
317         emstorage_create_dir_if_delete();
318
319         SNPRINTF(tempname, sizeof(tempname), "%s%c%d", MAILTEMP, DIR_SEPERATOR_CH, rand());
320
321         char *p = EM_SAFE_STRDUP(tempname);
322         if (p == NULL) {
323                 EM_DEBUG_EXCEPTION("\t strdup failed...\n");
324                 error = EMAIL_ERROR_OUT_OF_MEMORY;
325                 goto FINISH_OFF;
326         }
327
328         *filename = p;
329
330         ret = true;
331
332 FINISH_OFF:
333         if (err_code != NULL)
334                 *err_code = error;
335         EM_DEBUG_FUNC_END();
336         return ret;
337 }
338
339 int emcore_get_file_name(char *path, char **filename, int *err_code)
340 {
341         EM_DEBUG_FUNC_BEGIN("path[%s], filename[%p], err_code[%p]", path, filename, err_code);
342
343         int ret = false;
344         int error = EMAIL_ERROR_NONE;
345
346         if (!path || !filename) {
347                 EM_DEBUG_EXCEPTION("path[%p], filename[%p]", path, filename);
348
349                 error = EMAIL_ERROR_INVALID_PARAM;
350                 goto FINISH_OFF;
351         }
352
353         int i = (int)EM_SAFE_STRLEN(path);
354
355         /*  get filename */
356         for (; i >= 0; i--)
357                 if (path[i] == DIR_SEPERATOR_CH)
358                         break;
359
360         *filename = path + i + 1;
361
362         ret = true;
363
364 FINISH_OFF:
365                 if (err_code != NULL)
366                 *err_code = error;
367         EM_DEBUG_FUNC_END();
368         return ret;
369 }
370
371 int emcore_get_file_size(char *path, int *size, int *err_code)
372 {
373         EM_DEBUG_FUNC_BEGIN("path[%s], size[%p], err_code[%p]", path, size, err_code);
374
375         int ret = false;
376         int error = EMAIL_ERROR_NONE;
377
378         if ((path == NULL) || (size == NULL)) {
379                 EM_DEBUG_EXCEPTION("\t path[%p], size[%p]\n", path, size);
380
381                 error = EMAIL_ERROR_INVALID_PARAM;
382                 goto FINISH_OFF;
383         }
384
385         struct stat st_buf;
386
387         if (stat(path, &st_buf) < 0)  {
388                 EM_DEBUG_EXCEPTION("\t stat failed - %s\n", path);
389
390                 error = EMAIL_ERROR_SYSTEM_FAILURE;
391                 goto FINISH_OFF;
392         }
393
394         *size = st_buf.st_size;
395
396         ret = true;
397
398 FINISH_OFF:
399         if (err_code != NULL)
400                 *err_code = error;
401         EM_DEBUG_FUNC_END();
402         return ret;
403 }
404
405
406
407 static int _emcore_check_host(char *host)
408 {
409         if (!host)
410                 return 0;
411         return strncmp(host, ".SYNTAX-ERROR.", strlen(".SYNTAX-ERROR."));
412 }
413
414
415
416 int emcore_get_address_count(char *addr_str, int *count, int *err_code)
417 {
418         EM_DEBUG_FUNC_BEGIN("addr_str[%s], count[%p], err_code[%p]", addr_str, count, err_code);
419
420         int ret = false;
421         int error = EMAIL_ERROR_NONE;
422
423         ADDRESS *addr = NULL;
424         ADDRESS *p_addr = NULL;
425         int i = 0, j;
426         char *p = NULL;
427
428
429         if (!count)  {
430                 EM_DEBUG_EXCEPTION("addr_str[%s], count[%p]", addr_str, count);
431                 error = EMAIL_ERROR_INVALID_PARAM;
432                 goto FINISH_OFF;
433         }
434
435         if (addr_str != NULL)  {
436                 em_skip_whitespace(addr_str, &p);
437                 EM_DEBUG_LOG("em_skip_whitespace[p][%s]", p);
438
439
440                 for (i = 0, j = EM_SAFE_STRLEN(p); i < j; i++)
441                         if (p[i] == ';') p[i] = ',';
442                 rfc822_parse_adrlist(&addr, p, NULL);
443                 EM_SAFE_FREE(p);
444
445
446                 for (p_addr = addr, i = 0; p_addr; p_addr = p_addr->next, i++)  {
447                         if (p_addr->mailbox && p_addr->host) {
448                                 if (!strncmp(p_addr->mailbox, "UNEXPECTED_DATA_AFTER_ADDRESS", strlen("UNEXPECTED_DATA_AFTER_ADDRESS"))
449                                 || !strncmp(p_addr->mailbox, "INVALID_ADDRESS", strlen("INVALID_ADDRESS"))
450                                 || !strncmp(p_addr->host, ".SYNTAX-ERROR.", strlen(".SYNTAX-ERROR."))) { /*prevent 34356*/
451                                         EM_DEBUG_LOG("Invalid address ");
452                                         continue;
453                                 }
454                         }
455                         if ((!p_addr->mailbox) || (_emcore_check_host(p_addr->host) == 0)) {
456                                 EM_DEBUG_EXCEPTION("\t invalid address : mailbox[%s], host[%s]\n", p_addr->mailbox, p_addr->host);
457
458                                 error = EMAIL_ERROR_INVALID_ADDRESS;
459                                 /* goto FINISH_OFF; */
460                         }
461                 }
462         }
463
464         *count = i;
465         if (error != EMAIL_ERROR_INVALID_ADDRESS)
466         ret = true;
467
468 FINISH_OFF:
469         if (addr)
470                 mail_free_address(&addr);
471
472         if (err_code != NULL)
473                 *err_code = error;
474         EM_DEBUG_FUNC_END();
475         return ret;
476 }
477
478 INTERNAL_FUNC int emcore_set_network_error(int err_code)
479 {
480         email_session_t *session = NULL;
481
482         EM_DEBUG_FUNC_BEGIN();
483
484         emcore_get_current_session(&session);
485
486         if (!session)
487                 return false;
488
489         session->network = err_code;
490         EM_DEBUG_FUNC_END();
491         return true;
492 }
493
494 int emcore_get_empty_session(email_session_t **session)
495 {
496         EM_DEBUG_FUNC_BEGIN("session[%p]", session);
497
498         /*  lock()... */
499
500         int i;
501
502         for (i = 0; i < SESSION_MAX; i++)  {
503                 if (!g_session_list[i].status)  {
504                         memset(g_session_list+i, 0x00, sizeof(email_session_t));
505                         g_session_list[i].tid = GPOINTER_TO_INT(THREAD_SELF());
506                         g_session_list[i].status = true;
507                         break;
508                 }
509         }
510
511         /*  unlock()... */
512
513         if (session != NULL)
514                 *session = (i != SESSION_MAX) ? &g_session_list[i] : NULL;
515         EM_DEBUG_FUNC_END();
516         return (i != SESSION_MAX) ? true : false;
517 }
518
519 int emcore_clear_session(email_session_t *session)
520 {
521         EM_DEBUG_FUNC_BEGIN();
522
523         if (session)
524                 memset(session, 0x00, sizeof(email_session_t));
525         EM_DEBUG_FUNC_END();
526         return true;
527 }
528
529 int emcore_get_current_session(email_session_t **session)
530 {
531         EM_DEBUG_FUNC_BEGIN("session[%p]", session);
532
533         int i;
534
535         for (i = 0; i < SESSION_MAX; i++)  {
536                 if (g_session_list[i].tid == GPOINTER_TO_INT(THREAD_SELF())) {
537                         if (session)
538                                 *session = g_session_list + i;
539
540                         break;
541                 }
542         }
543
544         if (session)
545                 *session = (i != SESSION_MAX) ? g_session_list + i : NULL;
546         EM_DEBUG_FUNC_END();
547         return (i != SESSION_MAX) ? true : false;
548 }
549
550 int emcore_get_mail_count_by_query(int account_id, int priority_sender, int *total_mail, int *unread_mail, int *err_code)
551 {
552         EM_DEBUG_FUNC_BEGIN();
553
554         int ret = false;
555         int err = EMAIL_ERROR_NONE;
556         int i, count;
557         int type = EMAIL_PRIORITY_SENDER;
558         int unread_count = 0;
559         int total_count = 0;
560         char *conditional_clause_string = NULL;
561
562         int rule_count = 0;
563         int is_completed = 0;
564         emstorage_rule_tbl_t *rule = NULL;
565
566         int filter_count = 0;
567         email_list_filter_t *filter_list = NULL;
568
569         if (priority_sender) {
570                 /* Get rule list */
571                 if (!emstorage_get_rule(ALL_ACCOUNT, type, 0, &rule_count, &is_completed, &rule, true, &err) || !rule) {
572                         EM_DEBUG_EXCEPTION("emstorage_get_rule failed");
573                         goto FINISH_OFF;
574                 }
575
576                 /* Make query for searching unread mail */
577                 if (account_id != ALL_ACCOUNT) {
578                         filter_count = (rule_count * 2) + 3;
579                 } else {
580                         filter_count = (rule_count * 2) + 1;                              // 1 is unseen field.
581                 }
582
583                 filter_list = em_malloc(sizeof(email_list_filter_t) * filter_count);
584                 if (filter_list == NULL) {
585                         EM_DEBUG_EXCEPTION("em_malloc failed");
586                         err = EMAIL_ERROR_OUT_OF_MEMORY;
587                         goto FINISH_OFF;
588                 }
589
590                 for (i = 0, count = 0; i < filter_count - 1; i += 2, count++) {
591                         filter_list[i].list_filter_item_type                             = EMAIL_LIST_FILTER_ITEM_RULE;
592                         filter_list[i].list_filter_item.rule.rule_type                   = EMAIL_LIST_FILTER_RULE_INCLUDE;
593                         filter_list[i].list_filter_item.rule.target_attribute            = EMAIL_MAIL_ATTRIBUTE_FROM;
594                         filter_list[i].list_filter_item.rule.key_value.string_type_value = EM_SAFE_STRDUP(rule[count].value);
595
596                         filter_list[i+1].list_filter_item_type                           = EMAIL_LIST_FILTER_ITEM_OPERATOR;
597                         if (i == (filter_count - 2)) {
598                                 filter_list[i+1].list_filter_item.operator_type          = EMAIL_LIST_FILTER_OPERATOR_AND;
599                         } else {
600                                 filter_list[i+1].list_filter_item.operator_type          = EMAIL_LIST_FILTER_OPERATOR_OR;
601                         }
602                 }
603
604                 filter_list[i].list_filter_item_type                              = EMAIL_LIST_FILTER_ITEM_RULE;
605                 filter_list[i].list_filter_item.rule.rule_type                    = EMAIL_LIST_FILTER_RULE_EQUAL;
606                 filter_list[i].list_filter_item.rule.target_attribute             = EMAIL_MAIL_ATTRIBUTE_FLAGS_SEEN_FIELD;
607                 filter_list[i].list_filter_item.rule.key_value.integer_type_value = 0;
608
609                 if (account_id != ALL_ACCOUNT) {
610                         filter_list[i+1].list_filter_item_type                              = EMAIL_LIST_FILTER_ITEM_OPERATOR;
611                         filter_list[i+1].list_filter_item.operator_type                     = EMAIL_LIST_FILTER_OPERATOR_AND;
612
613                         filter_list[i+2].list_filter_item_type                              = EMAIL_LIST_FILTER_ITEM_RULE;
614                         filter_list[i+2].list_filter_item.rule.rule_type                    = EMAIL_LIST_FILTER_RULE_EQUAL;
615                         filter_list[i+2].list_filter_item.rule.target_attribute             = EMAIL_MAIL_ATTRIBUTE_ACCOUNT_ID;
616                         filter_list[i+2].list_filter_item.rule.key_value.integer_type_value = account_id;
617                 }
618
619                 if ((err = emstorage_write_conditional_clause_for_getting_mail_list(filter_list, filter_count, NULL, 0, -1, -1, &conditional_clause_string)) != EMAIL_ERROR_NONE) {
620                         EM_DEBUG_EXCEPTION("emstorage_write_conditional_clause_for_getting_mail_list failed[%d]", err);
621                         goto FINISH_OFF;
622                 }
623
624                 EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
625
626                 /* Search the mail of priority sender in DB */
627                 if ((err = emstorage_query_mail_count(conditional_clause_string, true, &total_count, &unread_count)) != EMAIL_ERROR_NONE) {
628                         EM_DEBUG_EXCEPTION("emstorage_query_mail_count failed:[%d]", err);
629                         goto FINISH_OFF;
630                 }
631         } else {
632                 if (!emstorage_get_mail_count(account_id, NULL, &total_count, &unread_count, true, &err))  {
633                         EM_DEBUG_EXCEPTION(" emstorage_get_mail_count failed - %d", err);
634
635                         goto FINISH_OFF;
636                 }
637         }
638
639         ret = true;
640
641 FINISH_OFF:
642
643         if (rule)
644                 emstorage_free_rule(&rule, rule_count, NULL);
645
646         if (filter_list)
647                 emstorage_free_list_filter(&filter_list, filter_count);
648
649         EM_SAFE_FREE(conditional_clause_string);
650
651         if (total_mail)
652                 *total_mail = total_count;
653
654         if (unread_mail)
655                 *unread_mail = unread_count;
656
657         if (err_code)
658                 *err_code = err;
659
660         return ret;
661 }
662
663 int emcore_display_unread_in_badge()
664 {
665         EM_DEBUG_FUNC_BEGIN();
666
667         int ret = false;
668         int err = EMAIL_ERROR_NONE;
669         int total_unread_count = 0;
670         int total_mail_count = 0;
671         int badge_ticker = 0;
672         int priority_sender = 0;
673
674
675         /* Get the Global noti ticker */
676         if (vconf_get_bool(VCONFKEY_TICKER_NOTI_BADGE_EMAIL, &badge_ticker) != 0) {
677                 EM_DEBUG_EXCEPTION("vconf_get_bool failed");
678                 err = EMAIL_ERROR_GCONF_FAILURE;
679                 goto FINISH_OFF;
680         }
681
682         /* Get the priority noti ticker */
683         if (!badge_ticker) {
684                 if (vconf_get_bool(VCONF_VIP_NOTI_BADGE_TICKER, &badge_ticker) != 0) {
685                         EM_DEBUG_EXCEPTION("vconf_get_bool failed");
686                         err = EMAIL_ERROR_GCONF_FAILURE;
687                         goto FINISH_OFF;
688                 }
689
690                 if (!badge_ticker) {
691                         EM_DEBUG_LOG("Not display the badge");
692                         ret = true;
693                         goto FINISH_OFF;
694                 }
695
696                 priority_sender = 1;
697         }
698
699         /* Get unread mail count */
700         if (!emcore_get_mail_count_by_query(ALL_ACCOUNT, priority_sender, &total_mail_count, &total_unread_count, &err)) {
701                 EM_DEBUG_EXCEPTION("emcore_get_mail_count_by_query failed");
702                 goto FINISH_OFF;
703         }
704
705         /* temporarily disable : set unread count to badge */
706         if (vconf_set_int(VCONF_KEY_UNREAD_MAIL_COUNT, total_unread_count) != 0) {
707                 EM_DEBUG_EXCEPTION("vconf_set_int failed");
708                 err = EMAIL_ERROR_GCONF_FAILURE;
709                 goto FINISH_OFF;
710         }
711
712         /* Use badge API */
713         badge_error_e badge_err = BADGE_ERROR_NONE;
714         bool exist;
715
716         if((badge_err = badge_is_existing("com.samsung.email", &exist)) != BADGE_ERROR_NONE) {
717                 EM_DEBUG_EXCEPTION("badge_is_existing failed [%d]", badge_err);
718                 err = EMAIL_ERROR_BADGE_API_FAILED;
719                 goto FINISH_OFF;
720         }
721         if (!exist) {
722                 /* create badge */
723                 if((badge_err = badge_create("com.samsung.email", "/usr/bin/email-service")) != BADGE_ERROR_NONE) {
724                         EM_DEBUG_EXCEPTION("badge_create failed [%d]", badge_err);
725                         err = EMAIL_ERROR_BADGE_API_FAILED;
726                         goto FINISH_OFF;
727                 }
728         }
729
730         if((badge_err = badge_set_count("com.samsung.email", total_unread_count)) != BADGE_ERROR_NONE) {
731                 EM_DEBUG_EXCEPTION("badge_set_count failed [%d]", badge_err);
732                 err = EMAIL_ERROR_BADGE_API_FAILED;
733                 goto FINISH_OFF;
734         }
735
736         if (total_unread_count <= 0)
737                 goto FINISH_OFF;
738
739         ret = true;
740
741 FINISH_OFF:
742
743         return ret;
744 }
745
746 static int emcore_layout_multi_noti(notification_h noti, int unread_mail, char *email_address, char *account_name)
747 {
748         EM_DEBUG_FUNC_BEGIN();
749         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
750         char modified_string[10];
751
752         noti_err = notification_set_layout(noti, NOTIFICATION_LY_NOTI_EVENT_MULTIPLE);
753         if (noti_err != NOTIFICATION_ERROR_NONE) {
754                 EM_DEBUG_EXCEPTION("notification_set_layout NOTI_EVENT_SINGLE failed [%d]", noti_err);
755                 goto FINISH_OFF;
756         }
757
758         SNPRINTF(modified_string, sizeof(modified_string), "%d", unread_mail);
759
760         noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_TITLE, "Email", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
761         if (noti_err != NOTIFICATION_ERROR_NONE) {
762                 EM_DEBUG_EXCEPTION("notification_set_text TEXT_TYPE_TITLE failed");
763                 goto FINISH_OFF;
764         }
765
766         noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_EVENT_COUNT, modified_string, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
767         if (noti_err != NOTIFICATION_ERROR_NONE) {
768                 EM_DEBUG_EXCEPTION("notification_set_text TEXT_TYPE_EVENT_COUNT failed");
769                 goto FINISH_OFF;
770         }
771
772         noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT, "new emails", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
773         if (noti_err != NOTIFICATION_ERROR_NONE) {
774                 EM_DEBUG_EXCEPTION("notification_set_text TEXT_TYPE_CONTENT failed");
775                 goto FINISH_OFF;
776         }
777
778         noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_1, email_address, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
779         if (noti_err != NOTIFICATION_ERROR_NONE) {
780                 EM_DEBUG_EXCEPTION("notification_set_text TEXT_TYPE_INFO_1 failed");
781                 goto FINISH_OFF;
782         }
783
784         noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_2, account_name, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
785         if (noti_err != NOTIFICATION_ERROR_NONE) {
786                 EM_DEBUG_EXCEPTION("notification_set_text TEXT_TYPE_INFO_2 failed");
787                 goto FINISH_OFF;
788         }
789
790         noti_err = notification_set_image(noti, NOTIFICATION_IMAGE_TYPE_ICON, EMAIL_NOTI_ICON_PATH);
791         if (noti_err != NOTIFICATION_ERROR_NONE) {
792                 EM_DEBUG_EXCEPTION("notification_set_image TYPE_ICON failed");
793                 goto FINISH_OFF;
794         }
795
796 FINISH_OFF:
797
798         EM_DEBUG_FUNC_END("noti_err : [%d]", noti_err);
799         return noti_err;
800 }
801
802 static int emcore_layout_single_noti(notification_h noti, char *account_name, char *display_sender, time_t time, char *subject)
803 {
804         EM_DEBUG_FUNC_BEGIN();
805         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
806
807         noti_err = notification_set_layout(noti, NOTIFICATION_LY_NOTI_EVENT_SINGLE);
808         if (noti_err != NOTIFICATION_ERROR_NONE) {
809                 EM_DEBUG_EXCEPTION("notification_set_layout NOTI_EVENT_SINGLE failed [%d]", noti_err);
810                 goto FINISH_OFF;
811         }
812
813         noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_TITLE, "Email", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
814         if (noti_err != NOTIFICATION_ERROR_NONE) {
815                 EM_DEBUG_EXCEPTION("notification_set_text TEXT_TYPE_TITLE failed");
816                 goto FINISH_OFF;
817         }
818
819         noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT, account_name, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
820         if (noti_err != NOTIFICATION_ERROR_NONE) {
821                 EM_DEBUG_EXCEPTION("notification_set_text TEXT_TYPE_CONTENT failed");
822                 goto FINISH_OFF;
823         }
824
825         noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_1, display_sender, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
826         if (noti_err != NOTIFICATION_ERROR_NONE) {
827                 EM_DEBUG_EXCEPTION("notification_set_text TEXT_TYPE_INFO_1 failed");
828                 goto FINISH_OFF;
829         }
830
831         /*
832         noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_SUB_1, time, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
833         if (noti_err != NOTIFICATION_ERROR_NONE) {
834                 EM_DEBUG_EXCEPTION("notification_set_text TEXT_TYPE_INFO_SUB_1 failed");
835                 goto FINISH_OFF;
836         }
837         */
838
839         noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_2, subject, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
840         if (noti_err != NOTIFICATION_ERROR_NONE) {
841                 EM_DEBUG_EXCEPTION("notification_set_text TEXT_TYPE_INFO_2 failed");
842                 goto FINISH_OFF;
843         }
844
845         noti_err = notification_set_image(noti, NOTIFICATION_IMAGE_TYPE_ICON, EMAIL_NOTI_ICON_PATH);
846         if (noti_err != NOTIFICATION_ERROR_NONE) {
847                 EM_DEBUG_EXCEPTION("notification_set_image TYPE_ICON failed");
848                 goto FINISH_OFF;
849         }
850
851 FINISH_OFF:
852
853         EM_DEBUG_FUNC_END("noti_err : [%d]", noti_err);
854         return noti_err;
855 }
856
857 static int emcore_add_notification(int account_id, int mail_id, email_action_t action)
858 {
859         EM_DEBUG_FUNC_BEGIN();
860         int err = EMAIL_ERROR_NONE;
861
862         EM_DEBUG_FUNC_END("ret [%d]", err);
863         return err;
864 }
865
866 INTERNAL_FUNC int emcore_show_user_message(int id, email_action_t action, int error)
867 {
868         EM_DEBUG_FUNC_BEGIN("id[%d], action[%d], error[%d]", id, action, error);
869
870         int ret = false;
871
872         if (action == EMAIL_ACTION_SEND_MAIL && error != EMAIL_ERROR_CANCELLED) {
873         /*  In case email is cancelled using cancel button in Outbox there is no need to show Cancel/Retry Pop up */
874                 emstorage_mail_tbl_t *mail_table_data = NULL;
875
876                 if (error == 0) /*  error 0 means 'this is not error' */
877                         return true;
878
879                 if (id <= 0) {
880                         EM_DEBUG_LOG("Invalid mail_id");
881                         return false;
882                 }
883
884                 if (!emstorage_get_mail_by_id(id, &mail_table_data, true, NULL)) {
885                         EM_DEBUG_LOG("Mail not found");
886                         return false;
887                 }
888
889                 if (emcore_add_notification(mail_table_data->account_id, id, action) != EMAIL_ERROR_NONE) {
890                         EM_DEBUG_EXCEPTION("emcore_notification_set error");
891
892                         if (!emstorage_free_mail(&mail_table_data, 1, NULL))
893                                 EM_DEBUG_EXCEPTION("emstorage_free_mail Failed");
894
895                         return false;
896                 }
897
898                 if (!emstorage_free_mail(&mail_table_data, 1, NULL))
899                         EM_DEBUG_EXCEPTION("emstorage_free_mail Failed");
900
901                 ret = true;
902         }
903         EM_DEBUG_FUNC_END("ret [%d]", ret);
904         return ret;
905 }
906
907
908 /* storage space handling - 210709 */
909 int emcore_get_storage_status(void)
910 {
911         EM_DEBUG_FUNC_BEGIN();
912         int storage_status = 0, nError = 0;
913
914         g_type_init();
915
916 #ifdef STORAGE_STATUS
917         nError = vconf_get_int(PS_KEY_SYSTEM_STORAGE_MOVI_STATUS,
918                                                         &storage_status);
919 #endif /*  STORAGE_STATUS */
920
921         if (nError == -1) {
922                 EM_DEBUG_EXCEPTION("vconf_get_int Failed");
923                 return false;
924         }
925         EM_DEBUG_FUNC_END();
926         return storage_status;
927 }
928
929 int emcore_is_storage_full(int *err_code)
930 {
931         EM_DEBUG_FUNC_BEGIN();
932
933         int ret = false;
934         int err = EMAIL_ERROR_NONE;
935         struct statfs buf = {0};
936
937         if (statfs(DATA_PATH, &buf) == -1) {
938                 EM_DEBUG_EXCEPTION("statfs(\"%s\") failed - %d", DATA_PATH, errno);
939                 err = EMAIL_ERROR_SYSTEM_FAILURE;
940                 goto FINISH_OFF;
941         }
942         else  {
943                 long i_free = (buf.f_bfree * buf.f_bsize) / (1024 * 1024);
944                 EM_DEBUG_LOG("f_bfree[%d] f_bsize[%d]", buf.f_bfree, buf.f_bsize);
945                 EM_DEBUG_LOG("Free space of storage is[%ld] MB.", i_free);
946                 if (i_free < EMAIL_LIMITATION_FREE_SPACE)
947                         err = EMAIL_ERROR_MAIL_MEMORY_FULL;
948         }
949
950         if (err == EMAIL_ERROR_MAIL_MEMORY_FULL)
951                 ret = true;
952
953 FINISH_OFF:
954         if (err_code != NULL)
955                 *err_code = err;
956         EM_DEBUG_FUNC_END("ret[%d]", ret);
957         return ret;
958 }
959
960 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)
961 {
962         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);
963
964         struct stat            st_buf;
965         int                    mail_size = 0; /*  size of the plain text body and attachments */
966         int                    err       = EMAIL_ERROR_NONE;
967         int                    i         = 0;
968
969         if (!input_mail_data || (input_attachment_count && !input_attachment_data_list) || (!input_attachment_count &&input_attachment_data_list) || !output_size)  {
970                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
971                 err = EMAIL_ERROR_INVALID_PARAM;
972                 goto FINISH_OFF;
973         }
974
975         if (input_mail_data->file_path_plain != NULL) {
976                 if (stat(input_mail_data->file_path_plain, &st_buf) < 0)  {
977                         EM_DEBUG_EXCEPTION("input_mail_data->file_path_plain : stat(\"%s\") failed...", input_mail_data->file_path_plain);
978                         err = EMAIL_ERROR_INVALID_MAIL;
979                         goto FINISH_OFF;
980                 }
981
982                 mail_size += st_buf.st_size;
983
984         }
985
986         if (input_mail_data->file_path_html != NULL) {
987                 if (stat(input_mail_data->file_path_html, &st_buf) < 0) {
988                         EM_DEBUG_EXCEPTION("input_mail_data->file_path_html : stat(\"%s\") failed...", input_mail_data->file_path_html);
989                         err = EMAIL_ERROR_INVALID_MAIL;
990                         goto FINISH_OFF;
991                 }
992
993                 mail_size += st_buf.st_size;
994         }
995
996         for(i = 0; i < input_attachment_count; i++)  {
997                 if (stat(input_attachment_data_list[i].attachment_path, &st_buf) < 0)  {
998                         EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", input_attachment_data_list[i].attachment_path);
999                         err = EMAIL_ERROR_INVALID_MAIL;
1000                         goto FINISH_OFF;
1001                 }
1002                 mail_size += st_buf.st_size;
1003         }
1004
1005         *output_size = mail_size;
1006
1007 FINISH_OFF:
1008
1009         EM_DEBUG_FUNC_END("mail_size [%d]", mail_size);
1010         return err;
1011 }
1012
1013
1014 /* parse the Full mailbox Path and Get the Alias Name of the Mailbox */
1015 char *emcore_get_alias_of_mailbox(const char *mailbox_path)
1016 {
1017         EM_DEBUG_FUNC_BEGIN();
1018         EM_IF_NULL_RETURN_VALUE(mailbox_path, NULL);
1019
1020         guint index = 0;
1021         gchar **token_list = NULL;
1022         gchar *mailbox = NULL, *name = NULL;
1023         char *converted_name;
1024
1025
1026         mailbox = g_strdup(mailbox_path);
1027         token_list = g_strsplit_set(mailbox, "/", -1);
1028
1029         if (mailbox)
1030                 g_free(mailbox);
1031
1032         while (token_list[index] != NULL)
1033                 index++;
1034
1035         name = g_strdup(token_list[index - 1]);
1036         if(!name) /* prevent 27459 */
1037                 return NULL;
1038
1039         g_strfreev(token_list);
1040
1041         converted_name = emcore_convert_mutf7_to_utf8(name);
1042
1043         if (name)
1044                 g_free(name);
1045
1046         EM_DEBUG_FUNC_END();
1047         return converted_name;
1048 }
1049
1050
1051 static int emcore_get_first_address(const char *full_address, char **alias, char **address)
1052 {
1053         EM_DEBUG_FUNC_BEGIN();
1054
1055         if (full_address == NULL || alias == NULL || address == NULL){
1056                 EM_DEBUG_EXCEPTION("Invalid Param  :  full_address[%p], alias[%p], address[%p]", full_address, alias, address);
1057                 return false;
1058         }
1059
1060         char *s = NULL;
1061         char *alias_start = NULL;
1062         char *alias_end = NULL;
1063         char *alias_cursor = NULL;
1064         char *address_start = NULL;
1065         char *address_end = NULL;
1066         char *first_address = NULL;
1067
1068         if (full_address){
1069                 s = (char *)strchr((char *)full_address, ';');
1070                 if (s == NULL)
1071                         first_address = strdup(full_address);   /*  only one  address */
1072                 else
1073                         first_address = strndup(full_address, s - full_address);        /*  over two addresses */
1074
1075                 /*  get alias */
1076                 *alias = NULL;
1077                 if ((alias_start = (char *)strchr((char *)first_address, '\"'))){
1078                         alias_start++;
1079                         alias_cursor = alias_start;
1080                         while ((alias_cursor = (char *)strchr((char *)(alias_cursor), '\"'))){
1081                                 alias_end = alias_cursor;
1082                                 alias_cursor++;
1083                                 if (*alias_cursor == 0)
1084                                         break;
1085                         }
1086                         if (alias_end)  {       /*  there is "alias" */
1087                                 *alias = strndup(alias_start, alias_end - alias_start);
1088                                 EM_DEBUG_LOG("alias [%s]", *alias);
1089                         }
1090                 }
1091
1092                 /*  get address */
1093                 *address = NULL;
1094                 if (alias_end == NULL)
1095                         s = first_address;
1096                 else
1097                         s = alias_end+1;
1098                 if ((address_start = (char *)strchr((char  *)s, '<'))){
1099                         address_start++;
1100                         if ((address_end = (char *)strchr((char  *)address_start, '>')))
1101                                 *address = strndup(address_start, address_end - address_start); /*  (alias) <(addr)>  ... */
1102                         else
1103                                 *address = strdup(s);
1104                 }
1105                 else
1106                *address = strdup(s);    /*  (addr) ; ...                 :  no alias */
1107         }
1108
1109         EM_SAFE_FREE(first_address);
1110         EM_DEBUG_FUNC_END();
1111         return true;
1112 }
1113
1114 void emcore_fill_address_information_of_mail_tbl(emstorage_mail_tbl_t *mail_data)
1115 {
1116         EM_DEBUG_FUNC_BEGIN("mail_data [%p]", mail_data);
1117
1118         char *first_alias   = NULL;
1119         char *first_address = NULL;
1120         char *recipient     = NULL;
1121
1122         /*  sender alias & address */
1123         if (emcore_get_first_address(mail_data->full_address_from, &first_alias, &first_address) == true) {
1124                 if (first_alias == NULL) {
1125                         mail_data->alias_sender = EM_SAFE_STRDUP(first_address);
1126                 }
1127                 else {
1128                         mail_data->alias_sender = first_alias;
1129                         first_alias = NULL;
1130                 }
1131                 mail_data->email_address_sender = first_address;
1132                 first_address = NULL;
1133         }
1134
1135         /*  recipient alias & address */
1136         if (mail_data->full_address_to != NULL)
1137                 recipient = mail_data->full_address_to;
1138         else if (mail_data->full_address_cc != NULL)
1139                 recipient = mail_data->full_address_cc;
1140         else if (mail_data->full_address_bcc != NULL)
1141                 recipient = mail_data->full_address_bcc;
1142
1143         if (emcore_get_first_address(recipient, &first_alias, &first_address) == true) {
1144                 if (first_alias == NULL)
1145                         mail_data->alias_recipient = EM_SAFE_STRDUP(first_address);
1146                 else
1147                         mail_data->alias_recipient = first_alias;
1148
1149                 mail_data->email_address_recipient = first_address;
1150         }
1151         EM_DEBUG_FUNC_END();
1152 }
1153
1154
1155 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)
1156 {
1157         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);
1158
1159         int          err = EMAIL_ERROR_NONE;
1160         unsigned int byte_read = 0;
1161         unsigned int byte_written = 0;
1162         int          local_preview_buffer_length = 0;
1163         char        *local_preview_text = NULL;
1164         char        *encoding_type = NULL;
1165         char        *utf8_encoded_string = NULL;
1166         FILE        *fp_html = NULL;
1167         FILE        *fp_plain = NULL;
1168         GError      *glib_error = NULL;
1169         struct stat  st_buf;
1170
1171         if (!output_preview_buffer) {
1172                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1173                 err = EMAIL_ERROR_INVALID_PARAM;
1174                 goto FINISH_OFF;
1175         }
1176
1177         local_preview_buffer_length = input_preview_buffer_length * 2;
1178
1179         if ( input_html_path ) { /*prevent 26249*/
1180                 /*      get preview text from html file */
1181                 if( (err = em_get_encoding_type_from_file_path(input_html_path, &encoding_type)) != EMAIL_ERROR_NONE) {
1182                         EM_DEBUG_EXCEPTION("em_get_encoding_type_from_file_path failed [%s]", err);
1183                         goto FINISH_OFF;
1184                 }
1185
1186                 if (stat(input_html_path, &st_buf) < 0)  {
1187                         EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", input_html_path);
1188                         err = EMAIL_ERROR_INVALID_MAIL;
1189                         goto FINISH_OFF;
1190                 }
1191
1192                 if (!(fp_html = fopen(input_html_path, "r")))   {
1193                         EM_DEBUG_EXCEPTION("fopen failed [%s]", input_html_path);
1194                         err = EMAIL_ERROR_SYSTEM_FAILURE;
1195                         goto FINISH_OFF;
1196                 }
1197
1198                 if (S_ISREG(st_buf.st_mode) && st_buf.st_size == 0) {
1199                         EM_DEBUG_LOG("input_html_file is empty size");
1200                         err = EMAIL_ERROR_EMPTY_FILE;
1201                         goto FINISH_OFF;
1202                 }
1203
1204                 if (!(local_preview_text = (char*)em_malloc(sizeof(char) * (st_buf.st_size + 1)))) {
1205                         EM_DEBUG_EXCEPTION("em_malloc failed");
1206                         err = EMAIL_ERROR_OUT_OF_MEMORY;
1207                         goto FINISH_OFF;
1208                 }
1209
1210                 byte_read = fread(local_preview_text, sizeof(char), st_buf.st_size, fp_html);
1211
1212                 if(byte_read <= 0) { /*prevent 26249*/
1213                         EM_SAFE_FREE(local_preview_text);
1214                         if (ferror(fp_html)) {
1215                                 EM_DEBUG_EXCEPTION("fread failed [%s]", input_html_path);
1216                                 err = EMAIL_ERROR_SYSTEM_FAILURE;
1217                                 goto FINISH_OFF;
1218                         }
1219                 }
1220                 else {
1221                         if ( (err = emcore_strip_HTML(local_preview_text)) != EMAIL_ERROR_NONE) {
1222                                 EM_DEBUG_EXCEPTION("emcore_strip failed");
1223                                 goto FINISH_OFF;
1224                         }
1225                 }
1226         }
1227
1228         if ( !local_preview_text && input_plain_path) { /*prevent 26249*/
1229                 /*  get preview text from plain text file */
1230                 if( (err = em_get_encoding_type_from_file_path(input_plain_path, &encoding_type)) != EMAIL_ERROR_NONE) {
1231                         EM_DEBUG_EXCEPTION("em_get_encoding_type_from_file_path failed [%s]", err);
1232                         goto FINISH_OFF;
1233                 }
1234
1235                 memset(&st_buf, 0, sizeof(struct stat));
1236                 if (stat(input_plain_path, &st_buf) < 0)  {
1237                         EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", input_plain_path);
1238                         err = EMAIL_ERROR_INVALID_MAIL;
1239                         goto FINISH_OFF;
1240                 }
1241
1242                 if (!(fp_plain = fopen(input_plain_path, "r")))  {
1243                         EM_DEBUG_EXCEPTION("fopen failed [%s]", input_plain_path);
1244                         err = EMAIL_ERROR_SYSTEM_FAILURE;
1245                         goto FINISH_OFF;
1246                 }
1247
1248                 if (S_ISREG(st_buf.st_mode) && st_buf.st_size == 0) {
1249                         EM_DEBUG_LOG("input_text_file is empty size");
1250                         err = EMAIL_ERROR_EMPTY_FILE;
1251                         goto FINISH_OFF;
1252                 }
1253
1254                 if (!(local_preview_text = (char*)em_malloc(sizeof(char) * local_preview_buffer_length))) {
1255                         EM_DEBUG_EXCEPTION("em_malloc failed");
1256                         goto FINISH_OFF;
1257                 }
1258
1259                 byte_read = fread(local_preview_text, sizeof(char), local_preview_buffer_length - 1, fp_plain);
1260
1261                 if(byte_read <=0) { /*prevent 26249*/
1262                         EM_SAFE_FREE(local_preview_text);
1263                         err = EMAIL_ERROR_NULL_VALUE;
1264                         if (ferror(fp_plain)) {
1265                                 EM_DEBUG_EXCEPTION("fread failed [%s]", input_plain_path);
1266                                 err = EMAIL_ERROR_SYSTEM_FAILURE;
1267                         }
1268                         goto FINISH_OFF;
1269                 }
1270                 reg_replace(local_preview_text, CR_STRING, " ");
1271                 reg_replace(local_preview_text, LF_STRING, " ");
1272                 reg_replace(local_preview_text, TAB_STRING, " ");
1273         }
1274
1275         if(local_preview_text) {
1276                 em_trim_left(local_preview_text);
1277                 EM_DEBUG_LOG("local_preview_text : [%s]", local_preview_text);
1278                 if(encoding_type && strcasecmp(encoding_type, "UTF-8") != 0) {
1279                         EM_DEBUG_LOG("encoding_type [%s]", encoding_type);
1280                         utf8_encoded_string = (char*)g_convert (local_preview_text, -1, "UTF-8", encoding_type, &byte_read, &byte_written, &glib_error);
1281
1282                         if(utf8_encoded_string == NULL) {
1283                                 EM_DEBUG_EXCEPTION("g_convert failed : byte_read[%d], EM_SAFE_STRLEN : [%d]", byte_read, EM_SAFE_STRLEN(local_preview_text));
1284                                 EM_DEBUG_LOG("Error is G_CONVERT_ERROR_ILLEGAL_SEQUENCE");
1285
1286                                 if (!g_error_matches (glib_error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE)) {
1287                                         EM_DEBUG_EXCEPTION("g_convert failed");
1288                                         goto FINISH_OFF;
1289                                 }
1290
1291                                 EM_DEBUG_LOG("Extract the preview text, again");
1292
1293                                 utf8_encoded_string = (char *)g_convert(local_preview_text, byte_read, "UTF-8", encoding_type, &byte_read, &byte_written, &glib_error);
1294                                 if (utf8_encoded_string == NULL) {
1295                                         EM_DEBUG_EXCEPTION("g_convert failed : byte_read[%d]", byte_read);
1296                                         goto FINISH_OFF;
1297                                 }
1298
1299                         }
1300                         EM_SAFE_FREE(local_preview_text);
1301                         local_preview_text = utf8_encoded_string;
1302                 }
1303         }
1304
1305 FINISH_OFF:
1306
1307         if (local_preview_text != NULL)
1308                 *output_preview_buffer = EM_SAFE_STRDUP(local_preview_text);
1309
1310         EM_SAFE_FREE(local_preview_text);
1311         EM_SAFE_FREE(encoding_type);
1312
1313         if (fp_html != NULL)
1314                 fclose(fp_html);
1315
1316         if (fp_plain != NULL)
1317                 fclose(fp_plain);
1318
1319         EM_DEBUG_FUNC_END("err [%d]", err);
1320         return err;
1321 }
1322
1323 INTERNAL_FUNC int emcore_add_transaction_info(int mail_id, int handle , int *err_code)
1324 {
1325         EM_DEBUG_FUNC_BEGIN("mail_id[%d], handle[%d]", mail_id, handle);
1326
1327         int ret = false;
1328         int err = EMAIL_ERROR_NONE ;
1329         em_transaction_info_type_t  *pTransinfo = NULL ;
1330         em_transaction_info_type_t      *pTemp = NULL;
1331
1332         EM_DEBUG_LOG("g_transaction_info_list[%p]", g_transaction_info_list);
1333         pTransinfo = g_transaction_info_list ;
1334
1335         if (!(pTemp = em_malloc(sizeof(em_transaction_info_type_t))))  {
1336                 EM_DEBUG_EXCEPTION("malloc failed...");
1337                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1338                 goto FINISH_OFF;
1339         }
1340         pTemp->mail_id = mail_id ;
1341         pTemp->handle = handle;
1342
1343         if (!pTransinfo) {
1344                 pTransinfo = pTemp ;
1345                 g_transaction_info_list = pTransinfo ;
1346         }
1347         else {
1348                 while (pTransinfo->next)
1349                         pTransinfo = pTransinfo->next;
1350                 pTransinfo->next = pTemp;
1351         }
1352         ret = true ;
1353
1354 FINISH_OFF:
1355
1356         if (err_code)
1357                 *err_code = err;
1358         EM_DEBUG_FUNC_END("g_transaction_info_list[%p]", g_transaction_info_list);
1359         return ret;
1360 }
1361
1362 INTERNAL_FUNC int emcore_get_handle_by_mailId_from_transaction_info(int mail_id, int *pHandle)
1363 {
1364         EM_DEBUG_FUNC_BEGIN("mail_id[%d], handle[%p]", mail_id, pHandle);
1365
1366         int ret = false;
1367         em_transaction_info_type_t  *pTransinfo = NULL ;
1368
1369         if (g_transaction_info_list == NULL) {
1370                 EM_DEBUG_EXCEPTION("g_transaction_info_list NULL");
1371                 return false;
1372         }
1373         pTransinfo = g_transaction_info_list;
1374
1375         do {
1376                 EM_DEBUG_LOG("pTransinfo->mail_id[%d]", pTransinfo->mail_id);
1377                 if (pTransinfo->mail_id == mail_id) {
1378                         *pHandle = pTransinfo->handle;
1379                         ret = true;
1380                         EM_DEBUG_LOG("*pHandle[%d]", *pHandle);
1381                         break;
1382                 }
1383                 else
1384                         pTransinfo = pTransinfo->next ;
1385         }while (pTransinfo);
1386         EM_DEBUG_FUNC_END();
1387         return ret;
1388 }
1389
1390 INTERNAL_FUNC int emcore_delete_transaction_info_by_mailId(int mail_id )
1391 {
1392         EM_DEBUG_FUNC_BEGIN("mail_id[%d]", mail_id);
1393
1394         em_transaction_info_type_t  *pTransinfo ;
1395         em_transaction_info_type_t *pTemp = NULL;
1396
1397         if (g_transaction_info_list == NULL) {
1398                 EM_DEBUG_EXCEPTION("g_transaction_info_list NULL");
1399                 return false;
1400         }
1401         pTransinfo = g_transaction_info_list;
1402
1403         EM_DEBUG_LOG("pTransinfo[%p]", pTransinfo);
1404
1405         do {
1406                 EM_DEBUG_LOG("pTransinfo->mail_id[%d]", pTransinfo->mail_id);
1407                 if (pTransinfo->mail_id == mail_id) {
1408                         pTemp = pTransinfo->next ;
1409                         if (!pTemp) {
1410                                 EM_SAFE_FREE(pTransinfo) ;
1411                                 g_transaction_info_list = NULL;
1412                         }
1413                         else {
1414                                 pTransinfo->mail_id = pTransinfo->next->mail_id;
1415                                 pTransinfo->handle = pTransinfo->next->handle ;
1416                                 pTransinfo->next = pTransinfo->next->next;
1417
1418                                 EM_SAFE_FREE(pTemp);
1419                         }
1420                         break;
1421                 }
1422                 else {
1423                         pTransinfo = pTransinfo->next ;
1424                 }
1425
1426         }while (pTransinfo);
1427         EM_DEBUG_FUNC_END();
1428         return true;
1429 }
1430
1431
1432 #include <regex.h>
1433
1434 int reg_replace (char *input_source_text, char *input_old_pattern_string, char *input_new_string)
1435 {
1436         EM_DEBUG_FUNC_BEGIN("input_source_text [%p], input_old_pattern_string [%p], input_new_string [%p]", input_source_text, input_old_pattern_string, input_new_string);
1437         int         error_code = EMAIL_ERROR_NONE;
1438         char       *pos = NULL;
1439         int         so, n, nmatch, source_text_length, n_count = 1;
1440         regmatch_t *pmatch = NULL;
1441         regex_t     reg_pattern;
1442
1443         if(!input_source_text || !input_old_pattern_string || !input_new_string) {
1444                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1445                 error_code = EMAIL_ERROR_INVALID_PARAM;
1446                 goto FINISH_OFF;
1447         }
1448
1449         source_text_length = EM_SAFE_STRLEN(input_source_text);
1450
1451         regcomp(&reg_pattern, input_old_pattern_string, REG_ICASE);
1452
1453         nmatch = reg_pattern.re_nsub + 1;
1454
1455         EM_DEBUG_LOG("nmatch [%d]", nmatch);
1456
1457         if(nmatch < 1) {
1458                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_DATA");
1459                 error_code = EMAIL_ERROR_INVALID_DATA;
1460                 goto FINISH_OFF;
1461         }
1462
1463         pmatch = (regmatch_t*)em_malloc(sizeof(regmatch_t) * nmatch);
1464
1465         if(pmatch == NULL) {
1466                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_OUT_OF_MEMORY");
1467                 error_code = EMAIL_ERROR_OUT_OF_MEMORY;
1468                 goto FINISH_OFF;
1469         }
1470
1471         for (pos = input_new_string; *pos ; pos++) {
1472                 if (*pos == '\\' && *(pos + 1) > '0' && *(pos + 1) <= '9') {
1473
1474                         so = pmatch[*(pos + 1) - 48].rm_so;
1475                         n  = pmatch[*(pos + 1) - 48].rm_eo - so;
1476
1477                         EM_DEBUG_LOG("so [%d], n [%d]", so, n);
1478
1479                         if (so < 0 || EM_SAFE_STRLEN (input_new_string) + n - 1 > source_text_length)
1480                                 break;
1481
1482                         memmove (pos + n, pos + 2, EM_SAFE_STRLEN (pos) - 1);
1483                         memmove (pos, input_source_text + so, n);
1484                         pos = pos + n - 2;
1485                 }
1486         }
1487
1488         for (pos = input_source_text; !regexec (&reg_pattern, pos, 1, pmatch, 0);) {
1489                 n = pmatch[0].rm_eo - pmatch[0].rm_so;
1490                 pos += pmatch[0].rm_so;
1491
1492                 memmove (pos + EM_SAFE_STRLEN (input_new_string), pos + n, EM_SAFE_STRLEN (pos) - n + 1);
1493                 memmove (pos, input_new_string, EM_SAFE_STRLEN (input_new_string));
1494                 pos += EM_SAFE_STRLEN (input_new_string);
1495                 n_count++;
1496         }
1497
1498 FINISH_OFF:
1499
1500         EM_SAFE_FREE(pmatch);
1501         regfree (&reg_pattern);
1502
1503         EM_DEBUG_FUNC_END("error_code [%d]", error_code);
1504         return error_code;
1505 }
1506
1507
1508 int emcore_strip_HTML(char *source_string)
1509 {
1510         EM_DEBUG_FUNC_BEGIN("source_string [%p]", source_string);
1511
1512         int result = EMAIL_ERROR_NONE;
1513
1514         reg_replace(source_string, CR_STRING, " ");
1515         reg_replace(source_string, LF_STRING, " ");
1516         reg_replace(source_string, "<head[^>]*>", "<head>"); /*  "<()*head([^>])*>", "<head>" */
1517         reg_replace(source_string, "<*/head>", "</head>");  /*  "(<()*(/)()*head()*>)", "</head>" */
1518         reg_replace(source_string, "<head>.*</head>", ""); /*  "(<head>).*(</head>)", "" */
1519         reg_replace(source_string, "<style[^>]*>.*</style>", "");
1520
1521         reg_replace(source_string, "<*/p>", " ");
1522         reg_replace(source_string, "<br>", " ");
1523
1524         /*   "<[^>]*>", " */
1525         reg_replace(source_string, "<[^>]*>", "");
1526
1527
1528         /*   "&bull;", " *  */
1529         /* reg_replace(source_string, "&bull;", " * "); */
1530
1531         /*   "&lsaquo;", "< */
1532         /* reg_replace(source_string, "&lsaquo;", "<"); */
1533
1534         /*   "&rsaquo;", "> */
1535         /* reg_replace(source_string, "&rsaquo;", ">"); */
1536
1537         /*   "&trade;", "(tm) */
1538         /* reg_replace(source_string, "&trade;", "(tm)"); */
1539
1540         /*   "&frasl;", "/ */
1541         /* reg_replace(source_string, "&frasl;", "/"); */
1542
1543         /*  "&lt;", "< */
1544         reg_replace(source_string, "&lt;", "<");
1545
1546         /*  "&gt;", "> */
1547         reg_replace(source_string, "&gt;", ">");
1548
1549         /*  "&copy;", "(c) */
1550         /* reg_replace(source_string, "&copy;", "(c)"); */
1551
1552         /* "&quot;", "\' */
1553         reg_replace(source_string, "&quot;", "\'");
1554
1555         /*  "&nbsp;", " */
1556         reg_replace(source_string, "&nbsp;", " ");
1557
1558         reg_replace(source_string, "  ", " ");
1559
1560         EM_DEBUG_FUNC_END();
1561
1562         return result;
1563 }
1564
1565 /*  emcore_send_noti_for_new_mail is not used currently because DBUS could not send very long message.*/
1566 /*  But I think it can be used to notify incomming new mail for replacing NOTI_MAIL_ADD with some modification(uid should be replaced with mail_id).  */
1567 /*  This notification is including addtional information comparing NOTI_MAIL_ADD. */
1568 /*  By this change, email application will be able to add email item without additional DB query.  */
1569 /*  It might improve performance of sync email.  */
1570 /*  kyuho.jo 2010-09-07 */
1571
1572 INTERNAL_FUNC int emcore_send_noti_for_new_mail(int account_id, char *mailbox_name, char *subject, char *from, char *uid, char *datetime)
1573 {
1574         EM_DEBUG_FUNC_BEGIN("mailbox_name(%s) subject(%s), from(%s), uid(%s), datetime(%s)", mailbox_name, subject, from, uid, datetime);
1575         int error_code = EMAIL_ERROR_NONE;
1576         int param_length = 0;
1577         char *param_string = NULL;
1578
1579         if (mailbox_name == NULL || subject == NULL || from == NULL || uid == NULL || datetime == NULL) {
1580                 error_code = EMAIL_ERROR_INVALID_PARAM;
1581                 EM_DEBUG_EXCEPTION("Invalid parameter, mailbox_name(%p), subject(%p), from(%p), uid(%p), datetime(%p)", mailbox_name, subject, from, uid, datetime);
1582                 goto FINISH_OFF;
1583         }
1584
1585         param_length = strlen(mailbox_name) + strlen(subject) + strlen(from) + strlen(uid) + strlen(datetime) + 5; /*prevent 34358*/
1586
1587         param_string = em_malloc(param_length);
1588         if (param_string == NULL) {
1589                 error_code = EMAIL_ERROR_OUT_OF_MEMORY;
1590                 EM_DEBUG_EXCEPTION("Memory allocation for 'param_string' is failed");
1591                 goto FINISH_OFF;
1592         }
1593
1594         memset(param_string, 0x00, param_length);
1595
1596         SNPRINTF(param_string, param_length, "%s%c%s%c%s%c%s%c%s", mailbox_name, 0x01, subject, 0x01, from, 0x01, uid, 0x01, datetime);
1597
1598         if (emcore_notify_network_event(NOTI_DOWNLOAD_NEW_MAIL, account_id, param_string, 0, 0) == 0) { /*  failed */
1599                 error_code = EMAIL_ERROR_UNKNOWN;
1600                 EM_DEBUG_EXCEPTION("emcore_notify_network_event is failed");
1601                 goto FINISH_OFF;
1602         }
1603
1604 FINISH_OFF:
1605
1606         EM_SAFE_FREE(param_string);
1607         EM_DEBUG_FUNC_END();
1608         return error_code;
1609 }
1610
1611 #define MAX_TITLE_LENGTH 1024
1612 int emcore_update_notification_for_unread_mail(int account_id)
1613 {
1614         EM_DEBUG_FUNC_BEGIN("acccount_id[%d]", account_id);
1615         int ret = false;
1616         int i, account_count = 0;
1617         int err = EMAIL_ERROR_NONE;
1618         emstorage_account_tbl_t *p_account_tbl = NULL;
1619
1620         if (!emstorage_get_account_list(&account_count, &p_account_tbl, true, false, &err)) {
1621                 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [%d]", err);
1622                 goto FINISH_OFF;
1623         }
1624
1625         for (i = 0; i < account_count ; i++) {
1626                 if (emcore_add_notification(p_account_tbl[i].account_id, 0, EMAIL_ACTION_NUM) != EMAIL_ERROR_NONE) {
1627                         EM_DEBUG_EXCEPTION("emcore_add_notification failed");
1628                         continue;
1629                 }
1630         }
1631
1632         ret = true;
1633
1634 FINISH_OFF:
1635
1636         if (p_account_tbl)
1637                 emstorage_free_account(&p_account_tbl, account_count, NULL);
1638
1639         EM_DEBUG_FUNC_END();
1640         return ret;
1641 }
1642
1643 INTERNAL_FUNC int emcore_finalize_sync(int account_id, int *error)
1644 {
1645         EM_DEBUG_FUNC_BEGIN("account_id [%d], error [%p]", account_id, error);
1646         int err = EMAIL_ERROR_NONE, ret = true, result_sync_status = SYNC_STATUS_FINISHED;
1647
1648         if ((err = emcore_update_sync_status_of_account(account_id, SET_TYPE_MINUS, SYNC_STATUS_SYNCING)) != EMAIL_ERROR_NONE)
1649                 EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
1650
1651         if (!emstorage_get_sync_status_of_account(ALL_ACCOUNT, &result_sync_status, &err))
1652                 EM_DEBUG_EXCEPTION("emstorage_get_sync_status_of_account failed [%d]", err);
1653
1654         if (result_sync_status == SYNC_STATUS_HAVE_NEW_MAILS) {
1655                 if (!emcore_update_notification_for_unread_mail(ALL_ACCOUNT))
1656                         EM_DEBUG_EXCEPTION("emcore_update_notification_for_unread_mail failed");
1657
1658                 if (!emcore_display_unread_in_badge())
1659                         EM_DEBUG_EXCEPTION("emcore_display_unread_in_badge failed");
1660
1661                 emcore_start_alert();
1662
1663                 if ((err = emcore_update_sync_status_of_account(account_id, SET_TYPE_MINUS, SYNC_STATUS_HAVE_NEW_MAILS)) != EMAIL_ERROR_NONE)
1664                         EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
1665         }
1666
1667         EM_DEBUG_FUNC_END();
1668         return ret;
1669 }
1670
1671 INTERNAL_FUNC int emcore_clear_all_notifications()
1672 {
1673         int account_count = 0, i;
1674         emstorage_account_tbl_t *account_list;
1675         int error_code = EMAIL_ERROR_NONE;
1676
1677         if(!emstorage_get_account_list(&account_count, &account_list, true, false, &error_code)) {
1678                 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed");
1679                 goto FINISH_OFF;
1680         }
1681
1682         for(i = 0; i < account_count; i++) {
1683                 emcore_delete_notification_by_account(account_list[i].account_id);
1684         }
1685
1686 FINISH_OFF:
1687         if(account_count) {
1688                 emstorage_free_account(&account_list, account_count, NULL);
1689         }
1690
1691         EM_DEBUG_FUNC_END("return[%d]", error_code);
1692         return error_code;
1693 }
1694
1695 #define EAS_EXECUTABLE_PATH "/usr/bin/eas-engine"
1696
1697 INTERNAL_FUNC int emcore_delete_notification_by_account(int account_id)
1698 {
1699         EM_DEBUG_FUNC_BEGIN("account_id [%d]", account_id);
1700         int error_code = EMAIL_ERROR_NONE;
1701         int private_id = 0;
1702         char vconf_private_id[MAX_PATH] = {0, };
1703
1704         SNPRINTF(vconf_private_id, sizeof(vconf_private_id), "%s/%d", VCONF_KEY_NOTI_PRIVATE_ID, account_id);
1705         if (vconf_get_int(vconf_private_id, &private_id) != 0) {
1706                 EM_DEBUG_EXCEPTION("vconf_get_int failed");
1707         }
1708         EM_DEBUG_FUNC_END();
1709         return error_code;
1710 }
1711
1712 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
1713
1714 /**
1715  * @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)
1716  * Prepare a linked list of uid ranges with each node having a uid_range and lowest and highest uid in it.
1717  *
1718  *@author                                       h.gahlaut@samsung.com
1719  * @param[in] id_set                    Specifies the array of mail_id and corresponding server_mail_id sorted by server_mail_ids in ascending order
1720  * @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
1721  * @param[in] range_len         Specifies the maximum length of string of range allowed.
1722  * @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
1723  * @param[out] err_code         Returns the error code.
1724  * @remarks                                     An example of a uid_range formed is 2:6,8,10,14:15,89,
1725  *                                                      While using it the caller should remove the ending, (comma)
1726  * @return This function returns true on success or false on failure.
1727  */
1728
1729 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)
1730 {
1731         EM_DEBUG_FUNC_BEGIN();
1732
1733         int ret = false;
1734         int error = EMAIL_ERROR_NONE;
1735
1736         if (NULL == id_set || id_set_count  <= 0 || NULL == uid_range_set) {
1737                 EM_DEBUG_EXCEPTION(" Invalid Parameter id_set[%p] id_set_count[%d] uid_range_set[%p]", id_set, id_set_count, uid_range_set);
1738                 error = EMAIL_ERROR_INVALID_PARAM;
1739                 goto FINISH_OFF;
1740         }
1741
1742         int i = 0;
1743         unsigned long current_uid = 0;
1744         unsigned long first_uid = 0;
1745         unsigned long last_uid = 0;
1746         const int max_subset_string_size = MAX_SUBSET_STRING_SIZE;
1747         char subset_string[MAX_SUBSET_STRING_SIZE] = {0,};
1748         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*/
1749
1750         if (range_len < (max_subset_string_size + 1))           /* 1 for ending NULL character */ {
1751                 EM_DEBUG_EXCEPTION(" Invalid Parameter range_len[%d]", range_len);
1752                 error = EMAIL_ERROR_INVALID_PARAM;
1753                 goto FINISH_OFF;
1754         }
1755
1756         EM_DEBUG_LOG("id set count[%d] range_len[%d]", id_set_count, range_len);
1757
1758         do {
1759                 first_uid = last_uid = current_uid = id_set[i].server_mail_id;
1760                 /* Start subset string by putting first server mail id in it from id_set*/
1761                 memset(subset_string, 0x00, max_subset_string_size);
1762                 SNPRINTF(subset_string, max_subset_string_size, "%lu", first_uid);
1763                 ++i;
1764
1765                 /* Check if only one server mail id was left in id_set */
1766                 if (i >= id_set_count) {
1767                         /* No more server mail id left in id_set */
1768                         if (false == emcore_append_subset_string_to_uid_range(subset_string, &current_node, uid_range_set, range_len, first_uid, last_uid)) {
1769                                 EM_DEBUG_EXCEPTION("emcore_append_subset_string_to_uid_range failed");
1770                                 goto FINISH_OFF;
1771                         }
1772                         break;
1773                 }
1774                 else {
1775                         /* 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 */
1776                         do {
1777                                 current_uid = id_set[i].server_mail_id;
1778                                 if (current_uid == (last_uid + 1)) {
1779                                         last_uid = current_uid;
1780                                         ++i;
1781                                 }
1782                                 else {
1783                                         memset(subset_string, 0x00, max_subset_string_size);
1784                                         if (first_uid != last_uid)      /* Form subset string by first_uid:last_uid */
1785                                                 SNPRINTF(subset_string, max_subset_string_size, "%lu:%lu", first_uid, last_uid);
1786                                         else    /* Form subset string by first_uid */
1787                                                 SNPRINTF(subset_string, max_subset_string_size, "%lu", first_uid);
1788
1789                                         if (false == emcore_append_subset_string_to_uid_range(subset_string, &current_node, uid_range_set, range_len, first_uid, last_uid)) {
1790                                                 EM_DEBUG_EXCEPTION("emcore_append_subset_string_to_uid_range failed");
1791                                                 goto FINISH_OFF;
1792                                         }
1793                                         /* To Start formation of new subset string break out of inner loop */
1794                                         break;
1795                                 }
1796
1797                         } while (i < id_set_count);
1798
1799                         /* Flow comes here in two cases :
1800                         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
1801                         2. due to break statement */
1802
1803                         if (last_uid == current_uid) {
1804                                 /* Case 1 */
1805
1806                                 memset(subset_string, 0x00, max_subset_string_size);
1807                                 SNPRINTF(subset_string, max_subset_string_size, "%lu:%lu", first_uid, last_uid);
1808
1809                                 if (false == emcore_append_subset_string_to_uid_range(subset_string, &current_node, uid_range_set, range_len, first_uid, last_uid)) {
1810                                         EM_DEBUG_EXCEPTION("emcore_append_subset_string_to_uid_range failed");
1811                                         goto FINISH_OFF;
1812                                 }
1813                         }
1814                         else {
1815                                 /* Case 2: Do Nothing */
1816                         }
1817
1818                 }
1819         } while (i < id_set_count);
1820
1821         ret = true;
1822
1823 FINISH_OFF:
1824         if (NULL != err_code)
1825                 *err_code = error;
1826         EM_DEBUG_FUNC_END();
1827         return ret;
1828
1829 }
1830
1831 /**
1832  * @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)
1833  * 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
1834  * and stores the subset_string in its uid_range. Also sets the lowest and highest uids for the corresponsing uid_range
1835  *
1836  * @author                                      h.gahlaut@samsung.com
1837  * @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.
1838  * @param[in] range_len         Specifies the maximum length of range string allowed.
1839  * @param[in] luid                      Specifies the lowest uid in subset string
1840  * @param[in] huid                      Specifies the highest uid in subset string
1841  * @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
1842  * @param[out] err_code         Returns the error code.
1843  * @remarks
1844  * @return This function returns true on success or false on failure.
1845  */
1846
1847 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)
1848 {
1849         EM_DEBUG_FUNC_BEGIN();
1850         email_uid_range_set *current_node = NULL;
1851
1852         if (NULL == (*uid_range_set)) {
1853                 /*This happens only once when list  creation starts. Head Node is allocated */
1854                 current_node = (email_uid_range_set *)em_malloc(sizeof(email_uid_range_set));
1855                 if (NULL == current_node) {
1856                         EM_DEBUG_EXCEPTION("em_malloc failed");
1857                         return false;
1858                 }
1859
1860                 current_node->uid_range = (char *)em_malloc(range_len);
1861
1862                 if (NULL == current_node->uid_range) {
1863                         EM_DEBUG_EXCEPTION("em_malloc failed");
1864                         EM_SAFE_FREE(current_node);
1865                         return false;
1866                 }
1867
1868                 SNPRINTF(current_node->uid_range, range_len, "%s,", subset_string);
1869
1870                 current_node->lowest_uid = luid;
1871                 current_node->highest_uid = huid;
1872                 (*uid_range_set) = current_node;
1873
1874                 (*current_node_adr) = current_node;
1875
1876         }
1877         else {
1878                 /* Apart from first call to this function flow will always come here */
1879                 current_node = (*current_node_adr);
1880                 int len_sub_string = EM_SAFE_STRLEN(subset_string);
1881                 int space_left_in_buffer = range_len - EM_SAFE_STRLEN(current_node->uid_range);
1882
1883                 if ((len_sub_string + 1 + 1) <= space_left_in_buffer)   /* 1 for comma + 1 for ending null character */ {
1884                         SNPRINTF(current_node->uid_range + EM_SAFE_STRLEN(current_node->uid_range), space_left_in_buffer, "%s,", subset_string);
1885                         current_node->highest_uid = huid;
1886                 }
1887                 else {
1888                         /* No more space left in uid_range string.If continued on it, it will exceeded max size of range_len */
1889                         /* Allocate new node in Uid Range set */
1890                         email_uid_range_set *new_node = NULL;
1891
1892                         new_node = (email_uid_range_set *)em_malloc(sizeof(email_uid_range_set));
1893
1894                         if (NULL == new_node) {
1895                                 EM_DEBUG_EXCEPTION("em_malloc failed");
1896                                 return false;
1897                         }
1898
1899                         /* Allocate uid_range of new node */
1900
1901                         new_node->uid_range =  (char *)em_malloc(range_len);
1902
1903                         if (NULL == new_node->uid_range) {
1904                                 EM_DEBUG_EXCEPTION("em_malloc failed");
1905                                 EM_SAFE_FREE(new_node);
1906                                 return false;
1907                         }
1908
1909                         SNPRINTF(new_node->uid_range, range_len, "%s, ", subset_string);
1910
1911                         new_node->lowest_uid = luid;
1912                         new_node->highest_uid = huid;
1913
1914                         current_node->next = new_node;
1915
1916                         (*current_node_adr) = new_node;
1917                 }
1918         }
1919         EM_DEBUG_FUNC_END();
1920         return true;
1921 }
1922
1923 /**
1924  * void emcore_free_uid_range_set(email_uid_range_set **uid_range_head)
1925  * Frees the linked list of uid ranges
1926  *
1927  * @author                                      h.gahlaut@samsung.com
1928  * @param[in] uid_range_head    Head pointer of linked list of uid ranges
1929  * @remarks
1930  * @return This function does not return anything.
1931  */
1932
1933 INTERNAL_FUNC
1934 void emcore_free_uid_range_set(email_uid_range_set **uid_range_set)
1935 {
1936         EM_DEBUG_FUNC_BEGIN();
1937
1938         email_uid_range_set *current_node = NULL;
1939         email_uid_range_set *uid_range_head = NULL;
1940
1941         current_node = uid_range_head = (*uid_range_set);       /* Make the current node and head ptr point to starting of  uid_range_set */
1942
1943         while (current_node) {
1944                 uid_range_head = current_node->next;            /* Move the head ptr to next node*/
1945
1946                 EM_SAFE_FREE(current_node->uid_range);
1947                 EM_SAFE_FREE(current_node);                             /* Free the current node */
1948
1949                 current_node = uid_range_head;                  /* Make the current node point to head ptr */
1950         }
1951
1952         (*uid_range_set) = NULL;
1953         EM_DEBUG_FUNC_END();
1954 }
1955
1956
1957 /**
1958  * @fn emcore_form_comma_separated_strings(int numbers[], int num_count, int max_string_len, char *** strings, int *string_count, int *err_code)
1959  * Forms comma separated strings of a give max_string_len from an array of numbers
1960  *
1961  * @author                                      h.gahlaut@samsung.com
1962  * @param[in] numbers                   Specifies the array of numbers to be converted into comma separated strings.
1963  * @param[in] num_count         Specifies the count of numbers in numbers array.
1964  * @param[in] max_string_len    Specifies the maximum length of comma separated strings that are to be formed.
1965  * @param[out] strings                  Returns the base address of a double dimension array which stores the strings.
1966  * @param[out] string_count             Returns the number of strings formed.
1967  * @param[out] err_code         Returns the error code.
1968  * @remarks                                     If Input to the function is five numbers like 2755 2754 2748 2749 2750 and a given max_string_len is 20.
1969  *                                                      Then this function will form two comma separated strings as follows -
1970  *                                                      "2755, 2754, 2748"
1971  *                                                      "2749, 2750"
1972  * @return This function returns true on success or false on failure.
1973  */
1974
1975 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)
1976 {
1977         EM_DEBUG_FUNC_BEGIN();
1978
1979         int error = EMAIL_ERROR_NONE;
1980         int ret = false;
1981
1982         char **string_list = NULL;
1983         int num_of_strings = 0;
1984         int i = 0;
1985         int j =0;
1986         char num[MAX_INTEGER_LENGTH + 1] = {0, };
1987         int num_len = 0;
1988         int space_in_buffer = 0;
1989         int len_of_string_formed = 0;
1990
1991         if (NULL == numbers || num_count <= 0 || \
1992                 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.  */ {
1993                 EM_DEBUG_EXCEPTION("Invalid Parameter numbers[%p] num_count [%d] max_string_len [%d] strings [%p] string_count[%p]", \
1994                         numbers, num_count, max_string_len, strings, string_count);
1995                 error = EMAIL_ERROR_INVALID_PARAM;
1996                 goto FINISH_OFF;
1997         }
1998
1999         EM_DEBUG_LOG("num_count [%d] max_string_len [%d]", num_count, max_string_len);
2000
2001         string_list = em_malloc(sizeof(char *));
2002
2003         if (NULL == string_list) {
2004                 EM_DEBUG_EXCEPTION("em_malloc failed ");
2005                 goto FINISH_OFF;
2006         }
2007
2008         string_list[num_of_strings] = em_malloc(max_string_len);
2009
2010         if (NULL == string_list[num_of_strings]) {
2011                 EM_DEBUG_EXCEPTION("em_malloc failed ");
2012                 goto FINISH_OFF;
2013         }
2014
2015         ++num_of_strings;
2016         space_in_buffer = max_string_len;
2017
2018         for (j = 0; j < num_count;++j) {
2019                 memset(num, 0x00, MAX_INTEGER_LENGTH + 1);
2020                 SNPRINTF(num, MAX_INTEGER_LENGTH + 1, "%d", numbers[j]);
2021
2022                 num_len = EM_SAFE_STRLEN(num);
2023
2024                 len_of_string_formed = EM_SAFE_STRLEN(string_list[num_of_strings - 1]);
2025
2026                 space_in_buffer = max_string_len - len_of_string_formed ;
2027
2028                 if (space_in_buffer >= (num_len+1+1))                   /*  1 for comma and 1 for ending NULL */ {
2029                         SNPRINTF(string_list[num_of_strings - 1] + len_of_string_formed, max_string_len, "%d,", numbers[j]);
2030                 }
2031                 else {  /*  Removing comma at end of string  */
2032                         string_list[num_of_strings - 1][len_of_string_formed-1] = '\0';
2033                         char **temp = NULL;
2034                         temp = (char **)realloc(string_list, sizeof(char *) * (num_of_strings + 1));    /*  Allocate new buffer to store a pointer to a new string */
2035
2036                         if (NULL == temp) {
2037                                 EM_DEBUG_EXCEPTION("realloc failed");
2038                                 goto FINISH_OFF;
2039                         }
2040
2041                         memset(temp + num_of_strings, 0X00, sizeof(char *));
2042
2043                         string_list = temp;
2044                         temp = NULL;
2045                         string_list[num_of_strings] = em_malloc(max_string_len);/*  Allocate new buffer to store the string */
2046
2047                         if (NULL == string_list[num_of_strings]) {
2048                                 EM_DEBUG_EXCEPTION(" em_malloc failed ");
2049                                 goto FINISH_OFF;
2050                         }
2051                         ++num_of_strings;
2052                         SNPRINTF(string_list[num_of_strings - 1] , max_string_len, "%d,", numbers[j]);/*  Start making new string */
2053                 }
2054         }
2055
2056         /*  Removing comma at end of string  */
2057         len_of_string_formed = EM_SAFE_STRLEN(string_list[num_of_strings - 1]);
2058         string_list[num_of_strings - 1][len_of_string_formed-1] = '\0';
2059         ret = true;
2060
2061 FINISH_OFF:
2062
2063         if (false == ret)
2064                 emcore_free_comma_separated_strings(&string_list, &num_of_strings);
2065
2066         if (true == ret) {
2067                 for (i = 0; i < num_of_strings;++i)
2068                         EM_DEBUG_LOG("%s", string_list[i]);
2069                 *strings = string_list;
2070                 *string_count = num_of_strings;
2071         }
2072
2073
2074         if (NULL != err_code)
2075                 *err_code = error;
2076
2077         EM_DEBUG_FUNC_END("ret [%d]", ret);
2078         return ret;
2079 }
2080 /**
2081  * @fn emcore_free_comma_separated_strings(char *** string_list, int *string_count)
2082  * Frees the double dimensional array of strings.
2083  *
2084  * @author                                      h.gahlaut@samsung.com
2085  * @param[in] uid_range_head    Address of base address of double dimensional array of strings.
2086  * @param[in] string_count              Address of variable holding the count of strings.
2087  * @remarks
2088  * @return This function does not return anything.
2089  */
2090 INTERNAL_FUNC void emcore_free_comma_separated_strings(char *** string_list, int *string_count)
2091 {
2092         EM_DEBUG_FUNC_BEGIN();
2093         int i = 0;
2094         char **str_list = NULL;
2095         int count = 0;
2096
2097         if (NULL != string_list) {
2098                 str_list = *string_list;
2099
2100                 if (0 != *string_count) {
2101                         count = *string_count;
2102                         for (i = 0; i < count; ++i)
2103                                 EM_SAFE_FREE(str_list[i]);
2104                 }
2105
2106                 EM_SAFE_FREE(str_list);
2107                 *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 */
2108                 *string_count = 0;
2109         }
2110         EM_DEBUG_FUNC_END();
2111 }
2112
2113
2114 #endif
2115
2116
2117
2118
2119 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)
2120 {
2121         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);
2122         int ret = false, err = EMAIL_ERROR_NONE;
2123         char *extcheck = NULL;
2124         char attachment_file_name[MAX_PATH + 1] = { 0, };
2125
2126         if (!source_file_name || !result_file_name) {
2127                 EM_DEBUG_EXCEPTION("Invalid Parameter");
2128                 err  = EMAIL_ERROR_INVALID_PARAM;
2129                 goto FINISH_OFF;
2130         }
2131
2132         strncpy(attachment_file_name, source_file_name, MAX_PATH);
2133         extcheck = strchr(attachment_file_name, '.');
2134
2135         if (extcheck)
2136                 EM_DEBUG_LOG("Extension Exist in the Attachment [%s] ", extcheck);
2137         else  { /* No extension attached, So add the Extension based on the subtype */
2138                 if (sub_type) {
2139                         strcat(attachment_file_name, ".");
2140                         strcat(attachment_file_name, sub_type);
2141                         EM_DEBUG_LOG("attachment_file_name with extension[%s] ", attachment_file_name);
2142                 }
2143                 else
2144                         EM_DEBUG_LOG("UnKnown Extesnsion");
2145
2146         }
2147         memset(result_file_name, 0 , result_file_name_buffer_length);
2148         EM_SAFE_STRNCPY(result_file_name, attachment_file_name, result_file_name_buffer_length - 1);
2149         EM_DEBUG_LOG("*result_file_name[%s]", result_file_name);
2150         ret = true;
2151
2152 FINISH_OFF:
2153         if (err_code)
2154                 *err_code = err;
2155         EM_DEBUG_FUNC_END();
2156         return ret;
2157 }
2158
2159 #ifdef __FEATURE_LOCAL_ACTIVITY__
2160 INTERNAL_FUNC int emcore_add_activity(emstorage_activity_tbl_t *new_activity, int *err_code)
2161 {
2162         EM_DEBUG_FUNC_BEGIN();
2163
2164         EM_DEBUG_LOG("\t new_activity[%p], err_code[%p]", new_activity, err_code);
2165
2166         /*  default variable */
2167         int ret = false;
2168         int err = EMAIL_ERROR_NONE;
2169
2170         if (!new_activity) {
2171                 EM_DEBUG_LOG("\t new_activity[%p]\n", new_activity);
2172                 err = EMAIL_ERROR_INVALID_PARAM;
2173                 goto FINISH_OFF;
2174         }
2175         if (!emstorage_add_activity(new_activity, false, &err)) {
2176                 EM_DEBUG_LOG("\t emstorage_add_activity falied - %d\n", err);
2177
2178                 goto FINISH_OFF;
2179         }
2180         ret = true;
2181
2182 FINISH_OFF:
2183         if (err_code)
2184                 *err_code = err;
2185
2186         return ret;
2187 }
2188
2189 INTERNAL_FUNC int emcore_delete_activity(emstorage_activity_tbl_t *activity, int *err_code)
2190 {
2191         EM_DEBUG_FUNC_BEGIN();
2192
2193         EM_DEBUG_LOG("\t new_activity[%p], err_code[%p]", activity, err_code);
2194
2195         /*  default variable */
2196         int ret = false;
2197         int err = EMAIL_ERROR_NONE;
2198         if (!activity) {
2199                 EM_DEBUG_LOG("\t new_activity[%p]\n", activity);
2200
2201                 err = EMAIL_ERROR_INVALID_PARAM;
2202                 goto FINISH_OFF;
2203         }
2204         if (!emstorage_delete_local_activity(activity, true, &err)) {
2205                 EM_DEBUG_LOG("\t emstorage_delete_local_activity falied - %d\n", err);
2206
2207                 goto FINISH_OFF;
2208         }
2209         ret = true;
2210
2211 FINISH_OFF:
2212         if (err_code)
2213                 *err_code = err;
2214
2215         return ret;
2216 }
2217
2218 INTERNAL_FUNC int emcore_get_next_activity_id(int *activity_id, int *err_code)
2219 {
2220         EM_DEBUG_FUNC_BEGIN();
2221
2222         int ret = false;
2223         int err = EMAIL_ERROR_NONE;
2224
2225         if (NULL == activity_id)
2226         {
2227                 EM_DEBUG_EXCEPTION("\t activity_id[%p]", activity_id);
2228
2229                 err = EMAIL_ERROR_INVALID_PARAM;
2230                 goto FINISH_OFF;
2231         }
2232
2233         if (false == emstorage_get_next_activity_id(activity_id, &err)) {
2234                 EM_DEBUG_LOG("\t emstorage_get_next_activity_id failed - %d\n", err);
2235                 goto FINISH_OFF;
2236         }
2237
2238         ret = true;
2239
2240         FINISH_OFF:
2241         if (NULL != err_code) {
2242                 *err_code = err;
2243         }
2244
2245         return ret;
2246
2247 }
2248
2249 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
2250
2251
2252 INTERNAL_FUNC void emcore_free_rule(email_rule_t* rule)
2253 {
2254         EM_DEBUG_FUNC_BEGIN();
2255
2256         if (!rule)
2257                 return;
2258
2259         EM_SAFE_FREE(rule->value);
2260
2261         EM_DEBUG_FUNC_END();
2262 }
2263
2264 INTERNAL_FUNC int emcore_search_string_from_file(char *file_path, char *search_string, int *result)
2265 {
2266         EM_DEBUG_FUNC_BEGIN("file_path : [%s], search_string : [%s]", file_path, search_string);
2267         int error = EMAIL_ERROR_NONE;
2268         int file_size = 0;
2269         int p_result = false;
2270         FILE *fp = NULL;
2271         char *buf = NULL;
2272         char *stripped = NULL;
2273
2274         if (!search_string || !file_path || !result) {
2275                 EM_DEBUG_EXCEPTION("Invalid parameter");
2276                 error = EMAIL_ERROR_INVALID_PARAM;
2277                 return error;
2278         }
2279
2280         fp = fopen(file_path, "r");
2281         if (fp == NULL) {
2282                 EM_DEBUG_EXCEPTION("fopen failed");
2283                 error = EMAIL_ERROR_SYSTEM_FAILURE;
2284                 goto FINISH_OFF;
2285         }
2286
2287         if (!emcore_get_file_size(file_path, &file_size, &error)) {
2288                 EM_DEBUG_EXCEPTION("emcore_get_file_size failed");
2289                 goto FINISH_OFF;
2290         }
2291
2292         buf = em_malloc(file_size);
2293         if (buf == NULL) {
2294                 EM_DEBUG_EXCEPTION("em_malloc failed");
2295                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2296                 goto FINISH_OFF;
2297         }
2298
2299         if (fread(buf, sizeof(char), file_size, fp) != file_size) {
2300                 EM_DEBUG_EXCEPTION("Get the data from file : failed");
2301                 error = EMAIL_ERROR_SYSTEM_FAILURE;
2302                 goto FINISH_OFF;
2303         }
2304
2305         /*prevent 35586*/
2306         stripped = em_replace_all_string(buf, CRLF_STRING, "");
2307         if (strstr(stripped, search_string))
2308                 p_result = true;
2309
2310 FINISH_OFF:
2311         if(!p_result)
2312                 EM_DEBUG_LOG("Search string[%s] not found",search_string);
2313
2314         *result = p_result;
2315
2316         if (fp)
2317                 fclose(fp);
2318
2319         EM_SAFE_FREE(buf);
2320         EM_SAFE_FREE(stripped);  /*prevent 35586*/
2321
2322         EM_DEBUG_FUNC_END("error:[%d]", error);
2323         return error;
2324 }
2325 /* EOF */