62e59008834a23fe15daf44bd09a060c7368eee3
[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 != NULL) {
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 (ferror(fp_html)) {
1208                         EM_DEBUG_EXCEPTION("fread failed [%s]", input_html_path);
1209                         err = EMAIL_ERROR_SYSTEM_FAILURE;
1210                         goto FINISH_OFF;
1211                 }
1212
1213                 if ( (err = emcore_strip_HTML(local_preview_text)) != EMAIL_ERROR_NONE) {
1214                         EM_DEBUG_EXCEPTION("emcore_strip failed");
1215                         goto FINISH_OFF;
1216                 }
1217         }
1218
1219         if ( (local_preview_text == NULL || (local_preview_text && EM_SAFE_STRLEN(local_preview_text) == 0) ) && input_plain_path != NULL) {
1220                 /*  get preview text from plain text file */
1221                 if( (err = em_get_encoding_type_from_file_path(input_plain_path, &encoding_type)) != EMAIL_ERROR_NONE) {
1222                         EM_DEBUG_EXCEPTION("em_get_encoding_type_from_file_path failed [%s]", err);
1223                         goto FINISH_OFF;
1224                 }
1225
1226                 if (!(fp_plain = fopen(input_plain_path, "r")))  {
1227                         EM_DEBUG_EXCEPTION("fopen failed [%s]", input_plain_path);
1228                         err = EMAIL_ERROR_SYSTEM_FAILURE;
1229                         goto FINISH_OFF;
1230                 }
1231
1232                 if (!(local_preview_text = (char*)em_malloc(sizeof(char) * local_preview_buffer_length))) {
1233                         EM_DEBUG_EXCEPTION("em_malloc failed");
1234                         goto FINISH_OFF;
1235                 }
1236
1237                 byte_read = fread(local_preview_text, sizeof(char), local_preview_buffer_length - 1, fp_plain);
1238
1239                 if (ferror(fp_plain)) {
1240                         EM_DEBUG_EXCEPTION("fread failed [%s]", input_plain_path);
1241                         err = EMAIL_ERROR_SYSTEM_FAILURE;
1242                         goto FINISH_OFF;
1243                 }
1244
1245                 reg_replace(local_preview_text, CR_STRING, " ");
1246                 reg_replace(local_preview_text, LF_STRING, " ");
1247                 reg_replace(local_preview_text, TAB_STRING, " ");
1248         }
1249
1250         if(local_preview_text) {
1251                 em_trim_left(local_preview_text);
1252                 EM_DEBUG_LOG("local_preview_text : [%s]", local_preview_text);
1253                 if(encoding_type && strcasecmp(encoding_type, "UTF-8") != 0) {
1254                         EM_DEBUG_LOG("encoding_type [%s]", encoding_type);
1255                         utf8_encoded_string = (char*)g_convert (local_preview_text, -1, "UTF-8", encoding_type, &byte_read, &byte_written, &glib_error);
1256
1257                         if(utf8_encoded_string == NULL) {
1258                                 EM_DEBUG_EXCEPTION("g_convert failed : byte_read[%d], EM_SAFE_STRLEN : [%d]", byte_read, EM_SAFE_STRLEN(local_preview_text));
1259                                 EM_DEBUG_LOG("Error is G_CONVERT_ERROR_ILLEGAL_SEQUENCE");
1260
1261                                 if (!g_error_matches (glib_error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE)) {
1262                                         EM_DEBUG_EXCEPTION("g_convert failed");
1263                                         goto FINISH_OFF;
1264                                 }
1265                                 
1266                                 EM_DEBUG_LOG("Extract the preview text, again");
1267
1268                                 utf8_encoded_string = (char *)g_convert(local_preview_text, byte_read, "UTF-8", encoding_type, &byte_read, &byte_written, &glib_error);
1269                                 if (utf8_encoded_string == NULL) {
1270                                         EM_DEBUG_EXCEPTION("g_convert failed : byte_read[%d]", byte_read);
1271                                         goto FINISH_OFF;
1272                                 }
1273
1274                         }       
1275                         EM_SAFE_FREE(local_preview_text);
1276                         local_preview_text = utf8_encoded_string;
1277                 }
1278         }
1279
1280 FINISH_OFF:
1281
1282         if (local_preview_text != NULL)
1283                 *output_preview_buffer = EM_SAFE_STRDUP(local_preview_text);
1284
1285         EM_SAFE_FREE(local_preview_text);
1286         EM_SAFE_FREE(encoding_type);
1287         
1288         if (fp_html != NULL)
1289                 fclose(fp_html);
1290
1291         if (fp_plain != NULL)
1292                 fclose(fp_plain);
1293
1294         EM_DEBUG_FUNC_END("err [%d]", err);
1295         return err;
1296 }
1297
1298 INTERNAL_FUNC int emcore_add_transaction_info(int mail_id, int handle , int *err_code)
1299 {
1300         EM_DEBUG_FUNC_BEGIN("mail_id[%d], handle[%d]", mail_id, handle);
1301
1302         int ret = false;
1303         int err = EMAIL_ERROR_NONE ;
1304         em_transaction_info_type_t  *pTransinfo = NULL ;
1305         em_transaction_info_type_t      *pTemp = NULL;
1306
1307         EM_DEBUG_LOG("g_transaction_info_list[%p]", g_transaction_info_list);
1308         pTransinfo = g_transaction_info_list ;
1309         
1310         if (!(pTemp = em_malloc(sizeof(em_transaction_info_type_t))))  {
1311                 EM_DEBUG_EXCEPTION("malloc failed...");
1312                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1313                 goto FINISH_OFF;
1314         }
1315         pTemp->mail_id = mail_id ;
1316         pTemp->handle = handle;
1317                 
1318         if (!pTransinfo) {
1319                 pTransinfo = pTemp ;
1320                 g_transaction_info_list = pTransinfo ;
1321         }
1322         else {
1323                 while (pTransinfo->next)
1324                         pTransinfo = pTransinfo->next;
1325                 pTransinfo->next = pTemp;
1326         }
1327         ret = true ;
1328         
1329 FINISH_OFF:
1330
1331         if (err_code)
1332                 *err_code = err;
1333         EM_DEBUG_FUNC_END("g_transaction_info_list[%p]", g_transaction_info_list);
1334         return ret;     
1335 }
1336
1337 INTERNAL_FUNC int emcore_get_handle_by_mailId_from_transaction_info(int mail_id, int *pHandle)
1338 {
1339         EM_DEBUG_FUNC_BEGIN("mail_id[%d], handle[%p]", mail_id, pHandle);
1340
1341         int ret = false;
1342         em_transaction_info_type_t  *pTransinfo = NULL ;
1343         
1344         if (g_transaction_info_list == NULL) {
1345                 EM_DEBUG_EXCEPTION("g_transaction_info_list NULL");
1346                 return false;
1347         }
1348         pTransinfo = g_transaction_info_list;
1349
1350         do {
1351                 EM_DEBUG_LOG("pTransinfo->mail_id[%d]", pTransinfo->mail_id);
1352                 if (pTransinfo->mail_id == mail_id) {
1353                         *pHandle = pTransinfo->handle;
1354                         ret = true;
1355                         EM_DEBUG_LOG("*pHandle[%d]", *pHandle);
1356                         break;
1357                 }
1358                 else
1359                         pTransinfo = pTransinfo->next ;
1360         }while (pTransinfo);
1361         EM_DEBUG_FUNC_END();
1362         return ret;
1363 }
1364
1365 INTERNAL_FUNC int emcore_delete_transaction_info_by_mailId(int mail_id )
1366 {
1367         EM_DEBUG_FUNC_BEGIN("mail_id[%d]", mail_id);
1368
1369         em_transaction_info_type_t  *pTransinfo ;
1370         em_transaction_info_type_t *pTemp = NULL;
1371
1372         if (g_transaction_info_list == NULL) {
1373                 EM_DEBUG_EXCEPTION("g_transaction_info_list NULL");
1374                 return false;
1375         }
1376         pTransinfo = g_transaction_info_list;
1377
1378         EM_DEBUG_LOG("pTransinfo[%p]", pTransinfo);
1379
1380         do {
1381                 EM_DEBUG_LOG("pTransinfo->mail_id[%d]", pTransinfo->mail_id);
1382                 if (pTransinfo->mail_id == mail_id) {
1383                         pTemp = pTransinfo->next ;
1384                         if (!pTemp) {
1385                                 EM_SAFE_FREE(pTransinfo) ;
1386                                 g_transaction_info_list = NULL;
1387                         }
1388                         else {
1389                                 pTransinfo->mail_id = pTransinfo->next->mail_id;
1390                                 pTransinfo->handle = pTransinfo->next->handle ;
1391                                 pTransinfo->next = pTransinfo->next->next;
1392
1393                                 EM_SAFE_FREE(pTemp);
1394                         }
1395                         break;
1396                 }
1397                 else {
1398                         pTransinfo = pTransinfo->next ;
1399                 }       
1400
1401         }while (pTransinfo);
1402         EM_DEBUG_FUNC_END();
1403         return true;
1404 }
1405
1406
1407 #include <regex.h>
1408
1409 int reg_replace (char *input_source_text, char *input_old_pattern_string, char *input_new_string)
1410 {
1411         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);
1412         int         error_code = EMAIL_ERROR_NONE; 
1413         char       *pos = NULL;
1414         int         so, n, nmatch, source_text_length, n_count = 1;
1415         regmatch_t *pmatch = NULL; 
1416         regex_t     reg_pattern;
1417
1418         if(!input_source_text || !input_old_pattern_string || !input_new_string) {
1419                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1420                 error_code = EMAIL_ERROR_INVALID_PARAM;
1421                 goto FINISH_OFF;                
1422         }
1423
1424         source_text_length = EM_SAFE_STRLEN(input_source_text);
1425
1426         regcomp(&reg_pattern, input_old_pattern_string, REG_ICASE);
1427
1428         nmatch = reg_pattern.re_nsub + 1;
1429
1430         EM_DEBUG_LOG("nmatch [%d]", nmatch);
1431
1432         if(nmatch < 1) {
1433                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_DATA");
1434                 error_code = EMAIL_ERROR_INVALID_DATA;
1435                 goto FINISH_OFF;                
1436         }
1437         
1438         pmatch = (regmatch_t*)em_malloc(sizeof(regmatch_t) * nmatch);
1439
1440         if(pmatch == NULL) {
1441                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_OUT_OF_MEMORY");
1442                 error_code = EMAIL_ERROR_OUT_OF_MEMORY;
1443                 goto FINISH_OFF;                
1444         }
1445         
1446         for (pos = input_new_string; *pos ; pos++) {
1447                 if (*pos == '\\' && *(pos + 1) > '0' && *(pos + 1) <= '9') {
1448                 
1449                         so = pmatch[*(pos + 1) - 48].rm_so;
1450                         n  = pmatch[*(pos + 1) - 48].rm_eo - so;
1451
1452                         EM_DEBUG_LOG("so [%d], n [%d]", so, n);
1453                         
1454                         if (so < 0 || EM_SAFE_STRLEN (input_new_string) + n - 1 > source_text_length) 
1455                                 break;
1456
1457                         memmove (pos + n, pos + 2, EM_SAFE_STRLEN (pos) - 1);
1458                         memmove (pos, input_source_text + so, n);
1459                         pos = pos + n - 2;
1460                 }
1461         }
1462         
1463         for (pos = input_source_text; !regexec (&reg_pattern, pos, 1, pmatch, 0);) {
1464                 n = pmatch[0].rm_eo - pmatch[0].rm_so;
1465                 pos += pmatch[0].rm_so;
1466
1467                 memmove (pos + EM_SAFE_STRLEN (input_new_string), pos + n, EM_SAFE_STRLEN (pos) - n + 1);
1468                 memmove (pos, input_new_string, EM_SAFE_STRLEN (input_new_string));
1469                 pos += EM_SAFE_STRLEN (input_new_string);
1470                 n_count++;
1471         }
1472         
1473 FINISH_OFF:
1474
1475         EM_SAFE_FREE(pmatch);
1476         regfree (&reg_pattern);
1477         
1478         EM_DEBUG_FUNC_END("error_code [%d]", error_code);
1479         return error_code;
1480 }
1481
1482
1483 int emcore_strip_HTML(char *source_string)
1484 {
1485         EM_DEBUG_FUNC_BEGIN("source_string [%p]", source_string);
1486
1487         int result = EMAIL_ERROR_NONE;
1488         
1489         reg_replace(source_string, CR_STRING, " ");
1490         reg_replace(source_string, LF_STRING, " ");
1491         reg_replace(source_string, "<head[^>]*>", "<head>"); /*  "<()*head([^>])*>", "<head>" */
1492         reg_replace(source_string, "<*/head>", "</head>");  /*  "(<()*(/)()*head()*>)", "</head>" */
1493         reg_replace(source_string, "<head>.*</head>", ""); /*  "(<head>).*(</head>)", "" */
1494         reg_replace(source_string, "<style[^>]*>.*</style>", "");
1495
1496         reg_replace(source_string, "<*/p>", " ");
1497         reg_replace(source_string, "<br>", " ");
1498
1499         /*   "<[^>]*>", " */
1500         reg_replace(source_string, "<[^>]*>", "");
1501
1502
1503         /*   "&bull;", " *  */
1504         /* reg_replace(source_string, "&bull;", " * "); */
1505
1506         /*   "&lsaquo;", "< */
1507         /* reg_replace(source_string, "&lsaquo;", "<"); */
1508
1509         /*   "&rsaquo;", "> */
1510         /* reg_replace(source_string, "&rsaquo;", ">"); */
1511
1512         /*   "&trade;", "(tm) */
1513         /* reg_replace(source_string, "&trade;", "(tm)"); */
1514
1515         /*   "&frasl;", "/ */
1516         /* reg_replace(source_string, "&frasl;", "/"); */
1517
1518         /*  "&lt;", "< */
1519         reg_replace(source_string, "&lt;", "<");
1520
1521         /*  "&gt;", "> */
1522         reg_replace(source_string, "&gt;", ">");
1523
1524         /*  "&copy;", "(c) */
1525         /* reg_replace(source_string, "&copy;", "(c)"); */
1526
1527         /* "&quot;", "\' */
1528         reg_replace(source_string, "&quot;", "\'");
1529
1530         /*  "&nbsp;", " */
1531         reg_replace(source_string, "&nbsp;", " ");
1532
1533         reg_replace(source_string, "  ", " ");
1534
1535         EM_DEBUG_FUNC_END();
1536
1537         return result;
1538 }
1539
1540 /*  emcore_send_noti_for_new_mail is not used currently because DBUS could not send very long message.*/
1541 /*  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).  */
1542 /*  This notification is including addtional information comparing NOTI_MAIL_ADD. */
1543 /*  By this change, email application will be able to add email item without additional DB query.  */
1544 /*  It might improve performance of sync email.  */
1545 /*  kyuho.jo 2010-09-07 */
1546
1547 INTERNAL_FUNC int emcore_send_noti_for_new_mail(int account_id, char *mailbox_name, char *subject, char *from, char *uid, char *datetime)
1548 {
1549         EM_DEBUG_FUNC_BEGIN("mailbox_name(%s) subject(%s), from(%s), uid(%s), datetime(%s)", mailbox_name, subject, from, uid, datetime);
1550         int error_code = EMAIL_ERROR_NONE;
1551         int param_length = 0;
1552         char *param_string = NULL;
1553
1554         if (mailbox_name == NULL || subject == NULL || from == NULL || uid == NULL || datetime == NULL) {
1555                 error_code = EMAIL_ERROR_INVALID_PARAM;
1556                 EM_DEBUG_EXCEPTION("Invalid parameter, mailbox_name(%p), subject(%p), from(%p), uid(%p), datetime(%p)", mailbox_name, subject, from, uid, datetime);
1557                 goto FINISH_OFF;
1558         }
1559
1560         param_length = strlen(mailbox_name) + strlen(subject) + strlen(from) + strlen(uid) + strlen(datetime) + 5; /*prevent 34358*/
1561
1562         param_string = em_malloc(param_length);
1563         if (param_string == NULL) {
1564                 error_code = EMAIL_ERROR_OUT_OF_MEMORY;
1565                 EM_DEBUG_EXCEPTION("Memory allocation for 'param_string' is failed");
1566                 goto FINISH_OFF;
1567         }
1568
1569         memset(param_string, 0x00, param_length);
1570
1571         SNPRINTF(param_string, param_length, "%s%c%s%c%s%c%s%c%s", mailbox_name, 0x01, subject, 0x01, from, 0x01, uid, 0x01, datetime);
1572
1573         if (emcore_notify_network_event(NOTI_DOWNLOAD_NEW_MAIL, account_id, param_string, 0, 0) == 0) { /*  failed */
1574                 error_code = EMAIL_ERROR_UNKNOWN;
1575                 EM_DEBUG_EXCEPTION("emcore_notify_network_event is failed");
1576                 goto FINISH_OFF;
1577         }
1578
1579 FINISH_OFF:
1580
1581         EM_SAFE_FREE(param_string);
1582         EM_DEBUG_FUNC_END();
1583         return error_code;
1584 }
1585
1586 #define MAX_TITLE_LENGTH 1024
1587 int emcore_update_notification_for_unread_mail(int account_id)
1588 {
1589         EM_DEBUG_FUNC_BEGIN("acccount_id[%d]", account_id);
1590         int ret = false;
1591         int i, account_count = 0;
1592         int err = EMAIL_ERROR_NONE;
1593         emstorage_account_tbl_t *p_account_tbl = NULL;
1594         
1595         if (!emstorage_get_account_list(&account_count, &p_account_tbl, true, false, &err)) {
1596                 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [%d]", err);
1597                 goto FINISH_OFF;
1598         }
1599
1600         for (i = 0; i < account_count ; i++) {
1601                 if (emcore_add_notification(p_account_tbl[i].account_id, 0, EMAIL_ACTION_NUM) != EMAIL_ERROR_NONE) {
1602                         EM_DEBUG_EXCEPTION("emcore_add_notification failed");
1603                         continue;
1604                 }
1605         }
1606
1607         ret = true;
1608
1609 FINISH_OFF:
1610
1611         if (p_account_tbl)
1612                 emstorage_free_account(&p_account_tbl, account_count, NULL);
1613
1614         EM_DEBUG_FUNC_END();
1615         return ret;
1616 }
1617
1618 INTERNAL_FUNC int emcore_finalize_sync(int account_id, int *error)
1619 {
1620         EM_DEBUG_FUNC_BEGIN("account_id [%d], error [%p]", account_id, error);
1621         int err = EMAIL_ERROR_NONE, ret = true, result_sync_status = SYNC_STATUS_FINISHED;
1622
1623         if ((err = emcore_update_sync_status_of_account(account_id, SET_TYPE_MINUS, SYNC_STATUS_SYNCING)) != EMAIL_ERROR_NONE)
1624                 EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
1625
1626         if (!emstorage_get_sync_status_of_account(ALL_ACCOUNT, &result_sync_status, &err))
1627                 EM_DEBUG_EXCEPTION("emstorage_get_sync_status_of_account failed [%d]", err);
1628
1629         if (result_sync_status == SYNC_STATUS_HAVE_NEW_MAILS) {
1630                 if (!emcore_update_notification_for_unread_mail(ALL_ACCOUNT))
1631                         EM_DEBUG_EXCEPTION("emcore_update_notification_for_unread_mail failed");
1632
1633                 if (!emcore_display_unread_in_badge()) 
1634                         EM_DEBUG_EXCEPTION("emcore_display_unread_in_badge failed");
1635
1636                 emcore_start_alert();
1637
1638                 if ((err = emcore_update_sync_status_of_account(account_id, SET_TYPE_MINUS, SYNC_STATUS_HAVE_NEW_MAILS)) != EMAIL_ERROR_NONE)
1639                         EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
1640         }
1641
1642         EM_DEBUG_FUNC_END();
1643         return ret;
1644 }
1645
1646 INTERNAL_FUNC int emcore_clear_all_notifications()
1647 {
1648         int account_count = 0, i;
1649         emstorage_account_tbl_t *account_list;
1650         int error_code = EMAIL_ERROR_NONE;
1651         
1652         if(!emstorage_get_account_list(&account_count, &account_list, true, false, &error_code)) {
1653                 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed");
1654                 goto FINISH_OFF;
1655         }
1656
1657         for(i = 0; i < account_count; i++) {
1658                 emcore_delete_notification_by_account(account_list[i].account_id);
1659         }
1660
1661 FINISH_OFF:
1662         if(account_count) {
1663                 emstorage_free_account(&account_list, account_count, NULL);
1664         }
1665
1666         EM_DEBUG_FUNC_END("return[%d]", error_code);
1667         return error_code;
1668 }
1669
1670 #define EAS_EXECUTABLE_PATH "/usr/bin/eas-engine"
1671
1672 INTERNAL_FUNC int emcore_delete_notification_by_account(int account_id)
1673 {
1674         EM_DEBUG_FUNC_BEGIN("account_id [%d]", account_id);
1675         int error_code = EMAIL_ERROR_NONE;
1676         int private_id = 0;
1677         char vconf_private_id[MAX_PATH] = {0, };
1678         
1679         SNPRINTF(vconf_private_id, sizeof(vconf_private_id), "%s/%d", VCONF_KEY_NOTI_PRIVATE_ID, account_id);
1680         if (vconf_get_int(vconf_private_id, &private_id) != 0) {
1681                 EM_DEBUG_EXCEPTION("vconf_get_int failed");
1682         }
1683         EM_DEBUG_FUNC_END();
1684         return error_code;
1685 }
1686
1687 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
1688
1689 /**
1690  * @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)
1691  * Prepare a linked list of uid ranges with each node having a uid_range and lowest and highest uid in it.
1692  *
1693  *@author                                       h.gahlaut@samsung.com
1694  * @param[in] id_set                    Specifies the array of mail_id and corresponding server_mail_id sorted by server_mail_ids in ascending order
1695  * @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
1696  * @param[in] range_len         Specifies the maximum length of string of range allowed. 
1697  * @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
1698  * @param[out] err_code         Returns the error code.
1699  * @remarks                                     An example of a uid_range formed is 2:6,8,10,14:15,89, 
1700  *                                                      While using it the caller should remove the ending, (comma)
1701  * @return This function returns true on success or false on failure.
1702  */
1703  
1704 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)
1705 {
1706         EM_DEBUG_FUNC_BEGIN();
1707
1708         int ret = false;
1709         int error = EMAIL_ERROR_NONE;
1710
1711         if (NULL == id_set || id_set_count  <= 0 || NULL == uid_range_set) {
1712                 EM_DEBUG_EXCEPTION(" Invalid Parameter id_set[%p] id_set_count[%d] uid_range_set[%p]", id_set, id_set_count, uid_range_set);
1713                 error = EMAIL_ERROR_INVALID_PARAM;
1714                 goto FINISH_OFF;
1715         }
1716
1717         int i = 0;
1718         unsigned long current_uid = 0;
1719         unsigned long first_uid = 0;
1720         unsigned long last_uid = 0;
1721         const int max_subset_string_size = MAX_SUBSET_STRING_SIZE;                      
1722         char subset_string[MAX_SUBSET_STRING_SIZE] = {0,};      
1723         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*/
1724
1725         if (range_len < (max_subset_string_size + 1))           /* 1 for ending NULL character */ {
1726                 EM_DEBUG_EXCEPTION(" Invalid Parameter range_len[%d]", range_len);
1727                 error = EMAIL_ERROR_INVALID_PARAM;
1728                 goto FINISH_OFF;
1729         }
1730         
1731         EM_DEBUG_LOG("id set count[%d] range_len[%d]", id_set_count, range_len);
1732         
1733         do {
1734                 first_uid = last_uid = current_uid = id_set[i].server_mail_id;
1735                 /* Start subset string by putting first server mail id in it from id_set*/
1736                 memset(subset_string, 0x00, max_subset_string_size);
1737                 SNPRINTF(subset_string, max_subset_string_size, "%lu", first_uid);
1738                 ++i;
1739                 
1740                 /* Check if only one server mail id was left in id_set */
1741                 if (i >= id_set_count) {
1742                         /* No more server mail id left in id_set */
1743                         if (false == emcore_append_subset_string_to_uid_range(subset_string, &current_node, uid_range_set, range_len, first_uid, last_uid)) {
1744                                 EM_DEBUG_EXCEPTION("emcore_append_subset_string_to_uid_range failed");
1745                                 goto FINISH_OFF;
1746                         }
1747                         break;
1748                 }
1749                 else {
1750                         /* 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 */
1751                         do {
1752                                 current_uid = id_set[i].server_mail_id;
1753                                 if (current_uid == (last_uid + 1)) {
1754                                         last_uid = current_uid;
1755                                         ++i;                    
1756                                 }
1757                                 else {  
1758                                         memset(subset_string, 0x00, max_subset_string_size);
1759                                         if (first_uid != last_uid)      /* Form subset string by first_uid:last_uid */
1760                                                 SNPRINTF(subset_string, max_subset_string_size, "%lu:%lu", first_uid, last_uid);
1761                                         else    /* Form subset string by first_uid */
1762                                                 SNPRINTF(subset_string, max_subset_string_size, "%lu", first_uid);
1763                                         
1764                                         if (false == emcore_append_subset_string_to_uid_range(subset_string, &current_node, uid_range_set, range_len, first_uid, last_uid)) {
1765                                                 EM_DEBUG_EXCEPTION("emcore_append_subset_string_to_uid_range failed");
1766                                                 goto FINISH_OFF;
1767                                         }
1768                                         /* To Start formation of new subset string break out of inner loop */
1769                                         break;                                  
1770                                 }
1771                                 
1772                         } while (i < id_set_count);             
1773
1774                         /* Flow comes here in two cases :
1775                         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
1776                         2. due to break statement */
1777
1778                         if (last_uid == current_uid) {
1779                                 /* Case 1 */
1780                                 
1781                                 memset(subset_string, 0x00, max_subset_string_size);
1782                                 SNPRINTF(subset_string, max_subset_string_size, "%lu:%lu", first_uid, last_uid);
1783                         
1784                                 if (false == emcore_append_subset_string_to_uid_range(subset_string, &current_node, uid_range_set, range_len, first_uid, last_uid)) {
1785                                         EM_DEBUG_EXCEPTION("emcore_append_subset_string_to_uid_range failed");
1786                                         goto FINISH_OFF;
1787                                 }
1788                         }
1789                         else {
1790                                 /* Case 2: Do Nothing */
1791                         }
1792                                 
1793                 }               
1794         } while (i < id_set_count);
1795
1796         ret = true;
1797         
1798 FINISH_OFF:
1799         if (NULL != err_code)
1800                 *err_code = error;
1801         EM_DEBUG_FUNC_END();
1802         return ret;
1803         
1804 }  
1805
1806 /**
1807  * @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)
1808  * 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 
1809  * and stores the subset_string in its uid_range. Also sets the lowest and highest uids for the corresponsing uid_range
1810  * 
1811  * @author                                      h.gahlaut@samsung.com
1812  * @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.
1813  * @param[in] range_len         Specifies the maximum length of range string allowed. 
1814  * @param[in] luid                      Specifies the lowest uid in subset string
1815  * @param[in] huid                      Specifies the highest uid in subset string
1816  * @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
1817  * @param[out] err_code         Returns the error code.
1818  * @remarks                                                                             
1819  * @return This function returns true on success or false on failure.
1820  */
1821  
1822 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)
1823 {
1824         EM_DEBUG_FUNC_BEGIN();
1825         email_uid_range_set *current_node = NULL;
1826
1827         if (NULL == (*uid_range_set)) {
1828                 /*This happens only once when list  creation starts. Head Node is allocated */
1829                 current_node = (email_uid_range_set *)em_malloc(sizeof(email_uid_range_set));
1830                 if (NULL == current_node) {
1831                         EM_DEBUG_EXCEPTION("em_malloc failed");
1832                         return false;
1833                 }       
1834
1835                 current_node->uid_range = (char *)em_malloc(range_len);
1836                         
1837                 if (NULL == current_node->uid_range) {
1838                         EM_DEBUG_EXCEPTION("em_malloc failed");
1839                         EM_SAFE_FREE(current_node);
1840                         return false;
1841                 }
1842
1843                 SNPRINTF(current_node->uid_range, range_len, "%s,", subset_string);
1844
1845                 current_node->lowest_uid = luid;
1846                 current_node->highest_uid = huid;
1847                 (*uid_range_set) = current_node;
1848
1849                 (*current_node_adr) = current_node; 
1850                 
1851         }
1852         else {
1853                 /* Apart from first call to this function flow will always come here */
1854                 current_node = (*current_node_adr);
1855                 int len_sub_string = EM_SAFE_STRLEN(subset_string);
1856                 int space_left_in_buffer = range_len - EM_SAFE_STRLEN(current_node->uid_range);
1857
1858                 if ((len_sub_string + 1 + 1) <= space_left_in_buffer)   /* 1 for comma + 1 for ending null character */ {
1859                         SNPRINTF(current_node->uid_range + EM_SAFE_STRLEN(current_node->uid_range), space_left_in_buffer, "%s,", subset_string);
1860                         current_node->highest_uid = huid;
1861                 }
1862                 else {
1863                         /* No more space left in uid_range string.If continued on it, it will exceeded max size of range_len */
1864                         /* Allocate new node in Uid Range set */
1865                         email_uid_range_set *new_node = NULL;
1866
1867                         new_node = (email_uid_range_set *)em_malloc(sizeof(email_uid_range_set));
1868
1869                         if (NULL == new_node) {
1870                                 EM_DEBUG_EXCEPTION("em_malloc failed");
1871                                 return false;
1872                         }
1873
1874                         /* Allocate uid_range of new node */
1875                 
1876                         new_node->uid_range =  (char *)em_malloc(range_len);
1877
1878                         if (NULL == new_node->uid_range) {
1879                                 EM_DEBUG_EXCEPTION("em_malloc failed");
1880                                 EM_SAFE_FREE(new_node);
1881                                 return false;
1882                         }
1883
1884                         SNPRINTF(new_node->uid_range, range_len, "%s, ", subset_string);
1885
1886                         new_node->lowest_uid = luid;
1887                         new_node->highest_uid = huid;
1888
1889                         current_node->next = new_node;
1890
1891                         (*current_node_adr) = new_node; 
1892                 }
1893         }
1894         EM_DEBUG_FUNC_END();
1895         return true;
1896 }
1897
1898 /**
1899  * void emcore_free_uid_range_set(email_uid_range_set **uid_range_head)
1900  * Frees the linked list of uid ranges 
1901  *
1902  * @author                                      h.gahlaut@samsung.com
1903  * @param[in] uid_range_head    Head pointer of linked list of uid ranges               
1904  * @remarks                                                                     
1905  * @return This function does not return anything.
1906  */
1907  
1908 INTERNAL_FUNC
1909 void emcore_free_uid_range_set(email_uid_range_set **uid_range_set)
1910 {
1911         EM_DEBUG_FUNC_BEGIN();
1912
1913         email_uid_range_set *current_node = NULL;
1914         email_uid_range_set *uid_range_head = NULL;
1915         
1916         current_node = uid_range_head = (*uid_range_set);       /* Make the current node and head ptr point to starting of  uid_range_set */
1917
1918         while (current_node) {          
1919                 uid_range_head = current_node->next;            /* Move the head ptr to next node*/
1920
1921                 EM_SAFE_FREE(current_node->uid_range);
1922                 EM_SAFE_FREE(current_node);                             /* Free the current node */
1923
1924                 current_node = uid_range_head;                  /* Make the current node point to head ptr */
1925         }
1926
1927         (*uid_range_set) = NULL;
1928         EM_DEBUG_FUNC_END();
1929 }
1930
1931
1932 /**
1933  * @fn emcore_form_comma_separated_strings(int numbers[], int num_count, int max_string_len, char *** strings, int *string_count, int *err_code)
1934  * Forms comma separated strings of a give max_string_len from an array of numbers 
1935  * 
1936  * @author                                      h.gahlaut@samsung.com
1937  * @param[in] numbers                   Specifies the array of numbers to be converted into comma separated strings.
1938  * @param[in] num_count         Specifies the count of numbers in numbers array. 
1939  * @param[in] max_string_len    Specifies the maximum length of comma separated strings that are to be formed.
1940  * @param[out] strings                  Returns the base address of a double dimension array which stores the strings.
1941  * @param[out] string_count             Returns the number of strings formed.
1942  * @param[out] err_code         Returns the error code.
1943  * @remarks                                     If Input to the function is five numbers like 2755 2754 2748 2749 2750 and a given max_string_len is 20.
1944  *                                                      Then this function will form two comma separated strings as follows -
1945  *                                                      "2755, 2754, 2748"
1946  *                                                      "2749, 2750"
1947  * @return This function returns true on success or false on failure.
1948  */
1949
1950 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)
1951 {
1952         EM_DEBUG_FUNC_BEGIN();
1953
1954         int error = EMAIL_ERROR_NONE;
1955         int ret = false;
1956
1957         char **string_list = NULL;
1958         int num_of_strings = 0;
1959         int i = 0;
1960         int j =0;
1961         char num[MAX_INTEGER_LENGTH + 1] = {0, };
1962         int num_len = 0;
1963         int space_in_buffer = 0;
1964         int len_of_string_formed = 0;
1965
1966         if (NULL == numbers || num_count <= 0 || \
1967                 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.  */ {
1968                 EM_DEBUG_EXCEPTION("Invalid Parameter numbers[%p] num_count [%d] max_string_len [%d] strings [%p] string_count[%p]", \
1969                         numbers, num_count, max_string_len, strings, string_count);
1970                 error = EMAIL_ERROR_INVALID_PARAM;
1971                 goto FINISH_OFF;
1972         }
1973
1974         EM_DEBUG_LOG("num_count [%d] max_string_len [%d]", num_count, max_string_len);
1975
1976         string_list = em_malloc(sizeof(char *));
1977
1978         if (NULL == string_list) {
1979                 EM_DEBUG_EXCEPTION("em_malloc failed ");
1980                 goto FINISH_OFF;
1981         }
1982         
1983         string_list[num_of_strings] = em_malloc(max_string_len);
1984         
1985         if (NULL == string_list[num_of_strings]) {
1986                 EM_DEBUG_EXCEPTION("em_malloc failed ");
1987                 goto FINISH_OFF;
1988         }
1989         
1990         ++num_of_strings;
1991         space_in_buffer = max_string_len;
1992         
1993         for (j = 0; j < num_count;++j) {
1994                 memset(num, 0x00, MAX_INTEGER_LENGTH + 1);
1995                 SNPRINTF(num, MAX_INTEGER_LENGTH + 1, "%d", numbers[j]);
1996
1997                 num_len = EM_SAFE_STRLEN(num);
1998                 
1999                 len_of_string_formed = EM_SAFE_STRLEN(string_list[num_of_strings - 1]);
2000
2001                 space_in_buffer = max_string_len - len_of_string_formed ;
2002
2003                 if (space_in_buffer >= (num_len+1+1))                   /*  1 for comma and 1 for ending NULL */ {
2004                         SNPRINTF(string_list[num_of_strings - 1] + len_of_string_formed, max_string_len, "%d,", numbers[j]);
2005                 }
2006                 else {  /*  Removing comma at end of string  */
2007                         string_list[num_of_strings - 1][len_of_string_formed-1] = '\0';         
2008                         char **temp = NULL;
2009                         temp = (char **)realloc(string_list, sizeof(char *) * (num_of_strings + 1));    /*  Allocate new buffer to store a pointer to a new string */
2010
2011                         if (NULL == temp) {     
2012                                 EM_DEBUG_EXCEPTION("realloc failed");
2013                                 goto FINISH_OFF;
2014                         }
2015
2016                         memset(temp + num_of_strings, 0X00, sizeof(char *));
2017
2018                         string_list = temp;
2019                         temp = NULL;
2020                         string_list[num_of_strings] = em_malloc(max_string_len);/*  Allocate new buffer to store the string */
2021
2022                         if (NULL == string_list[num_of_strings]) {
2023                                 EM_DEBUG_EXCEPTION(" em_malloc failed ");
2024                                 goto FINISH_OFF;
2025                         }
2026                         ++num_of_strings;
2027                         SNPRINTF(string_list[num_of_strings - 1] , max_string_len, "%d,", numbers[j]);/*  Start making new string */
2028                 }
2029         }
2030
2031         /*  Removing comma at end of string  */
2032         len_of_string_formed = EM_SAFE_STRLEN(string_list[num_of_strings - 1]);
2033         string_list[num_of_strings - 1][len_of_string_formed-1] = '\0'; 
2034         ret = true;
2035
2036 FINISH_OFF:
2037
2038         if (false == ret)
2039                 emcore_free_comma_separated_strings(&string_list, &num_of_strings);
2040
2041         if (true == ret) {
2042                 for (i = 0; i < num_of_strings;++i)
2043                         EM_DEBUG_LOG("%s", string_list[i]);
2044                 *strings = string_list;
2045                 *string_count = num_of_strings;
2046         }
2047         
2048
2049         if (NULL != err_code)
2050                 *err_code = error;
2051
2052         EM_DEBUG_FUNC_END("ret [%d]", ret);
2053         return ret;
2054 }
2055 /**
2056  * @fn emcore_free_comma_separated_strings(char *** string_list, int *string_count)
2057  * Frees the double dimensional array of strings. 
2058  *
2059  * @author                                      h.gahlaut@samsung.com
2060  * @param[in] uid_range_head    Address of base address of double dimensional array of strings.
2061  * @param[in] string_count              Address of variable holding the count of strings.
2062  * @remarks                                                                     
2063  * @return This function does not return anything.
2064  */
2065 INTERNAL_FUNC void emcore_free_comma_separated_strings(char *** string_list, int *string_count)
2066 {
2067         EM_DEBUG_FUNC_BEGIN();
2068         int i = 0;
2069         char **str_list = NULL;
2070         int count = 0;
2071
2072         if (NULL != string_list) {
2073                 str_list = *string_list;
2074
2075                 if (0 != *string_count) {
2076                         count = *string_count;
2077                         for (i = 0; i < count; ++i)
2078                                 EM_SAFE_FREE(str_list[i]);
2079                 }
2080                 
2081                 EM_SAFE_FREE(str_list);
2082                 *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 */
2083                 *string_count = 0;
2084         }
2085         EM_DEBUG_FUNC_END();
2086 }
2087
2088
2089 #endif
2090
2091
2092
2093
2094 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)
2095 {
2096         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);
2097         int ret = false, err = EMAIL_ERROR_NONE;
2098         char *extcheck = NULL;
2099         char attachment_file_name[MAX_PATH + 1] = { 0, };
2100
2101         if (!source_file_name || !result_file_name) {
2102                 EM_DEBUG_EXCEPTION("Invalid Parameter");
2103                 err  = EMAIL_ERROR_INVALID_PARAM;
2104                 goto FINISH_OFF;
2105         }
2106                                 
2107         strncpy(attachment_file_name, source_file_name, MAX_PATH);
2108         extcheck = strchr(attachment_file_name, '.');
2109
2110         if (extcheck)
2111                 EM_DEBUG_LOG("Extension Exist in the Attachment [%s] ", extcheck);
2112         else  { /* No extension attached, So add the Extension based on the subtype */
2113                 if (sub_type) {
2114                         strcat(attachment_file_name, ".");
2115                         strcat(attachment_file_name, sub_type);
2116                         EM_DEBUG_LOG("attachment_file_name with extension[%s] ", attachment_file_name);
2117                 }
2118                 else
2119                         EM_DEBUG_LOG("UnKnown Extesnsion");
2120
2121         }
2122         memset(result_file_name, 0 , result_file_name_buffer_length);
2123         EM_SAFE_STRNCPY(result_file_name, attachment_file_name, result_file_name_buffer_length - 1);
2124         EM_DEBUG_LOG("*result_file_name[%s]", result_file_name);
2125         ret = true;
2126
2127 FINISH_OFF:
2128         if (err_code)
2129                 *err_code = err;
2130         EM_DEBUG_FUNC_END();
2131         return ret;
2132 }
2133
2134 #ifdef __FEATURE_LOCAL_ACTIVITY__
2135 INTERNAL_FUNC int emcore_add_activity(emstorage_activity_tbl_t *new_activity, int *err_code)
2136 {
2137         EM_DEBUG_FUNC_BEGIN();
2138         
2139         EM_DEBUG_LOG("\t new_activity[%p], err_code[%p]", new_activity, err_code);
2140
2141         /*  default variable */
2142         int ret = false;
2143         int err = EMAIL_ERROR_NONE;
2144         
2145         if (!new_activity) {
2146                 EM_DEBUG_LOG("\t new_activity[%p]\n", new_activity);
2147                 err = EMAIL_ERROR_INVALID_PARAM;
2148                 goto FINISH_OFF;
2149         }
2150         if (!emstorage_add_activity(new_activity, false, &err)) {
2151                 EM_DEBUG_LOG("\t emstorage_add_activity falied - %d\n", err);
2152                 
2153                 goto FINISH_OFF;
2154         }
2155         ret = true;
2156         
2157 FINISH_OFF:
2158         if (err_code)
2159                 *err_code = err;
2160         
2161         return ret;
2162 }
2163
2164 INTERNAL_FUNC int emcore_delete_activity(emstorage_activity_tbl_t *activity, int *err_code)
2165 {
2166         EM_DEBUG_FUNC_BEGIN();
2167         
2168         EM_DEBUG_LOG("\t new_activity[%p], err_code[%p]", activity, err_code);
2169         
2170         /*  default variable */
2171         int ret = false;
2172         int err = EMAIL_ERROR_NONE;
2173         if (!activity) {
2174                 EM_DEBUG_LOG("\t new_activity[%p]\n", activity);
2175                 
2176                 err = EMAIL_ERROR_INVALID_PARAM;
2177                 goto FINISH_OFF;
2178         }
2179         if (!emstorage_delete_local_activity(activity, true, &err)) {
2180                 EM_DEBUG_LOG("\t emstorage_delete_local_activity falied - %d\n", err);
2181                 
2182                 goto FINISH_OFF;
2183         }
2184         ret = true;
2185         
2186 FINISH_OFF:
2187         if (err_code)
2188                 *err_code = err;
2189         
2190         return ret;
2191 }
2192
2193 INTERNAL_FUNC int emcore_get_next_activity_id(int *activity_id, int *err_code)
2194 {
2195         EM_DEBUG_FUNC_BEGIN();
2196         
2197         int ret = false;
2198         int err = EMAIL_ERROR_NONE;
2199
2200         if (NULL == activity_id)
2201         {
2202                 EM_DEBUG_EXCEPTION("\t activity_id[%p]", activity_id);
2203                 
2204                 err = EMAIL_ERROR_INVALID_PARAM;
2205                 goto FINISH_OFF;
2206         }
2207         
2208         if (false == emstorage_get_next_activity_id(activity_id, &err)) {
2209                 EM_DEBUG_LOG("\t emstorage_get_next_activity_id failed - %d\n", err);
2210                 goto FINISH_OFF;
2211         }
2212         
2213         ret = true;
2214         
2215         FINISH_OFF:
2216         if (NULL != err_code) {
2217                 *err_code = err;
2218         }
2219         
2220         return ret;
2221
2222 }
2223
2224 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
2225
2226
2227 INTERNAL_FUNC void emcore_free_rule(email_rule_t* rule)
2228 {
2229         EM_DEBUG_FUNC_BEGIN();
2230
2231         if (!rule)
2232                 return;
2233
2234         EM_SAFE_FREE(rule->value);
2235
2236         EM_DEBUG_FUNC_END();
2237 }
2238
2239 INTERNAL_FUNC int emcore_search_string_from_file(char *file_path, char *search_string, int *result)
2240 {
2241         EM_DEBUG_FUNC_BEGIN("file_path : [%s], search_string : [%s]", file_path, search_string);
2242         int error = EMAIL_ERROR_NONE;
2243         int file_size = 0;
2244         int p_result = 1;
2245         FILE *fp = NULL;
2246         char *buf = NULL;
2247
2248         if (search_string == NULL || file_path == NULL) {
2249                 EM_DEBUG_EXCEPTION("Invalid parameter");
2250                 error = EMAIL_ERROR_INVALID_PARAM;
2251                 return error;
2252         }
2253
2254         fp = fopen(file_path, "r");
2255         if (fp == NULL) {
2256                 EM_DEBUG_EXCEPTION("fopen failed");
2257                 error = EMAIL_ERROR_SYSTEM_FAILURE;
2258                 goto FINISH_OFF;
2259         }
2260
2261         if (!emcore_get_file_size(file_path, &file_size, &error)) {
2262                 EM_DEBUG_EXCEPTION("emcore_get_file_size failed");
2263                 goto FINISH_OFF;
2264         }
2265
2266         buf = em_malloc(file_size);
2267         if (buf == NULL) {
2268                 EM_DEBUG_EXCEPTION("em_malloc failed");
2269                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2270                 goto FINISH_OFF;
2271         }
2272
2273         if (fread(buf, sizeof(char), file_size, fp) != file_size) {
2274                 EM_DEBUG_EXCEPTION("Get the data from file : failed");
2275                 error = EMAIL_ERROR_SYSTEM_FAILURE;
2276                 goto FINISH_OFF;
2277         }
2278
2279         buf = em_replace_all_string(buf, CRLF_STRING, "");
2280         if (!strstr(buf, search_string)) {
2281                 EM_DEBUG_LOG("Not found string");
2282                 p_result = 0;
2283         }
2284
2285 FINISH_OFF:
2286
2287         if (result)
2288                 *result = p_result;
2289         
2290         if (fp)
2291                 fclose(fp);
2292
2293         EM_SAFE_FREE(buf);      
2294
2295         EM_DEBUG_FUNC_END("error:[%d]", error);
2296         return error;
2297 }
2298 /* EOF */