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