Fixed the invalid casting and remove the flash noti
[platform/core/messaging/email-service.git] / email-core / email-core-utils.c
1 /*
2 *  email-service
3 *
4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5 *
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 */
21
22
23 /******************************************************************************
24  * File: email-core-utils.c
25  * Desc: Mail Utils
26  *
27  * Auth:
28  *
29  * History:
30  *      2006.08.16 : created
31  *****************************************************************************/
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <ctype.h>
37 #include <errno.h>
38 #include <dlfcn.h>
39 #include <libintl.h>
40 #include <locale.h>
41 #include <glib.h>
42 #include <glib-object.h>
43 #include <sys/time.h>
44 #include <unistd.h>
45 #include <sys/vfs.h>
46 #include <sys/stat.h>
47 #include <vconf.h>
48 #include <regex.h>
49 #include <pthread.h>
50 #include <notification.h>
51 #include <notification_type.h>
52 #include <notification_internal.h>
53 #include <notification_text_domain.h>
54 #include <badge.h>
55 #include <badge_internal.h>
56 #ifdef __FEATURE_USE_DRM_API__
57 #include <drm_client_types.h>
58 #include <drm_client.h>
59 #endif /* __FEATURE_USE_DRM_API__ */
60 #include <feedback.h>
61 #include <storage.h>
62 #include <bundle.h>
63 #include <curl/curl.h>
64 #include <contacts.h>
65 #include <contacts_internal.h>
66
67 #include "email-types.h"
68 #include "email-core-global.h"
69 #include "email-core-utils.h"
70 #include "email-debug-log.h"
71 #include "email-core-mail.h"
72 #include "email-core-event.h"
73 #include "email-core-mailbox.h"
74 #include "email-core-account.h"
75 #include "email-core-mailbox-sync.h"
76 #include "email-core-mime.h"
77 #include "email-core-gmime.h"
78 #include "email-core-signal.h"
79 #include "email-daemon.h"
80 #include "email-utilities.h"
81 #include "email-convert.h"
82 #include "email-internal-types.h"
83 #include "email-device.h"
84 #include "email-core-container.h"
85
86 #include <gio/gio.h>
87 #include "email-dbus-activation.h"
88
89
90 #include <app_control.h>
91 #include <app_control_internal.h>
92
93 #define LED_TIMEOUT_SECS          12
94 #define G_DISPLAY_LENGTH          256
95
96 #define DIR_SEPERATOR_CH          '/'
97 #define EMAIL_CH_QUOT             '"'
98 #define EMAIL_CH_BRACKET_S        '<'
99 #define EMAIL_CH_BRACKET_E        '>'
100 #define EMAIL_CH_COMMA            ','
101 #define EMAIL_CH_SEMICOLON        ';'
102 #define EMAIL_CH_ROUND_BRACKET_S  '('
103 #define EMAIL_CH_ROUND_BRACKET_E  ')'
104 #define EMAIL_CH_SQUARE_BRACKET_S '['
105 #define EMAIL_CH_SQUARE_BRACKET_E ']'
106 #define EMAIL_CH_SPACE            ' '
107 #define EMAIL_NOTI_ICON_PATH                "/usr/apps/org.tizen.quickpanel/shared/res/noti_icons/noti_email.png"
108 #define EMAIL_NOTI_INDICATOR_ICON_PATH      "/usr/apps/org.tizen.indicator/res/icons/Event/B03_Event_email.png"
109 #define EMAIL_NOTI_MAX_MAIL_ID   100
110
111
112 typedef struct  _em_transaction_info_type_t {
113         int mail_id;
114         int     handle;
115         struct _em_transaction_info_type_t *next;
116
117 } em_transaction_info_type_t;
118
119 em_transaction_info_type_t  *g_transaction_info_list;
120
121 __thread email_session_t g_session = {0};
122 static notification_h g_sending_noti_handle = NULL;
123 typedef struct emcore_account_list_t emcore_account_list_t;
124 struct emcore_account_list_t {
125         email_account_t *account;
126         emcore_account_list_t *next;
127 };
128
129 #include <gmime/gmime.h>
130
131 INTERNAL_FUNC char *emcore_convert_mutf7_to_utf8(char *mailbox_name)
132 {
133         EM_DEBUG_FUNC_BEGIN("mailbox_name[%p]", mailbox_name);
134
135         iconv_t cd;
136         char *result_mailbox_name = NULL;
137         char *mutf7_text = NULL;
138         char *cursor = NULL;
139         int  is_base64 = 0;
140
141         if (mailbox_name == NULL) {
142                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
143                 return NULL;
144         }
145
146         EM_DEBUG_LOG_SEC("mailbox_name[%s]", mailbox_name);
147
148         cursor = mutf7_text = EM_SAFE_STRDUP(mailbox_name);
149
150         if (mutf7_text == NULL) {
151                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_OUT_OF_MEMORY");
152                 return NULL;
153         }
154         
155         for (; *cursor; ++cursor)
156                 switch (*cursor) {
157                         case '+':
158                                 if (!is_base64) *cursor = '&';
159                                 break;
160
161                         case '&':
162                                 *cursor = '+';
163                                 is_base64 = 1;
164                                 break;
165
166                         case '-':
167                                 is_base64 = 0;
168                                 break;
169
170                         case ',':
171                                 if (is_base64)
172                                         *cursor = '/';
173                                 break;
174                 }
175
176         cd = g_mime_iconv_open("UTF-8", "UTF-7");
177
178         if (cd)
179                 result_mailbox_name = g_mime_iconv_strdup(cd, mutf7_text);
180
181         EM_DEBUG_LOG_SEC("result_mailbox_name[%s]", result_mailbox_name);
182
183         if (result_mailbox_name) {
184                 cursor = result_mailbox_name;
185                 for (; *cursor; ++cursor) {
186                         switch (*cursor) {
187                           case '&':
188                                 *cursor = '+';
189                                 break;
190
191                           case '+':
192                                 *cursor = '&';
193                                 break;
194                         }
195                 }
196         }
197         else {
198                 result_mailbox_name = (char *)utf8_from_mutf7((unsigned char *)mailbox_name);
199                 EM_DEBUG_LOG_SEC("result_mailbox_name[%s]", result_mailbox_name);
200                 if (result_mailbox_name == NULL)
201                         result_mailbox_name = EM_SAFE_STRDUP(mutf7_text);
202         }
203
204         EM_DEBUG_LOG_SEC("result_mailbox_name[%s]", result_mailbox_name);
205
206         if (cd)
207                 g_mime_iconv_close(cd);
208
209         EM_SAFE_FREE(mutf7_text);
210
211         EM_DEBUG_FUNC_END("result_mailbox_name[%p]", result_mailbox_name);
212         return result_mailbox_name;
213 }
214
215 /*  in smtp case, path argument must be ENCODED_PATH_SMTP */
216 int emcore_get_long_encoded_path_with_account_info(char *multi_user_name, email_account_t *account, char *path, int delimiter, char **long_enc_path, int *err_code)
217 {
218         EM_PROFILE_BEGIN(emCorelongEncodedpath);
219         EM_DEBUG_FUNC_BEGIN_SEC("account[%p], path[%s], delimiter[%d], long_enc_path[%p], err_code[%p]", account, path, delimiter, long_enc_path, err_code);
220
221         int ret = false;
222         int error = EMAIL_ERROR_NONE;
223         char *p = NULL;
224         email_authentication_method_t authentication_method = 0;
225
226         size_t long_enc_path_len = 0;
227
228         if (path == NULL || (path && strncmp(path, ENCODED_PATH_SMTP, EM_SAFE_STRLEN(ENCODED_PATH_SMTP)) != 0)) {               /*  imap or pop3 */
229                 EM_DEBUG_LOG_SEC("account->incoming_server_address[%p]", account->incoming_server_address);
230                 EM_DEBUG_LOG_SEC("account->incoming_server_address[%s]", account->incoming_server_address);
231
232                 if (!account->incoming_server_address) {
233                         EM_DEBUG_EXCEPTION("account->incoming_server_address is null");
234                         error = EMAIL_ERROR_INVALID_ACCOUNT;
235                         goto FINISH_OFF;
236                 }
237
238                 long_enc_path_len = EM_SAFE_STRLEN(account->incoming_server_address) + EM_SAFE_STRLEN(path) + 256; /*prevent 34357*/
239
240                 *long_enc_path = em_malloc(long_enc_path_len);
241                 if (!*long_enc_path)  {
242                         EM_DEBUG_EXCEPTION("malloc failed...");
243                         error = EMAIL_ERROR_OUT_OF_MEMORY;
244                         goto FINISH_OFF;
245                 }
246
247                 p = *long_enc_path;
248
249                 /*  ex:"{mai.test.com:143/imap} or {mai.test.com:143/imap/tls}my-mailbox" */
250
251         if (multi_user_name) {
252             SNPRINTF(p, long_enc_path_len, "{%s:%d/%s/user=%d%s%s",
253                 account->incoming_server_address,
254                 account->incoming_server_port_number,
255                 account->incoming_server_type == EMAIL_SERVER_TYPE_POP3 ? "pop3" : "imap", 
256                 account->account_id, 
257                 TOKEN_FOR_MULTI_USER, 
258                 multi_user_name);
259         } else {
260             SNPRINTF(p, long_enc_path_len, "{%s:%d/%s/user=%d",
261                 account->incoming_server_address,
262                 account->incoming_server_port_number,
263                 account->incoming_server_type == EMAIL_SERVER_TYPE_POP3 ? "pop3" : "imap", 
264                 account->account_id);
265         }
266
267                 if (account->incoming_server_secure_connection & 0x01)  {
268                         strncat(p, "/ssl", long_enc_path_len-(EM_SAFE_STRLEN(p)+1));
269                         /* strcat(p, "/tryssl"); */
270                 } else if (account->incoming_server_secure_connection & 0x02) {
271                         strncat(p, "/tls", long_enc_path_len-(EM_SAFE_STRLEN(p)+1));
272                 }
273
274                 authentication_method = account->incoming_server_authentication_method;
275
276                 if (account->incoming_server_requires_apop) {
277                         strncat(p, "/apop", long_enc_path_len-(EM_SAFE_STRLEN(p)+1));
278                 }
279         }
280         else  {         /*  smtp */
281                 long_enc_path_len = EM_SAFE_STRLEN(account->outgoing_server_address) + 64;
282
283                 *long_enc_path = em_malloc(EM_SAFE_STRLEN(account->outgoing_server_address) + 64);
284                 if (!*long_enc_path) {
285                         EM_DEBUG_EXCEPTION("\t malloc failed...\n");
286
287                         error = EMAIL_ERROR_OUT_OF_MEMORY;
288                         goto FINISH_OFF;
289                 }
290
291                 p = *long_enc_path;
292
293                 /*  ex:"mail.test.com:25/smtp" */
294
295                 SNPRINTF(p, long_enc_path_len, "%s:%d/%s",
296                         account->outgoing_server_address,
297                         account->outgoing_server_port_number,
298                         "smtp");
299
300                 if (account->outgoing_server_need_authentication > 0) {
301             if (multi_user_name) {
302                             SNPRINTF(p + EM_SAFE_STRLEN(p), long_enc_path_len-(EM_SAFE_STRLEN(p)), "/user=%d%s%s", 
303                                                                                         account->account_id, 
304                                                                                         TOKEN_FOR_MULTI_USER, 
305                                                                                         multi_user_name);
306             } else {
307                             SNPRINTF(p + EM_SAFE_STRLEN(p), long_enc_path_len-(EM_SAFE_STRLEN(p)), "/user=%d", 
308                                                                                         account->account_id);
309             }
310
311                         if (account->outgoing_server_need_authentication == EMAIL_AUTHENTICATION_METHOD_XOAUTH2)  {
312                                 strncat(p, "/xoauth2", long_enc_path_len-(EM_SAFE_STRLEN(p)+1));
313                         }
314                 }
315
316                 if (account->outgoing_server_secure_connection & 0x01) {
317                         strncat(p, "/ssl/force_tls_v1_0", long_enc_path_len-(EM_SAFE_STRLEN(p)+1));
318                         /* strcat(p, "/tryssl"); */
319                 }
320                 else if (account->outgoing_server_secure_connection & 0x02)
321                         strncat(p, "/tls/force_tls_v1_0", long_enc_path_len-(EM_SAFE_STRLEN(p)+1));
322                 else
323                         strncat(p, "/notls", long_enc_path_len-(EM_SAFE_STRLEN(p)+1));
324
325                 authentication_method = account->outgoing_server_need_authentication;
326         }
327
328 #ifdef FEATURE_CORE_DEBUG
329         strncat(p, "/debug", long_enc_path_len-(strlen("/debug")+1));
330 #endif
331
332         /* Authentication method */
333         switch(authentication_method) {
334         case EMAIL_AUTHENTICATION_METHOD_DEFAULT:
335                 strncat(p, "/needauth", long_enc_path_len-(EM_SAFE_STRLEN(p)+1));
336                 break;
337         case EMAIL_AUTHENTICATION_METHOD_XOAUTH2:
338                 strncat(p, "/xoauth2", long_enc_path_len-(EM_SAFE_STRLEN(p)+1));
339                 break;
340         case EMAIL_AUTHENTICATION_METHOD_NO_AUTH:
341         default:
342                 break;
343         }
344
345         if (path == NULL || (path && strncmp(path, ENCODED_PATH_SMTP, EM_SAFE_STRLEN(ENCODED_PATH_SMTP)) != 0)) {
346                 strncat(p, "}", long_enc_path_len-(EM_SAFE_STRLEN(p)+1));
347
348                 if (path != NULL) {
349                         strncat(p, path, long_enc_path_len-(EM_SAFE_STRLEN(p)+1));
350                 }
351         }
352
353         if(*long_enc_path)
354             EM_DEBUG_LOG_SEC("long_enc_path [%s]", *long_enc_path);
355
356         ret = true;
357
358 FINISH_OFF:
359         if (ret != true)
360                 EM_SAFE_FREE(p);
361
362         if (err_code != NULL)
363                 *err_code = error;
364         EM_PROFILE_END(emCorelongEncodedpath);
365         return ret;
366 }
367
368 int emcore_get_long_encoded_path(char *multi_user_name, int account_id, char *path, int delimiter, char **long_enc_path, int *err_code)
369 {
370         EM_PROFILE_BEGIN(emCorelongEncodedpath);
371         EM_DEBUG_FUNC_BEGIN("account_id[%d], delimiter[%d], long_enc_path[%p], err_code[%p]", account_id, delimiter, long_enc_path, err_code);
372
373         int ret = false;
374         int error = EMAIL_ERROR_NONE;
375         email_account_t *ref_account = NULL;
376
377         ref_account = emcore_get_account_reference(multi_user_name, account_id, false);
378         if (!ref_account)  {
379                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
380                 error = EMAIL_ERROR_INVALID_ACCOUNT;
381                 goto FINISH_OFF;
382         }
383
384         if (emcore_get_long_encoded_path_with_account_info(multi_user_name, ref_account, path, delimiter, long_enc_path, &error) == false) {
385                 EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path_with_account_info failed [%d]", error);
386                 goto FINISH_OFF;
387         }
388
389         ret = true;
390
391 FINISH_OFF:
392
393         if (ref_account) {
394                 emcore_free_account(ref_account);
395                 EM_SAFE_FREE(ref_account);
396         }
397
398         if (err_code != NULL)
399                 *err_code = error;
400         EM_PROFILE_END(emCorelongEncodedpath);
401         return ret;
402 }
403
404 int emcore_get_encoded_mailbox_name(char *name, char **enc_name, int *err_code)
405 {
406         EM_DEBUG_FUNC_BEGIN_SEC("name[%s], enc_name[%p], err_code[%p]", name, enc_name, err_code);
407
408         if (!name || !enc_name)  {
409                 if (err_code != NULL)
410                         *err_code = EMAIL_ERROR_INVALID_PARAM;
411                 EM_DEBUG_FUNC_END();
412                 return false;
413         }
414
415         /* encoding mailbox name (Charset->UTF8->UTF7) */
416         char *last_slash = NULL;
417         char *last_word = NULL;
418         char *last_enc_word = NULL;
419         char *ret_enc_name = NULL;
420         int err = EMAIL_ERROR_NONE;
421         int ret = true;
422
423         last_slash = rindex(name, '/');
424         last_word = (last_slash)? last_slash+1 : name;
425         if (!last_word) {
426                 EM_DEBUG_EXCEPTION("Wrong mailbox [%s]", name);
427                 err = EMAIL_ERROR_INVALID_PARAM;
428                 ret = false;
429                 goto FINISH_OFF;
430         }
431
432         /* convert the last mailbox folder to utf7 char */
433         last_enc_word = (char*)utf8_to_mutf7 ((unsigned char*)last_word);
434         if (!last_enc_word) {
435                 EM_DEBUG_EXCEPTION("utf8_to_mutf7 failed");
436                 err = EMAIL_ERROR_INVALID_PARAM;
437                 ret = false;
438                 goto FINISH_OFF;
439         }
440
441         /* if last word of mailbox name is ASCII, use input */
442         if (!strcmp(last_enc_word, last_word)) {
443                 EM_DEBUG_LOG_SEC("NOT UTF-8 [%s]", last_word);
444                 ret_enc_name = strdup(name);
445                 goto FINISH_OFF;
446         }
447
448         EM_DEBUG_LOG_SEC("UTF-7 [%s](%d)->[%s](%d)",last_word, strlen(last_word), last_enc_word, strlen(last_enc_word));
449
450         /* if last word of mailbox name is UTF-8, replace it */
451         /* it is not a subfolder */
452         if (!last_slash) {
453                 ret_enc_name = strdup(last_enc_word);
454         }
455         else { /* it is a subfolder */
456                 /*temprarily NULL assigned*/
457                 *last_slash = '\0';
458                 int len = strlen(name) + 1 + strlen(last_enc_word); /* including '/' */
459                 ret_enc_name = em_malloc(len + 1); /* NULL */
460                 if (!ret_enc_name) {
461                         EM_DEBUG_EXCEPTION("malloc failed...");
462                         err = EMAIL_ERROR_OUT_OF_MEMORY;
463                         ret = false;
464                         goto FINISH_OFF;
465                 }
466
467                 snprintf(ret_enc_name, len+1, "%s/%s", name, last_enc_word);
468                 *last_slash = '/';
469         }
470
471         EM_DEBUG_LOG_SEC("utf-7 encoding complete!! name[%s], enc_name[%s]", name, ret_enc_name);
472
473 FINISH_OFF:
474         EM_SAFE_FREE(last_enc_word);
475         *enc_name = ret_enc_name;
476         if (err_code != NULL)
477                 *err_code = err;
478
479         EM_DEBUG_FUNC_END("result [%s]", *enc_name);
480         return ret;
481 }
482
483 int emcore_get_temp_file_name(char **filename, int *err_code)
484 {
485         EM_DEBUG_FUNC_BEGIN("filename[%p], err_code[%p]", filename, err_code);
486
487         int ret = false;
488         int error = EMAIL_ERROR_NONE;
489
490         if (filename == NULL) {
491                 EM_DEBUG_EXCEPTION("\t filename[%p]\n", filename);
492                 error = EMAIL_ERROR_INVALID_PARAM;
493                 goto FINISH_OFF;
494         }
495
496         char tempname[512] = {0x00, };
497         struct timeval tv;
498
499
500         gettimeofday(&tv, NULL);
501         srand(tv.tv_usec);
502
503         /* Create Directory If deleted by user*/
504         emstorage_create_dir_if_delete();
505
506         SNPRINTF(tempname, sizeof(tempname), "%s%c%d", MAILTEMP, DIR_SEPERATOR_CH, rand());
507
508         char *p = EM_SAFE_STRDUP(tempname);
509         if (p == NULL) {
510                 EM_DEBUG_EXCEPTION("\t strdup failed...\n");
511                 error = EMAIL_ERROR_OUT_OF_MEMORY;
512                 goto FINISH_OFF;
513         }
514
515         *filename = p;
516
517         ret = true;
518
519 FINISH_OFF:
520         if (err_code != NULL)
521                 *err_code = error;
522         EM_DEBUG_FUNC_END();
523         return ret;
524 }
525
526 int emcore_get_file_name(char *path, char **filename, int *err_code)
527 {
528         EM_DEBUG_FUNC_BEGIN_SEC("path[%s], filename[%p], err_code[%p]", path, filename, err_code);
529
530         int ret = false;
531         int error = EMAIL_ERROR_NONE;
532
533         if (!path || !filename) {
534                 EM_DEBUG_EXCEPTION("path[%p], filename[%p]", path, filename);
535
536                 error = EMAIL_ERROR_INVALID_PARAM;
537                 goto FINISH_OFF;
538         }
539
540         int i = (int)EM_SAFE_STRLEN(path);
541
542         /*  get filename */
543         for (; i >= 0; i--)
544                 if (path[i] == DIR_SEPERATOR_CH)
545                         break;
546
547         *filename = path + i + 1;
548
549         ret = true;
550
551 FINISH_OFF:
552                 if (err_code != NULL)
553                 *err_code = error;
554         EM_DEBUG_FUNC_END();
555         return ret;
556 }
557
558 INTERNAL_FUNC int emcore_get_file_size(char *path, int *size, int *err_code)
559 {
560         EM_DEBUG_FUNC_BEGIN("path[%s], size[%p], err_code[%p]", path, size, err_code);
561
562         int ret = false;
563         int error = EMAIL_ERROR_NONE;
564
565         if ((path == NULL) || (size == NULL)) {
566                 EM_DEBUG_EXCEPTION("\t path[%p], size[%p]\n", path, size);
567
568                 error = EMAIL_ERROR_INVALID_PARAM;
569                 goto FINISH_OFF;
570         }
571
572         struct stat st_buf;
573
574         if (stat(path, &st_buf) < 0)  {
575                 EM_DEBUG_EXCEPTION("\t stat failed - %s\n", path);
576
577                 error = EMAIL_ERROR_SYSTEM_FAILURE;
578                 goto FINISH_OFF;
579         }
580
581         *size = st_buf.st_size;
582
583         ret = true;
584
585 FINISH_OFF:
586         if (err_code != NULL)
587                 *err_code = error;
588         EM_DEBUG_FUNC_END();
589         return ret;
590 }
591
592 INTERNAL_FUNC int emcore_check_drm_file(char *path, int *err_code)
593 {
594         EM_DEBUG_FUNC_BEGIN("path[%s], err_code[%p]", path, err_code);
595
596         int ret = false;
597         int error = EMAIL_ERROR_NONE;
598
599         if (path == NULL) {
600                 EM_DEBUG_EXCEPTION("path[%p]", path);
601                 error = EMAIL_ERROR_INVALID_PARAM;
602                 goto FINISH_OFF;
603         }
604
605 #ifdef __FEATURE_USE_DRM_API__
606         int drm_ret = false;
607         drm_bool_type_e isdrm;
608         drm_ret = drm_is_drm_file(path, &isdrm);
609         if (drm_ret != DRM_RETURN_SUCCESS || isdrm != DRM_TRUE) {
610                 EM_DEBUG_LOG("file is not drm file");
611                 error = EMAIL_ERROR_SYSTEM_FAILURE;
612                 goto FINISH_OFF;
613         }
614
615         ret = true;
616 #endif /* __FEATURE_USE_DRM_API__ */
617
618 FINISH_OFF:
619
620         if (err_code != NULL)
621                 *err_code = error;
622
623         EM_DEBUG_FUNC_END();
624         return ret;
625 }
626
627 INTERNAL_FUNC int emcore_check_drm_is_ringtone(char *ringtone_path, int *err_code)
628 {
629         EM_DEBUG_FUNC_BEGIN("ringtone_path[%s], err_code[%p]", ringtone_path, err_code);
630
631         int ret = false;
632         int error = EMAIL_ERROR_NONE;
633
634         if (ringtone_path == NULL) {
635                 EM_DEBUG_EXCEPTION("path[%p]", ringtone_path);
636                 error = EMAIL_ERROR_INVALID_PARAM;
637                 goto FINISH_OFF;
638         }
639
640 #ifdef __FEATURE_USE_DRM_API__
641         int drm_ret = false;
642         drm_bool_type_e allowed = DRM_UNKNOWN;
643         drm_action_allowed_data_s data;
644         memset(&data, 0x00, sizeof(drm_action_allowed_data_s));
645         strncpy(data.file_path, ringtone_path, strlen(ringtone_path));
646         data.data = (int)DRM_SETAS_RINGTONE;
647
648         drm_ret = drm_is_action_allowed(DRM_HAS_VALID_SETAS_STATUS, &data, &allowed);
649
650         if (drm_ret != DRM_RETURN_SUCCESS || allowed != DRM_TRUE) {
651                 EM_DEBUG_LOG("fail to drm_is_action_allowed [0x%x]", drm_ret);
652                 error = EMAIL_ERROR_SYSTEM_FAILURE;
653                 goto FINISH_OFF;
654         }
655
656         EM_DEBUG_LOG("allowed [DRM_TRUE]");
657         ret = true;
658 #endif /* __FEATURE_USE_DRM_API__ */
659
660 FINISH_OFF:
661
662         if (err_code != NULL)
663                 *err_code = error;
664
665         EM_DEBUG_FUNC_END();
666         return ret;
667 }
668
669 static int _emcore_check_host(char *host)
670 {
671         if (!host)
672                 return 0;
673         return strncmp(host, ".SYNTAX-ERROR.", strlen(".SYNTAX-ERROR."));
674 }
675
676
677
678 int emcore_get_address_count(char *addr_str, int *count, int *err_code)
679 {
680         EM_DEBUG_FUNC_BEGIN("addr_str[%s], count[%p], err_code[%p]", addr_str, count, err_code);
681
682         int ret = false;
683         int error = EMAIL_ERROR_NONE;
684
685         ADDRESS *addr = NULL;
686         ADDRESS *p_addr = NULL;
687         int i = 0, j;
688         char *p = NULL;
689
690
691         if (!count)  {
692                 EM_DEBUG_EXCEPTION("addr_str[%s], count[%p]", addr_str, count);
693                 error = EMAIL_ERROR_INVALID_PARAM;
694                 goto FINISH_OFF;
695         }
696
697         if (addr_str != NULL)  {
698                 em_skip_whitespace(addr_str, &p);
699                 EM_DEBUG_LOG("em_skip_whitespace[p][%s]", p);
700
701
702                 for (i = 0, j = EM_SAFE_STRLEN(p); i < j; i++)
703                         if (p[i] == ';') p[i] = ',';
704                 rfc822_parse_adrlist(&addr, p, NULL);
705                 EM_SAFE_FREE(p);
706
707
708                 for (p_addr = addr, i = 0; p_addr; p_addr = p_addr->next, i++)  {
709                         if (p_addr->mailbox && p_addr->host) {
710                                 if (!strncmp(p_addr->mailbox, "UNEXPECTED_DATA_AFTER_ADDRESS", strlen("UNEXPECTED_DATA_AFTER_ADDRESS"))
711                                 || !strncmp(p_addr->mailbox, "INVALID_ADDRESS", strlen("INVALID_ADDRESS"))
712                                 || !strncmp(p_addr->host, ".SYNTAX-ERROR.", strlen(".SYNTAX-ERROR."))) { /*prevent 34356*/
713                                         EM_DEBUG_LOG("Invalid address ");
714                                         continue;
715                                 }
716                         }
717                         if ((!p_addr->mailbox) || (_emcore_check_host(p_addr->host) == 0)) {
718                                 EM_DEBUG_EXCEPTION_SEC("\t invalid address : mailbox[%s], host[%s]\n", p_addr->mailbox, p_addr->host);
719
720                                 error = EMAIL_ERROR_INVALID_ADDRESS;
721                                 /* goto FINISH_OFF; */
722                         }
723                 }
724         }
725
726         *count = i;
727         if (error != EMAIL_ERROR_INVALID_ADDRESS)
728         ret = true;
729
730 FINISH_OFF:
731         if (addr)
732                 mail_free_address(&addr);
733
734         if (err_code != NULL)
735                 *err_code = error;
736         EM_DEBUG_FUNC_END();
737         return ret;
738 }
739
740 INTERNAL_FUNC int emcore_set_network_error(int err_code)
741 {
742         email_session_t *session = NULL;
743
744         EM_DEBUG_FUNC_BEGIN();
745
746         emcore_get_current_session(&session);
747
748         if (!session)
749                 return false;
750
751         session->network = err_code;
752         EM_DEBUG_FUNC_END();
753         return true;
754 }
755
756 INTERNAL_FUNC int emcore_get_empty_session(email_session_t **session)
757 {
758         EM_DEBUG_FUNC_BEGIN("session[%p]", session);
759
760         *session = &g_session;
761
762         EM_DEBUG_FUNC_END();
763         return true;
764 }
765
766 INTERNAL_FUNC int emcore_clear_session(email_session_t *session)
767 {
768         EM_DEBUG_FUNC_BEGIN();
769
770         if (session)
771                 memset (session, 0x00, sizeof(email_session_t));
772
773         EM_DEBUG_FUNC_END();
774         return true;
775 }
776
777 INTERNAL_FUNC int emcore_get_current_session(email_session_t **session)
778 {
779         EM_DEBUG_FUNC_BEGIN("session[%p]", session);
780
781         *session = &g_session;
782
783         EM_DEBUG_FUNC_END();
784         return true;
785 }
786
787 INTERNAL_FUNC int emcore_get_mail_count_by_query(char *multi_user_name, int account_id, int mailbox_type, int priority_sender, int *total_mail, int *unread_mail, int *err_code)
788 {
789         EM_DEBUG_FUNC_BEGIN("account_id : [%d], mailbox_type : [%d], priority_sender : [%d]", account_id, mailbox_type, priority_sender);
790
791         int ret = false;
792         int err = EMAIL_ERROR_NONE;
793         int i = 0;
794         int type = EMAIL_PRIORITY_SENDER;
795         int unread_count = 0;
796         int total_count = 0;
797         char *conditional_clause_string = NULL;
798
799         int rule_count = 0;
800         int is_completed = 0;
801         emstorage_rule_tbl_t *rule = NULL;
802
803         int filter_count = 0;
804         email_list_filter_t *filter_list = NULL;
805
806         /* Get rule list */
807         if (priority_sender) {
808                 if (!emstorage_get_rule(multi_user_name, ALL_ACCOUNT, type, 0, &rule_count, &is_completed, &rule, true, &err)) {
809                         EM_DEBUG_EXCEPTION("emstorage_get_rule failed");
810                         goto FINISH_OFF;
811                 }
812                 if (err == EMAIL_ERROR_FILTER_NOT_FOUND) {
813                         EM_DEBUG_LOG("no rule found");
814                         ret = true;             /*it is not an error*/
815                         goto FINISH_OFF;
816                 }
817         }
818
819         EM_DEBUG_LOG_DEV ("rule count [%d]", rule_count);
820
821         /* Make query for searching unread mail */
822         filter_count = 3;   /* unseen field requires one filter, "flags_seen_field = 0"
823                                 and "deleted_flag = 0" */
824
825         if (rule_count > 0 )
826                 filter_count += (rule_count * 2) + 2; /* one rule requires two filters,"A" "OR", plus two more operator "(" and ")" */
827
828
829         if (account_id != ALL_ACCOUNT) {
830                 filter_count += 2; /* two filters, "AND" "account_id = x" */
831         }
832
833         if (mailbox_type)
834                 filter_count += 2; /* two filters, "AND" "mailbox_type = x" */
835
836         filter_list = em_malloc(sizeof(email_list_filter_t) * filter_count);
837         if (filter_list == NULL) {
838                 EM_DEBUG_EXCEPTION("em_malloc failed");
839                 err = EMAIL_ERROR_OUT_OF_MEMORY;
840                 goto FINISH_OFF;
841         }
842
843         int k = 0;
844
845         /* priority sender only, convert one rule to two filters which is composed of from_address and or/and */
846         /* example: ( from_A OR from_B ) AND ... */
847         if (rule_count > 0) {
848                 /* first, add left parenthesis to from address clause, "(" */
849                 filter_list[0].list_filter_item_type                   = EMAIL_LIST_FILTER_ITEM_OPERATOR;
850                 filter_list[0].list_filter_item.operator_type          = EMAIL_LIST_FILTER_OPERATOR_LEFT_PARENTHESIS;
851
852                 for (i = 0; i < rule_count; i++) {
853                         /*array at odd index(1, 3, 5,..) has rule values, "from_A" */
854                         k = i*2+1;
855                         filter_list[k].list_filter_item_type                             = EMAIL_LIST_FILTER_ITEM_RULE;
856                         filter_list[k].list_filter_item.rule.rule_type                   = EMAIL_LIST_FILTER_RULE_INCLUDE;
857                         filter_list[k].list_filter_item.rule.target_attribute            = EMAIL_MAIL_ATTRIBUTE_FROM;
858                         filter_list[k].list_filter_item.rule.key_value.string_type_value = EM_SAFE_STRDUP(rule[i].value2);
859
860                         /*array at even index(2, 4, 6,..) has either AND ,or OR*/
861                         if (i != (rule_count - 1)) { /*if it is not the last rule, "OR" */
862                                 filter_list[++k].list_filter_item_type                 = EMAIL_LIST_FILTER_ITEM_OPERATOR;
863                                 filter_list[k].list_filter_item.operator_type          = EMAIL_LIST_FILTER_OPERATOR_OR;
864
865                         } else { /* ")" "AND" */
866                                 filter_list[++k].list_filter_item_type         = EMAIL_LIST_FILTER_ITEM_OPERATOR;
867                                 filter_list[k].list_filter_item.operator_type  = EMAIL_LIST_FILTER_OPERATOR_RIGHT_PARENTHESIS;
868
869                                 filter_list[++k].list_filter_item_type         = EMAIL_LIST_FILTER_ITEM_OPERATOR;
870                                 filter_list[k].list_filter_item.operator_type  = EMAIL_LIST_FILTER_OPERATOR_AND;
871                                 k++;
872                         }
873                 }
874         }
875
876         /*rule value, unseen +1*/
877         filter_list[k].list_filter_item_type                                = EMAIL_LIST_FILTER_ITEM_RULE;
878         filter_list[k].list_filter_item.rule.rule_type                      = EMAIL_LIST_FILTER_RULE_EQUAL;
879         filter_list[k].list_filter_item.rule.target_attribute               = EMAIL_MAIL_ATTRIBUTE_FLAGS_SEEN_FIELD;
880         filter_list[k++].list_filter_item.rule.key_value.integer_type_value = 0;
881
882         filter_list[k].list_filter_item_type                                = EMAIL_LIST_FILTER_ITEM_OPERATOR;
883         filter_list[k++].list_filter_item.operator_type                     = EMAIL_LIST_FILTER_OPERATOR_AND;
884
885         filter_list[k].list_filter_item_type                                = EMAIL_LIST_FILTER_ITEM_RULE;
886         filter_list[k].list_filter_item.rule.rule_type                      = EMAIL_LIST_FILTER_RULE_NOT_EQUAL;
887         filter_list[k].list_filter_item.rule.target_attribute               = EMAIL_MAIL_ATTRIBUTE_SAVE_STATUS;
888         filter_list[k++].list_filter_item.rule.key_value.integer_type_value = EMAIL_MAIL_STATUS_SAVED_OFFLINE;
889
890         /*account_id requires two filters*/
891         if (account_id != ALL_ACCOUNT) {
892                 /* odd index, logical operator */
893                 filter_list[k].list_filter_item_type                              = EMAIL_LIST_FILTER_ITEM_OPERATOR;
894                 filter_list[k++].list_filter_item.operator_type                   = EMAIL_LIST_FILTER_OPERATOR_AND;
895
896                 /* even index, rule value */
897                 filter_list[k].list_filter_item_type                              = EMAIL_LIST_FILTER_ITEM_RULE;
898                 filter_list[k].list_filter_item.rule.rule_type                    = EMAIL_LIST_FILTER_RULE_EQUAL;
899                 filter_list[k].list_filter_item.rule.target_attribute             = EMAIL_MAIL_ATTRIBUTE_ACCOUNT_ID;
900                 filter_list[k++].list_filter_item.rule.key_value.integer_type_value = account_id;
901         }
902
903         /*mailbox_type requires two filters*/
904         if (mailbox_type) {
905                 /* odd index, logical operator */
906                 filter_list[k].list_filter_item_type                              = EMAIL_LIST_FILTER_ITEM_OPERATOR;
907                 filter_list[k++].list_filter_item.operator_type                   = EMAIL_LIST_FILTER_OPERATOR_AND;
908
909                 /* even index, rule value */
910                 filter_list[k].list_filter_item_type                              = EMAIL_LIST_FILTER_ITEM_RULE;
911                 filter_list[k].list_filter_item.rule.rule_type                    = EMAIL_LIST_FILTER_RULE_EQUAL;
912                 filter_list[k].list_filter_item.rule.target_attribute             = EMAIL_MAIL_ATTRIBUTE_MAILBOX_TYPE;
913                 filter_list[k++].list_filter_item.rule.key_value.integer_type_value = mailbox_type;
914         }
915
916         if ((err = emstorage_write_conditional_clause_for_getting_mail_list(multi_user_name, filter_list, filter_count, NULL, 0, -1, -1, &conditional_clause_string)) != EMAIL_ERROR_NONE) {
917                 EM_DEBUG_EXCEPTION("emstorage_write_conditional_clause_for_getting_mail_list failed[%d]", err);
918                 goto FINISH_OFF;
919         }
920
921         EM_DEBUG_LOG_DEV ("conditional_clause_string[%s]", conditional_clause_string);
922
923         /* Search the mail of priority sender in DB */
924         if ((err = emstorage_query_mail_count(multi_user_name, conditional_clause_string, true, &total_count, &unread_count)) != EMAIL_ERROR_NONE) {
925                 EM_DEBUG_EXCEPTION("emstorage_query_mail_count failed:[%d]", err);
926                 goto FINISH_OFF;
927         }
928
929         ret = true;
930
931 FINISH_OFF:
932
933         if (rule)
934                 emstorage_free_rule(&rule, rule_count, NULL);
935
936         if (filter_list)
937                 emstorage_free_list_filter(&filter_list, filter_count);
938
939         EM_SAFE_FREE(conditional_clause_string);
940
941         if (total_mail)
942                 *total_mail = total_count;
943
944         if (unread_mail)
945                 *unread_mail = unread_count;
946
947         if (err_code)
948                 *err_code = err;
949
950         return ret;
951 }
952
953 INTERNAL_FUNC int emcore_display_badge_count(char *multi_user_name, int count)
954 {
955         EM_DEBUG_FUNC_BEGIN();
956         /* Use badge API */
957         int err = EMAIL_ERROR_NONE;
958         badge_error_e badge_err = BADGE_ERROR_NONE;
959         bool exist;
960         void *join_zone = NULL;
961
962         if ((err = emcore_set_join_zone(multi_user_name, &join_zone)) != EMAIL_ERROR_NONE) {
963                 EM_DEBUG_EXCEPTION("emcore_set_join_zone failed : [%d]", err);
964                 goto FINISH_OFF;
965         }
966
967         if((badge_err = badge_is_existing("org.tizen.email", &exist)) != BADGE_ERROR_NONE) {
968                 EM_DEBUG_EXCEPTION("badge_is_existing failed [%d]", badge_err);
969                 err = EMAIL_ERROR_BADGE_API_FAILED;
970                 goto FINISH_OFF;
971         }
972
973         if (!exist) {
974                 /* create badge */
975                 if((badge_err = badge_create("org.tizen.email", "/usr/bin/email-service")) != BADGE_ERROR_NONE) {
976                         EM_DEBUG_EXCEPTION("badge_create failed [%d]", badge_err);
977                         err = EMAIL_ERROR_BADGE_API_FAILED;
978                         goto FINISH_OFF;
979                 }
980         }
981
982         if((badge_err = badge_set_count("org.tizen.email", count)) != BADGE_ERROR_NONE) {
983                 EM_DEBUG_EXCEPTION("badge_set_count failed [%d]", badge_err);
984                 if (badge_err != BADGE_ERROR_SERVICE_NOT_READY) {
985                         err = EMAIL_ERROR_BADGE_API_FAILED;
986                         goto FINISH_OFF;
987                 }
988
989                 /* Badge callback function : When the badge service ready, call the callback function */
990                 badge_err = badge_add_deferred_task(emcore_display_unread_in_badge, NULL);
991                 if (badge_err != BADGE_ERROR_NONE) {
992                         EM_DEBUG_EXCEPTION("badge_add_deferred_task failed : [%d]", badge_err);
993                         err = EMAIL_ERROR_BADGE_API_FAILED;
994                         goto FINISH_OFF;
995                 }
996         }
997
998 FINISH_OFF:
999
1000         if (join_zone)
1001                 emcore_unset_join_zone(join_zone);
1002
1003         EM_DEBUG_FUNC_END();
1004         return err;
1005 }
1006
1007 void emcore_display_unread_in_badge(void *data)
1008 {
1009         EM_DEBUG_FUNC_BEGIN();
1010
1011         int err = EMAIL_ERROR_NONE;
1012         int total_unread_count = 0;
1013         int total_mail_count = 0;
1014         int unseen = 0;
1015
1016         if (!emcore_get_mail_count_by_query((char *)data, ALL_ACCOUNT, 
1017                                                                                 EMAIL_MAILBOX_TYPE_INBOX, 0, 
1018                                                                                 &total_mail_count, &total_unread_count, 
1019                                                                                 &err)) {
1020                 EM_DEBUG_EXCEPTION("emcore_get_mail_count_by_query failed");
1021                 goto FINISH_OFF;
1022         }
1023
1024         unseen = total_unread_count;
1025
1026 FINISH_OFF:
1027
1028         if (unseen <= 0) {
1029                 if ((err = emcore_display_badge_count((char *)data, 0)) != EMAIL_ERROR_NONE) {
1030                         EM_DEBUG_EXCEPTION("emcore_display_badge_count failed : [%d]", err);
1031                 }
1032         } else {
1033                 if ((err = emcore_display_badge_count((char *)data, unseen)) != EMAIL_ERROR_NONE) {
1034                         EM_DEBUG_EXCEPTION("emcore_display_badge_count failed : [%d]", err);
1035                 }
1036         }
1037
1038         EM_DEBUG_FUNC_END();
1039 }
1040
1041 static int emcore_layout_multi_noti(notification_h noti, int unread_mail, char *email_address, char *account_name)
1042 {
1043         EM_DEBUG_FUNC_BEGIN("unread_count %d", unread_mail);
1044         char temp_buffer[512] = {0};
1045         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
1046
1047         noti_err = notification_set_layout(noti, NOTIFICATION_LY_NOTI_EVENT_MULTIPLE);
1048         if (noti_err != NOTIFICATION_ERROR_NONE) {
1049                 EM_DEBUG_EXCEPTION("notification_set_layout NOTI_EVENT_MULTIPLE failed [%d]", noti_err);
1050                 goto FINISH_OFF;
1051         }
1052
1053         noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_TITLE, "New emails", dgettext(NATIVE_EMAIL_DOMAIN,"IDS_EMAIL_MBODY_NEW_EMAILS_ABB"), NOTIFICATION_VARIABLE_TYPE_NONE);
1054         if (noti_err != NOTIFICATION_ERROR_NONE) {
1055                 EM_DEBUG_EXCEPTION("notification_set_text TEXT_TYPE_TITLE failed");
1056                 goto FINISH_OFF;
1057         }
1058
1059         SNPRINTF(temp_buffer, sizeof(temp_buffer), "%d", unread_mail);
1060         noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_EVENT_COUNT, temp_buffer, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
1061         if (noti_err != NOTIFICATION_ERROR_NONE) {
1062                 EM_DEBUG_EXCEPTION("notification_set_text TEXT_TYPE_EVENT_COUNT failed");
1063                 goto FINISH_OFF;
1064         }
1065
1066         if (account_name)
1067                 SNPRINTF(temp_buffer, sizeof(temp_buffer), "%s, %s", email_address, account_name);
1068         else
1069                 SNPRINTF(temp_buffer, sizeof(temp_buffer), "%s", email_address);
1070
1071         noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT, temp_buffer, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
1072         if (noti_err != NOTIFICATION_ERROR_NONE) {
1073                 EM_DEBUG_EXCEPTION("notification_set_text TEXT_TYPE_CONTENT failed");
1074                 goto FINISH_OFF;
1075         }
1076
1077         noti_err = notification_set_time(noti, time(NULL));
1078         if (noti_err != NOTIFICATION_ERROR_NONE) {
1079                 EM_DEBUG_EXCEPTION("notification_set_time failed");
1080                 goto FINISH_OFF;
1081         }
1082
1083         noti_err = notification_set_image(noti, NOTIFICATION_IMAGE_TYPE_ICON, EMAIL_NOTI_ICON_PATH);
1084         if (noti_err != NOTIFICATION_ERROR_NONE) {
1085                 EM_DEBUG_EXCEPTION("notification_set_image TYPE_ICON failed");
1086                 goto FINISH_OFF;
1087         }
1088
1089         noti_err = notification_set_image(noti, NOTIFICATION_IMAGE_TYPE_ICON_FOR_INDICATOR, EMAIL_NOTI_INDICATOR_ICON_PATH);
1090         if (noti_err != NOTIFICATION_ERROR_NONE) {
1091                 EM_DEBUG_EXCEPTION("notification_set_image TYPE_ICON failed");
1092                 goto FINISH_OFF;
1093         }
1094
1095         noti_err = notification_set_image(noti, NOTIFICATION_IMAGE_TYPE_ICON_FOR_LOCK, EMAIL_NOTI_ICON_PATH);
1096         if (noti_err != NOTIFICATION_ERROR_NONE) {
1097                 EM_DEBUG_EXCEPTION("notification_set_image TYPE_ICON failed");
1098                 goto FINISH_OFF;
1099         }
1100
1101 FINISH_OFF:
1102
1103         EM_DEBUG_FUNC_END("noti_err : [%d]", noti_err);
1104         return noti_err;
1105 }
1106
1107 static int emcore_layout_single_noti(notification_h noti, char *account_name, int ids, char *display_sender, time_t time, char *subject, int display_status)
1108 {
1109         EM_DEBUG_FUNC_BEGIN();
1110         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
1111
1112         noti_err = notification_set_layout(noti, NOTIFICATION_LY_NOTI_EVENT_SINGLE);
1113         if (noti_err != NOTIFICATION_ERROR_NONE) {
1114                 EM_DEBUG_EXCEPTION("notification_set_layout NOTI_EVENT_SINGLE failed [%d]", noti_err);
1115                 goto FINISH_OFF;
1116         }
1117
1118         if (ids)
1119                 noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_TITLE, "Email", dgettext(NATIVE_EMAIL_DOMAIN,"IDS_ST_HEADER_EMAIL"), NOTIFICATION_VARIABLE_TYPE_NONE);
1120         else
1121                 noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_TITLE, display_sender, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
1122
1123         if (noti_err != NOTIFICATION_ERROR_NONE) {
1124                 EM_DEBUG_EXCEPTION("notification_set_text TEXT_TYPE_TITLE failed");
1125                 goto FINISH_OFF;
1126         }
1127 /*
1128         noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT, account_name, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
1129         if (noti_err != NOTIFICATION_ERROR_NONE) {
1130                 EM_DEBUG_EXCEPTION("notification_set_text TEXT_TYPE_CONTENT failed");
1131                 goto FINISH_OFF;
1132         }
1133
1134         if (ids)
1135                 noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT, NULL, display_sender, NOTIFICATION_VARIABLE_TYPE_NONE);
1136         else
1137                 noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT, display_sender, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
1138
1139         if (noti_err != NOTIFICATION_ERROR_NONE) {
1140                 EM_DEBUG_EXCEPTION("notification_set_text TEXT_TYPE_CONTENT failed");
1141                 goto FINISH_OFF;
1142         }
1143 */
1144         if (display_status)
1145                 noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT, subject, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
1146         else
1147                 noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_1, "New email", dgettext(NATIVE_EMAIL_DOMAIN,"IDS_EMAIL_TPOP_NEW_EMAIL_RECEIVED_ABB"), NOTIFICATION_VARIABLE_TYPE_NONE);
1148
1149         if (noti_err != NOTIFICATION_ERROR_NONE) {
1150                 EM_DEBUG_EXCEPTION("notification_set_text TEXT_TYPE_INFO_1 failed");
1151                 goto FINISH_OFF;
1152         }
1153
1154         noti_err = notification_set_time(noti, time);
1155         if (noti_err != NOTIFICATION_ERROR_NONE) {
1156                 EM_DEBUG_EXCEPTION("notification_set_time failed");
1157                 goto FINISH_OFF;
1158         }
1159
1160         noti_err = notification_set_image(noti, NOTIFICATION_IMAGE_TYPE_ICON, EMAIL_NOTI_ICON_PATH);
1161         if (noti_err != NOTIFICATION_ERROR_NONE) {
1162                 EM_DEBUG_EXCEPTION("notification_set_image TYPE_ICON failed");
1163                 goto FINISH_OFF;
1164         }
1165
1166         noti_err = notification_set_image(noti, NOTIFICATION_IMAGE_TYPE_ICON_FOR_INDICATOR, EMAIL_NOTI_INDICATOR_ICON_PATH);
1167         if (noti_err != NOTIFICATION_ERROR_NONE) {
1168                 EM_DEBUG_EXCEPTION("notification_set_image TYPE_ICON failed");
1169                 goto FINISH_OFF;
1170         }
1171
1172         noti_err = notification_set_image(noti, NOTIFICATION_IMAGE_TYPE_ICON_FOR_LOCK, EMAIL_NOTI_ICON_PATH);
1173         if (noti_err != NOTIFICATION_ERROR_NONE) {
1174                 EM_DEBUG_EXCEPTION("notification_set_image TYPE_ICON failed");
1175                 goto FINISH_OFF;
1176         }
1177 FINISH_OFF:
1178
1179         EM_DEBUG_FUNC_END("noti_err : [%d]", noti_err);
1180         return noti_err;
1181 }
1182
1183 static int emcore_get_alert_type(int vibrate_status)
1184 {
1185         EM_DEBUG_FUNC_BEGIN();
1186         int global_sound_status = 0;
1187         int global_vibe_status = 0;
1188         int email_vibe_status = 0;
1189         int call_state = 0;
1190         int alert_type = EMAIL_ALERT_TYPE_MUTE;
1191 #ifdef __FEATURE_VOICERECORDER_STATUS_FOR_NOTI__
1192         int voicerecoder_state = 0;
1193 #endif /* __FEATURE_VOICERECORDER_STATUS_FOR_NOTI__ */
1194
1195         if (vconf_get_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, &global_sound_status) != 0) {
1196                 EM_DEBUG_LOG("vconf_get_bool for VCONFKEY_SETAPPL_SOUND_STATUS_BOOL failed");
1197                 goto FINISH_OFF;
1198         }
1199
1200         if (vconf_get_bool(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, &global_vibe_status) != 0) {
1201                 EM_DEBUG_LOG("vconf_get_bool for VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOLfailed");
1202                 goto FINISH_OFF;
1203         }
1204
1205         EM_DEBUG_LOG("global_sound_status [%d] global_vibe_status [%d]", global_sound_status, global_vibe_status);
1206
1207         if (global_sound_status || global_vibe_status) {
1208 #ifdef __FEATURE_VOICERECORDER_STATUS_FOR_NOTI__
1209                 if (vconf_get_int(VCONFKEY_VOICERECORDER_STATE, &voicerecoder_state) != 0) {
1210                         EM_DEBUG_LOG("vconf_get_int for VCONFKEY_VOICERECORDER_STATE failed");
1211                 }
1212                 EM_DEBUG_LOG("voicerecoder_state [%d]", voicerecoder_state);
1213 #endif /* __FEATURE_VOICERECORDER_STATUS_FOR_NOTI__ */
1214
1215                 if (vconf_get_int(VCONFKEY_CALL_STATE, &call_state) != 0) {
1216                         EM_DEBUG_LOG("vconf_get_int for VCONFKEY_CALL_STATE failed");
1217                 }
1218                 EM_DEBUG_LOG("call_state [%d] ", call_state);
1219
1220                 email_vibe_status = vibrate_status;
1221
1222                 EM_DEBUG_LOG("email_vibe_status [%d] ", email_vibe_status);
1223 #ifdef __FEATURE_VOICERECORDER_STATUS_FOR_NOTI__
1224                 if (voicerecoder_state == VCONFKEY_VOICERECORDER_RECORDING) {
1225                         alert_type = EMAIL_ALERT_TYPE_VIB;
1226                         EM_DEBUG_LOG("voice recorder is on recording...");
1227                 }
1228 /*              else if (call_state > VCONFKEY_CALL_OFF && call_state < VCONFKEY_CALL_STATE_MAX) { */
1229 #endif /* __FEATURE_VOICERECORDER_STATUS_FOR_NOTI__ */
1230
1231                 if (call_state > VCONFKEY_CALL_OFF && call_state < VCONFKEY_CALL_STATE_MAX) {
1232                         EM_DEBUG_LOG("Calling");
1233                         if (global_sound_status)
1234                                 alert_type = EMAIL_ALERT_TYPE_MELODY;
1235                         else
1236                                 alert_type = EMAIL_ALERT_TYPE_NONE;
1237                 }
1238                 else if (global_sound_status && email_vibe_status) {
1239                         alert_type = EMAIL_ALERT_TYPE_MELODY_AND_VIB;
1240                 }
1241                 else if (global_sound_status) {
1242                         alert_type = EMAIL_ALERT_TYPE_MELODY;
1243                 }
1244                 else if (global_vibe_status) {
1245                         alert_type = EMAIL_ALERT_TYPE_VIB;
1246                 }
1247         }
1248
1249 FINISH_OFF:
1250
1251         EM_DEBUG_FUNC_END("alert_type [%d]", alert_type);
1252         return alert_type;
1253 }
1254
1255 static char *emcore_get_sound_file_path(int default_ringtone_status, char *alert_ringtone_path)
1256 {
1257         EM_DEBUG_FUNC_BEGIN();
1258         char *ret = NULL;
1259         int   use_default_ring_tone = 0;
1260
1261         use_default_ring_tone = default_ringtone_status;
1262
1263         EM_DEBUG_LOG("use_default_ring_tone [%d]", use_default_ring_tone);
1264
1265         if (use_default_ring_tone)
1266                 ret = vconf_get_str(VCONFKEY_SETAPPL_NOTI_MSG_RINGTONE_PATH_STR);
1267         else
1268                 ret = EM_SAFE_STRDUP(alert_ringtone_path);
1269
1270         if ((emcore_check_drm_file(ret, NULL)) && (!emcore_check_drm_is_ringtone(ret, NULL))) {
1271                 EM_DEBUG_LOG_DEV("The ringtone is not allowed DRM. : [%s]", ret);
1272                 EM_SAFE_FREE(ret);
1273                 ret = EM_SAFE_STRDUP(vconf_get_str(VCONFKEY_SETAPPL_NOTI_MSG_RINGTONE_PATH_STR));
1274         }
1275
1276         EM_DEBUG_FUNC_END("ret [%s]", ret);
1277         return ret;
1278 }
1279
1280 #if 0 //using notification_status_message_post instead of calling email-app toast - change date: 30/9/2014
1281 INTERNAL_FUNC int emcore_show_toast_popup(char *input_popup_string)
1282 {
1283         int err = EMAIL_ERROR_NONE;
1284         int ret = APP_CONTROL_ERROR_NONE;
1285         app_control_h svc_handle = NULL;
1286
1287         ret = app_control_create(&svc_handle);
1288
1289         if (ret != APP_CONTROL_ERROR_NONE || !svc_handle) {
1290                 EM_DEBUG_LOG("app_control_create() failed! ret:[%d]", ret);
1291                 err = EMAIL_ERROR_SYSTEM_FAILURE;
1292                 goto FINISH_OFF;
1293         }
1294
1295         ret = app_control_set_app_id(svc_handle, "org.tizen.email-tts-play");
1296         if (ret != APP_CONTROL_ERROR_NONE) {
1297                 EM_DEBUG_LOG("app_control_set_app_id() failed! ret:[%d]", ret);
1298                 err = EMAIL_ERROR_SYSTEM_FAILURE;
1299                 goto FINISH_OFF;
1300         }
1301
1302         ret = app_control_add_extra_data(svc_handle, "email_misc_work_type", "1003");
1303         if (ret != APP_CONTROL_ERROR_NONE) {
1304                 EM_DEBUG_LOG("app_control_add_extra_data() failed! ret:[%d]", ret);
1305                 err = EMAIL_ERROR_SYSTEM_FAILURE;
1306                 goto FINISH_OFF;
1307         }
1308
1309         ret = app_control_add_extra_data(svc_handle, "popup_string", input_popup_string);
1310         if (ret != APP_CONTROL_ERROR_NONE) {
1311                 EM_DEBUG_LOG("app_control_add_extra_data() failed! ret:[%d]", ret);
1312                 err = EMAIL_ERROR_SYSTEM_FAILURE;
1313                 goto FINISH_OFF;
1314         }
1315
1316         ret = app_control_send_launch_request(svc_handle, NULL, NULL);
1317         if (ret != APP_CONTROL_ERROR_NONE) {
1318                 EM_DEBUG_LOG("app_control_send_launch_request() failed! ret:[%d]", ret);
1319                 err = EMAIL_ERROR_SYSTEM_FAILURE;
1320                 goto FINISH_OFF;
1321         }
1322
1323 FINISH_OFF:
1324
1325     if (svc_handle) {
1326         ret = app_control_destroy(svc_handle);
1327         if (ret != APP_CONTROL_ERROR_NONE) {
1328             EM_DEBUG_LOG("app_control_destroy() failed! ret:[%d]", ret);
1329             err = EMAIL_ERROR_SYSTEM_FAILURE;
1330         }
1331     }
1332
1333         return err;
1334 }
1335 #endif
1336
1337
1338 INTERNAL_FUNC int emcore_add_notification(char *multi_user_name, int account_id, int mail_id, int unread_mail_count, int vip_unread_mail_count, int input_play_alert_tone, int sending_error, unsigned long display)
1339 {
1340         EM_DEBUG_FUNC_BEGIN("account_id[%d] mail_id[%d] unread_mail_count[%d] input_play_alert_tone[%d]", account_id, mail_id, unread_mail_count, input_play_alert_tone );
1341         int err = EMAIL_ERROR_NONE;
1342 #ifdef __FEATURE_NOTIFICATION_ENABLE__
1343         int i = 0;
1344         int private_id = 0;
1345         int *p_mail_id = 0;
1346         int unseen = 0;
1347         int new_noti = 0;
1348         int unread_mail_count_t = 0;
1349         char *mailbox_name = NULL;
1350         char vconf_private_id[MAX_PATH] = {0, };
1351         notification_h noti = NULL;
1352         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
1353         notification_vibration_type_e noti_vibe_type = NOTIFICATION_VIBRATION_TYPE_NONE;
1354         emstorage_account_tbl_t *account_tbl = NULL;
1355         emstorage_mail_tbl_t *p_mail_data = NULL;
1356         int display_status = false;
1357         EMAIL_ALERT_TYPE alert_type = EMAIL_ALERT_TYPE_NONE;
1358         char *alert_tone_path = NULL;
1359         void *join_zone = NULL;
1360
1361         /* For service bundle */
1362         char buf[256] = {0,};
1363         char **value = NULL;
1364         app_control_h service = NULL;
1365         bundle *b = NULL;
1366
1367         if (!emstorage_get_account_by_id(multi_user_name, account_id, GET_FULL_DATA_WITHOUT_PASSWORD, &account_tbl, true, &err)) {
1368                 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed [%d]", err);
1369                 goto FINISH_OFF;
1370         }
1371
1372         if (!account_tbl->options.notification_status) {
1373                 EM_DEBUG_LOG("Notification disabled");
1374                 goto FINISH_OFF;
1375         }
1376
1377         /* Get the unread mail id */
1378         if (!emstorage_get_unread_mailid(multi_user_name, account_id, 0, &p_mail_id, &unread_mail_count_t, &err)) {
1379                 EM_DEBUG_EXCEPTION("emstorage_get_unread_mailid failed [%d]", err);
1380                 goto FINISH_OFF;
1381         }
1382
1383         unseen = unread_mail_count_t;
1384         display_status = account_tbl->options.display_content_status;
1385
1386         /* Load the previous noti */
1387         SNPRINTF(vconf_private_id, sizeof(vconf_private_id), "%s/%d", VCONF_KEY_NOTI_PRIVATE_ID, account_id);
1388         if (vconf_get_int(vconf_private_id, &private_id) != 0) {
1389               EM_DEBUG_EXCEPTION("vconf_get_int failed");
1390         }
1391
1392         /* Turn display on to blink LED */
1393 //      emdevice_change_display_state(DISPLAY_STATE_ON);
1394
1395         if ((err = emcore_set_join_zone(multi_user_name, &join_zone)) != EMAIL_ERROR_NONE) {
1396                 EM_DEBUG_EXCEPTION("emcore_set_join_zone failed : [%d]", err);
1397                 goto FINISH_OFF;
1398         }
1399
1400         noti = notification_load(NULL, private_id);
1401         if(noti == NULL) {
1402                 EM_DEBUG_EXCEPTION("notification_load failed");
1403                 noti = notification_new(NOTIFICATION_TYPE_NOTI, NOTIFICATION_GROUP_ID_NONE, NOTIFICATION_PRIV_ID_NONE);
1404                 if(noti == NULL) {
1405                         EM_DEBUG_EXCEPTION("notification_new failed");
1406                         err = EMAIL_ERROR_SYSTEM_FAILURE;
1407                         goto FINISH_OFF;
1408                 }
1409
1410                 if ((noti_err = notification_set_pkgname(noti, NATIVE_EMAIL_APPLICATION_PKG)) != NOTIFICATION_ERROR_NONE) {
1411                         EM_DEBUG_EXCEPTION("notification_set_pkgname failed [%d]", noti_err);
1412                         err = EMAIL_ERROR_SYSTEM_FAILURE;
1413                         goto FINISH_OFF;
1414                 }
1415
1416                 new_noti = true;
1417         }
1418
1419         if (input_play_alert_tone) {
1420                 alert_type = emcore_get_alert_type(account_tbl->options.vibrate_status);
1421
1422                 if (alert_type == EMAIL_ALERT_TYPE_MELODY_AND_VIB || alert_type == EMAIL_ALERT_TYPE_MELODY)
1423                         alert_tone_path = emcore_get_sound_file_path(account_tbl->options.default_ringtone_status, account_tbl->options.alert_ringtone_path);
1424         }
1425
1426         /* blocking mode : */
1427 #ifdef __FEATURE_BLOCKING_MODE__
1428         if (!emcore_get_blocking_mode_status()) {
1429 #endif /* __FEATURE_BLOCKING_MODE__ */
1430                 if ((noti_err = notification_set_led_time_period(noti, 250, 2500)) != NOTIFICATION_ERROR_NONE) {
1431                         EM_DEBUG_EXCEPTION("notification_set_led_time_period failed [%d]", noti_err);
1432                         err = EMAIL_ERROR_SYSTEM_FAILURE;
1433                         goto FINISH_OFF;
1434                 }
1435
1436                 if ((noti_err = notification_set_led(noti, NOTIFICATION_LED_OP_ON, 0x00)) != NOTIFICATION_ERROR_NONE) {
1437                         EM_DEBUG_EXCEPTION("notification_set_led failed [%d]", noti_err);
1438                         err = EMAIL_ERROR_SYSTEM_FAILURE;
1439                         goto FINISH_OFF;
1440                 }
1441
1442                 if ((alert_type == EMAIL_ALERT_TYPE_MELODY_AND_VIB || alert_type == EMAIL_ALERT_TYPE_MELODY) && EM_SAFE_STRLEN(alert_tone_path)) {
1443                         if(strcmp(alert_tone_path, "silent") == 0) {
1444                                 if ((noti_err = notification_set_sound(noti, NOTIFICATION_SOUND_TYPE_DEFAULT, NULL)) != NOTIFICATION_ERROR_NONE) {
1445                                         EM_DEBUG_EXCEPTION("notification_set_sound failed [%d]", noti_err);
1446                                         err = EMAIL_ERROR_SYSTEM_FAILURE;
1447                                         goto FINISH_OFF;
1448                                 }
1449                         } else {
1450                                 if ((noti_err = notification_set_sound(noti, NOTIFICATION_SOUND_TYPE_USER_DATA, alert_tone_path)) != NOTIFICATION_ERROR_NONE) {
1451                                         EM_DEBUG_EXCEPTION("notification_set_sound failed [%d]", noti_err);
1452                                         err = EMAIL_ERROR_SYSTEM_FAILURE;
1453                                         goto FINISH_OFF;
1454                                 }
1455                         }
1456                 }
1457 #ifdef __FEATURE_BLOCKING_MODE__
1458         }
1459 #endif /* __FEATURE_BLOCKING_MODE__ */
1460
1461         if (alert_type == EMAIL_ALERT_TYPE_MELODY_AND_VIB || alert_type == EMAIL_ALERT_TYPE_VIB)
1462                 noti_vibe_type = NOTIFICATION_VIBRATION_TYPE_DEFAULT;
1463
1464         if ((noti_err = notification_set_vibration(noti, noti_vibe_type, NULL)) != NOTIFICATION_ERROR_NONE) {
1465                 EM_DEBUG_EXCEPTION("notification_set_vibration failed [%d]", noti_err);
1466                 err = EMAIL_ERROR_SYSTEM_FAILURE;
1467                 goto FINISH_OFF;
1468         }
1469
1470         if ((noti_err = notification_set_text_domain(noti, NATIVE_EMAIL_DOMAIN, "/usr/apps/org.tizen.email/res/locale")) != NOTIFICATION_ERROR_NONE) {
1471                 EM_DEBUG_EXCEPTION("notification_set_text_domain failed [%d]", noti_err);
1472                 err = EMAIL_ERROR_SYSTEM_FAILURE;
1473                 goto FINISH_OFF;
1474         }
1475
1476         if (!emstorage_get_mailbox_name_by_mailbox_type(multi_user_name, account_id, EMAIL_MAILBOX_TYPE_INBOX, &mailbox_name, false, &err)) {
1477                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_name_by_mailbox_type failed [%d]", err);
1478                 goto FINISH_OFF;
1479         }
1480
1481         EM_DEBUG_LOG("unseen [%d] display_status [%d]", unseen, display_status);
1482
1483         /* set execution option to launch email-viewer when single incoming mail*/
1484         app_control_create(&service);
1485         if (!service) {
1486                 EM_DEBUG_EXCEPTION("service create failed");
1487         }
1488
1489         if (unseen == 1) {
1490                 if (!emstorage_get_mail_by_id(multi_user_name, p_mail_id[0], &p_mail_data, false, &err)) {
1491                         EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
1492                         goto FINISH_OFF;
1493                 }
1494
1495                 EM_DEBUG_LOG_SEC("single layout:account_name[%s], alias_sender[%s], time[%d], subject[%s]", account_tbl->account_name, p_mail_data->alias_sender, p_mail_data->date_time, p_mail_data->subject);
1496                 if ((noti_err = emcore_layout_single_noti(noti, account_tbl->account_name, 0, p_mail_data->alias_sender, p_mail_data->date_time, p_mail_data->subject, display_status)) != NOTIFICATION_ERROR_NONE) {
1497                         EM_DEBUG_EXCEPTION("notification_layout_single_noti failed [%d]", noti_err);
1498                         err = EMAIL_ERROR_SYSTEM_FAILURE;
1499                         goto FINISH_OFF;
1500                 }
1501
1502                 snprintf(buf, sizeof(buf), "%d", 12);
1503                 app_control_add_extra_data(service, "RUN_TYPE", buf);
1504                 snprintf(buf, sizeof(buf), "%d", account_id);
1505                 app_control_add_extra_data(service, "ACCOUNT_ID", buf);
1506                 snprintf(buf, sizeof(buf), "%d", p_mail_data->mail_id);
1507                 app_control_add_extra_data(service, "MAIL_ID", buf);
1508                 snprintf(buf, sizeof(buf), "%d", p_mail_data->mailbox_id);
1509                 app_control_add_extra_data(service, "MAILBOX_ID", buf);
1510                 app_control_set_app_id(service, NATIVE_EMAIL_APPLICATION_PKG);
1511
1512                 app_control_to_bundle(service, &b);
1513                 noti_err = notification_set_execute_option(noti, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, NULL, NULL, b);
1514                 if (noti_err != NOTIFICATION_ERROR_NONE) {
1515                         EM_DEBUG_EXCEPTION("notification_set_execute_option failed [%d]", noti_err);
1516                         err = EMAIL_ERROR_SYSTEM_FAILURE;
1517                         goto FINISH_OFF;
1518                 }
1519         } else if (unseen > 1) {
1520             EM_DEBUG_LOG_SEC("multi layout: unseen[%d], address[%s], account_name[%s]", unseen, account_tbl->user_email_address, account_tbl->account_name);
1521                 if ((noti_err = emcore_layout_multi_noti(noti, unseen, account_tbl->user_email_address, account_tbl->account_name)) != NOTIFICATION_ERROR_NONE) {
1522                         EM_DEBUG_EXCEPTION("notification_layout_multi_noti failed [%d]", noti_err);
1523                         err = EMAIL_ERROR_SYSTEM_FAILURE;
1524                         goto FINISH_OFF;
1525                 }
1526
1527                 snprintf(buf, sizeof(buf), "%d", 13);
1528                 app_control_add_extra_data(service, "RUN_TYPE", buf);
1529                 snprintf(buf, sizeof(buf), "%d", account_id);
1530                 app_control_add_extra_data(service, "ACCOUNT_ID", buf);
1531
1532                 if (unseen > EMAIL_NOTI_MAX_MAIL_ID)
1533                     unseen = EMAIL_NOTI_MAX_MAIL_ID;
1534                 value = (char **)em_malloc(unseen * sizeof(char *));
1535                 if (value == NULL) {
1536                         EM_DEBUG_EXCEPTION("em_malloc failed");
1537                         err = EMAIL_ERROR_OUT_OF_MEMORY;
1538                         goto FINISH_OFF;
1539                 }
1540
1541                 for (i = 0; i < unseen; i++) {
1542                         memset(buf, 0x00, sizeof(buf));
1543                         snprintf(buf, sizeof(buf), "%d", p_mail_id[i]);
1544                         value[i] = EM_SAFE_STRDUP(buf);
1545                 }
1546                 app_control_add_extra_data_array(service, "MAIL_ID", (const char **)value, unseen);
1547                 app_control_set_app_id(service, NATIVE_EMAIL_APPLICATION_PKG);
1548
1549                 app_control_to_bundle(service, &b);
1550                 noti_err = notification_set_execute_option(noti, NOTIFICATION_EXECUTE_TYPE_MULTI_LAUNCH, NULL, NULL, b);
1551                 if (noti_err != NOTIFICATION_ERROR_NONE) {
1552                         EM_DEBUG_EXCEPTION("notification_set_execute_option failed [%d]", noti_err);
1553                         err = EMAIL_ERROR_SYSTEM_FAILURE;
1554                         goto FINISH_OFF;
1555                 }
1556         } else { /* if priority sender exists and no mail of priority sender, unseen will be 0*/
1557                 EM_DEBUG_LOG("-unseen: [%d]", unseen);
1558                 goto FINISH_OFF;
1559                 /* err = EMAIL_ERROR_PRIORITY_SENDER_MAIL_NOT_FOUND;*/ /*there is no mail for priority sender*/
1560         }
1561
1562         if ((noti_err = notification_set_display_applist(noti, display)) != NOTIFICATION_ERROR_NONE) {
1563                 EM_DEBUG_EXCEPTION("notification_insert failed [%d]", noti_err);
1564                 err = EMAIL_ERROR_SYSTEM_FAILURE;
1565                 goto FINISH_OFF;
1566         }
1567
1568         if (new_noti) {
1569                 if ((noti_err = notification_insert(noti, &private_id)) != NOTIFICATION_ERROR_NONE) {
1570                         EM_DEBUG_EXCEPTION("notification_insert failed [%d]", noti_err);
1571                         err = EMAIL_ERROR_SYSTEM_FAILURE;
1572                         goto FINISH_OFF;
1573                 }
1574
1575                 EM_DEBUG_LOG("Private_id = [%d]", private_id);
1576
1577                 SNPRINTF(vconf_private_id, sizeof(vconf_private_id), "%s/%d", VCONF_KEY_NOTI_PRIVATE_ID, account_id);
1578                 if (vconf_set_int(vconf_private_id, private_id) != 0) {
1579                         EM_DEBUG_EXCEPTION("vconf_set_int failed");
1580                         err = EMAIL_ERROR_SYSTEM_FAILURE;
1581                         goto FINISH_OFF;
1582                 }
1583         } else {
1584                 if ((noti_err = notification_update(noti)) != NOTIFICATION_ERROR_NONE) {
1585                         EM_DEBUG_EXCEPTION("notification_update failed [%d]", noti_err);
1586                         err = EMAIL_ERROR_SYSTEM_FAILURE;
1587                         goto FINISH_OFF;
1588                 }
1589         }
1590
1591 #ifdef __FEATURE_DRIVING_MODE__
1592         if (unseen) {
1593                 err = emcore_start_driving_mode(multi_user_name, p_mail_id[unseen - 1]);
1594                 if (err != EMAIL_ERROR_NONE)
1595                         EM_DEBUG_EXCEPTION("emcore_start_driving_mode failed : [%d]", err);
1596         }
1597 #endif /* __FEATURE_DRIVING_MODE__ */
1598
1599 FINISH_OFF:
1600
1601         if (service)
1602                 app_control_destroy(service);
1603
1604         if (noti)
1605                 notification_free(noti);
1606
1607         if (join_zone)
1608                 emcore_unset_join_zone(join_zone);
1609
1610         EM_SAFE_FREE(alert_tone_path);
1611         EM_SAFE_FREE(p_mail_id); /* prevent */
1612
1613         if (p_mail_data)
1614                 emstorage_free_mail(&p_mail_data, 1, NULL);
1615
1616         if (account_tbl)
1617                 emstorage_free_account(&account_tbl, 1, NULL);
1618
1619         for (i = 0; i < unseen; i++) {
1620                 if (value)
1621                         EM_SAFE_FREE(value[i]);
1622         }
1623         EM_SAFE_FREE(value);
1624
1625         EM_SAFE_FREE(mailbox_name);
1626
1627 #endif  /* __FEATURE_NOTIFICATION_ENABLE__ */
1628
1629         EM_DEBUG_FUNC_END("ret [%d]", err);
1630         return err;
1631 }
1632
1633 INTERNAL_FUNC int emcore_add_notification_for_send(char *multi_user_name, int account_id, int mail_id, email_action_t action, int sending_error, unsigned long display)
1634 {
1635         EM_DEBUG_FUNC_BEGIN("account_id: %d, mail_id: %d, action:%d", account_id, mail_id, action );
1636         int err = EMAIL_ERROR_NONE;
1637 #ifdef __FEATURE_NOTIFICATION_ENABLE__  
1638         int private_id = 0;
1639         void *join_zone = NULL;
1640         char *mailbox_name = NULL;
1641         char *dgettext_string = NULL;
1642         char vconf_private_id[MAX_PATH] = {0, };
1643         notification_h noti = NULL;
1644         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
1645         emstorage_mail_tbl_t *p_mail_data = NULL;
1646         emstorage_account_tbl_t *account_tbl = NULL;
1647
1648         /* For service bundle */
1649         char buf[256] = {0,};
1650         app_control_h service = NULL;
1651         bundle *b = NULL;
1652
1653         /* get general noti status */
1654         if (!emstorage_get_account_by_id(multi_user_name, account_id, GET_FULL_DATA_WITHOUT_PASSWORD, &account_tbl, true, &err)) {
1655                 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed [%d]", err);
1656                 goto FINISH_OFF;
1657         }
1658
1659         if (!account_tbl->options.notification_status) {
1660                 EM_DEBUG_LOG("notification disabled");
1661                 goto FINISH_OFF;
1662         }
1663
1664         if (!account_tbl->options.display_content_status) {
1665                 EM_DEBUG_LOG("display content disabled");
1666                 goto FINISH_OFF;
1667         }
1668
1669         /* Delete the previous noti */
1670         emcore_delete_notification_by_account(multi_user_name, account_id, true);
1671         if (g_sending_noti_handle) {
1672                 if((noti_err = notification_free(g_sending_noti_handle)) != NOTIFICATION_ERROR_NONE)
1673                         err = EMAIL_ERROR_NOTI;
1674                 g_sending_noti_handle = NULL;
1675         }
1676
1677         if ((err = emcore_set_join_zone(multi_user_name, &join_zone)) != EMAIL_ERROR_NONE) {
1678                 EM_DEBUG_EXCEPTION("emcore_set_join_zone failed : [%d]", err);
1679                 goto FINISH_OFF;
1680         }
1681
1682         if (action == EMAIL_ACTION_SENDING_MAIL)
1683                 noti = notification_create(NOTIFICATION_TYPE_ONGOING);
1684         else
1685                 noti = notification_new(NOTIFICATION_TYPE_NOTI, NOTIFICATION_GROUP_ID_NONE, NOTIFICATION_PRIV_ID_NONE);
1686
1687         if (noti == NULL) {
1688                 EM_DEBUG_EXCEPTION("notification_new failed");
1689                 err = EMAIL_ERROR_NOTI;
1690                 goto FINISH_OFF;
1691         }
1692
1693         if ((noti_err = notification_set_pkgname(noti, NATIVE_EMAIL_APPLICATION_PKG)) != NOTIFICATION_ERROR_NONE) {
1694                 EM_DEBUG_EXCEPTION("notification_set_pkgname failed [%d]", noti_err);
1695                 err = EMAIL_ERROR_NOTI;
1696                 goto FINISH_OFF;
1697         }
1698
1699         if (!emstorage_get_mail_by_id(multi_user_name, mail_id, &p_mail_data, false, &err)) {
1700                 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
1701                 goto FINISH_OFF;
1702         }
1703
1704         /* Create service bundle */
1705         app_control_create(&service);
1706         if (!service) {
1707                 EM_DEBUG_EXCEPTION("service create failed");
1708         }
1709
1710         snprintf(buf, sizeof(buf), "%d", 12);
1711         app_control_add_extra_data(service, "RUN_TYPE", buf);
1712         snprintf(buf, sizeof(buf), "%d", account_id);
1713         app_control_add_extra_data(service, "ACCOUNT_ID", buf);
1714         snprintf(buf, sizeof(buf), "%d", p_mail_data->mail_id);
1715         app_control_add_extra_data(service, "MAIL_ID", buf);
1716         snprintf(buf, sizeof(buf), "%d", p_mail_data->mailbox_id);
1717         app_control_add_extra_data(service, "MAILBOX_ID", buf);
1718         app_control_set_app_id(service, NATIVE_EMAIL_APPLICATION_PKG);
1719
1720         app_control_to_bundle(service, &b);
1721         noti_err = notification_set_execute_option(noti, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, NULL, NULL, b);
1722         if (noti_err != NOTIFICATION_ERROR_NONE) {
1723                 EM_DEBUG_EXCEPTION("notification_set_execute_option failed [%d]", noti_err);
1724                 err = EMAIL_ERROR_SYSTEM_FAILURE;
1725                 goto FINISH_OFF;
1726         }
1727
1728         switch (action) {
1729         case EMAIL_ACTION_SEND_MAIL :
1730 /*
1731                 setlocale(LC_MESSAGES, vconf_get_str(VCONFKEY_LANGSET));
1732                 bindtextdomain("sys_string", "/usr/share/locale");
1733                 textdomain("sys_string");
1734 */
1735                 setlocale(LC_MESSAGES, vconf_get_str(VCONFKEY_LANGSET));
1736                 bindtextdomain(NATIVE_EMAIL_DOMAIN, "/usr/apps/org.tizen.email/res/locale");
1737                 textdomain(NATIVE_EMAIL_DOMAIN);
1738
1739                 switch(sending_error) {
1740                 case EMAIL_ERROR_NONE:
1741                         dgettext_string = dgettext(NATIVE_EMAIL_DOMAIN, "IDS_EMAIL_TPOP_EMAIL_SENT");
1742                         break;
1743                 case EMAIL_ERROR_FLIGHT_MODE_ENABLE:
1744                         dgettext_string = dgettext(NATIVE_EMAIL_DOMAIN, "IDS_EMAIL_TPOP_FAILED_TO_CONNECT_TO_NETWORK");
1745                         break;
1746                 case EMAIL_ERROR_NETWORK_NOT_AVAILABLE:
1747                 case EMAIL_ERROR_NO_SIM_INSERTED:
1748                         dgettext_string = dgettext(NATIVE_EMAIL_DOMAIN, "IDS_EMAIL_TPOP_NETWORK_NOT_AVAILABLE_EMAIL_WILL_BE_SENT_WHEN_CONNECTED_TO_NETWORK");
1749                         break;
1750                 case EMAIL_ERROR_SERVER_STORAGE_FULL:
1751                         dgettext_string = dgettext(NATIVE_EMAIL_DOMAIN, "IDS_EMAIL_TPOP_SENDING_FAILED_SERVER_STORAGE_FULL_ABB");
1752                         break;
1753                 default:
1754                         dgettext_string = dgettext(NATIVE_EMAIL_DOMAIN, "IDS_EMAIL_TPOP_SENDING_FAILED");
1755                         break;
1756                 }
1757
1758                 if ((noti_err = notification_set_layout(noti, 
1759                                                                                                 NOTIFICATION_LY_NOTI_EVENT_SINGLE)) != NOTIFICATION_ERROR_NONE) {
1760                         EM_DEBUG_EXCEPTION("notification_set_layout failed [%d]", noti_err);
1761                         err = EMAIL_ERROR_NOTI;
1762                         goto FINISH_OFF;
1763                 }
1764
1765                 if ((noti_err = notification_set_image(noti, 
1766                                                                                                 NOTIFICATION_IMAGE_TYPE_ICON, 
1767                                                                                                 EMAIL_NOTI_ICON_PATH)) != NOTIFICATION_ERROR_NONE) {
1768                         EM_DEBUG_EXCEPTION("notification_set_image failed [%d]", noti_err);
1769                         err = EMAIL_ERROR_NOTI;
1770                         goto FINISH_OFF;
1771                 }
1772
1773                 if ((noti_err = notification_set_text(noti, 
1774                                                                                         NOTIFICATION_TEXT_TYPE_TITLE,
1775                                                                                         p_mail_data->alias_recipient,           
1776                                                                                         NULL, 
1777                                                                                         NOTIFICATION_VARIABLE_TYPE_NONE)) != NOTIFICATION_ERROR_NONE) {
1778                         EM_DEBUG_EXCEPTION("notification_set_text failed [%d]", noti_err);
1779                         err = EMAIL_ERROR_NOTI;
1780                         goto FINISH_OFF;
1781                 }
1782
1783                 switch (sending_error) {
1784                 case EMAIL_ERROR_NONE:
1785                         if ((noti_err = notification_set_text(noti,
1786                                                                                                 NOTIFICATION_TEXT_TYPE_CONTENT,
1787                                                                                                 "Email Sent",
1788                                                                                                 dgettext_string,
1789                                                                                                 NOTIFICATION_VARIABLE_TYPE_NONE)) != NOTIFICATION_ERROR_NONE) {
1790                                 EM_DEBUG_EXCEPTION("notification_set_text failed [%d]", noti_err);
1791                                 err = EMAIL_ERROR_NOTI;
1792                                 goto FINISH_OFF;
1793                         }
1794                         break;
1795                 case EMAIL_ERROR_SERVER_STORAGE_FULL:
1796                         if ((noti_err = notification_set_text(noti,
1797                                                                                                 NOTIFICATION_TEXT_TYPE_CONTENT,
1798                                                                                                 "Sending failed. Server storage full.",
1799                                                                                                 dgettext_string,
1800                                                                                                 NOTIFICATION_VARIABLE_TYPE_NONE)) != NOTIFICATION_ERROR_NONE) {
1801                                 EM_DEBUG_EXCEPTION("notification_set_text failed [%d]", noti_err);
1802                                 err = EMAIL_ERROR_NOTI;
1803                                 goto FINISH_OFF;
1804                         }
1805                         break;
1806                 default:
1807                         if ((noti_err = notification_set_text(noti, 
1808                                                                                                 NOTIFICATION_TEXT_TYPE_CONTENT, 
1809                                                                                                 "Sending failed", 
1810                                                                                                 dgettext_string,
1811                                                                                                 NOTIFICATION_VARIABLE_TYPE_NONE)) != NOTIFICATION_ERROR_NONE) {
1812                                 EM_DEBUG_EXCEPTION("notification_set_text failed [%d]", noti_err);
1813                                 err = EMAIL_ERROR_NOTI;
1814                                 goto FINISH_OFF;
1815                         }
1816                         break;
1817                 }
1818
1819                 if (sending_error == EMAIL_ERROR_SMTP_SEND_FAILURE_BY_OVERSIZE) {
1820                         if ((noti_err = notification_set_text(noti, 
1821                                                                                                 NOTIFICATION_TEXT_TYPE_INFO_1, 
1822                                                                                                 dgettext(NATIVE_EMAIL_DOMAIN, "IDS_EMAIL_POP_THE_EMAIL_IS_TOO_LARGE"), 
1823                                                                                                 NULL, 
1824                                                                                                 NOTIFICATION_VARIABLE_TYPE_NONE)) != NOTIFICATION_ERROR_NONE) {
1825                                 EM_DEBUG_EXCEPTION("notification_set_text TEXT_TYPE_INFO_1 failed");
1826                                 err = EMAIL_ERROR_NOTI;
1827                                 goto FINISH_OFF;
1828                         }
1829                 }
1830
1831                 if ((noti_err = notification_set_display_applist(noti, 
1832                                                                                                 NOTIFICATION_DISPLAY_APP_NOTIFICATION_TRAY | NOTIFICATION_DISPLAY_APP_TICKER)) != NOTIFICATION_ERROR_NONE) {
1833                         EM_DEBUG_EXCEPTION("notification_insert failed [%d]", noti_err);
1834                         err = EMAIL_ERROR_NOTI;
1835                         goto FINISH_OFF;
1836                 }
1837
1838                 if ((noti_err = notification_insert(noti, &private_id)) != NOTIFICATION_ERROR_NONE) {
1839                         EM_DEBUG_EXCEPTION("notification_insert failed [%d]", noti_err);
1840                         err = EMAIL_ERROR_NOTI;
1841                         goto FINISH_OFF;
1842                 }
1843
1844                 EM_DEBUG_LOG("Private_id = [%d]", private_id);
1845
1846                 SNPRINTF(vconf_private_id, sizeof(vconf_private_id), "%s/%d", VCONF_KEY_NOTI_PRIVATE_ID, account_id);
1847                 if (vconf_set_int(vconf_private_id, private_id) != 0) {
1848                         EM_DEBUG_EXCEPTION("vconf_set_int failed");
1849                         err = EMAIL_ERROR_GCONF_FAILURE;
1850                         goto FINISH_OFF;
1851                 }
1852
1853                 break;
1854
1855         case EMAIL_ACTION_SENDING_MAIL :
1856
1857                 if ((noti_err = notification_set_text_domain(noti, NATIVE_EMAIL_DOMAIN, "/usr/apps/org.tizen.email/res/locale")) != NOTIFICATION_ERROR_NONE) {
1858                         EM_DEBUG_EXCEPTION("notification_set_text_domain failed [%d]", noti_err);
1859                         err = EMAIL_ERROR_NOTI;
1860                         goto FINISH_OFF;
1861                 }
1862
1863                 if ((noti_err = notification_set_layout(noti, NOTIFICATION_LY_ONGOING_PROGRESS)) != NOTIFICATION_ERROR_NONE) {
1864                         EM_DEBUG_EXCEPTION("notification_set_layout failed [%d]", noti_err);
1865                         err = EMAIL_ERROR_NOTI;
1866                         goto FINISH_OFF;
1867                 }
1868
1869                 if ((noti_err = notification_set_image(noti, NOTIFICATION_IMAGE_TYPE_ICON, EMAIL_NOTI_ICON_PATH)) != NOTIFICATION_ERROR_NONE) {
1870                         EM_DEBUG_EXCEPTION("notification_set_image failed [%d]", noti_err);
1871                         err = EMAIL_ERROR_NOTI;
1872                         goto FINISH_OFF;
1873                 }
1874
1875                 if ((noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_TITLE, "Sending email...", dgettext(NATIVE_EMAIL_DOMAIN, "IDS_EMAIL_TPOP_SENDING_EMAIL_ING"), NOTIFICATION_VARIABLE_TYPE_NONE)) != NOTIFICATION_ERROR_NONE) {
1876                         EM_DEBUG_EXCEPTION("notification_set_text failed [%d]", noti_err);
1877                         err = EMAIL_ERROR_NOTI;
1878                         goto FINISH_OFF;
1879                 }
1880
1881                 if ((noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT, p_mail_data->subject, NULL, NOTIFICATION_VARIABLE_TYPE_NONE)) != NOTIFICATION_ERROR_NONE) {
1882                         EM_DEBUG_EXCEPTION("notification_set_text failed [%d]", noti_err);
1883                         err = EMAIL_ERROR_NOTI;
1884                         goto FINISH_OFF;
1885                 }
1886
1887                 /*notification_set_execute_option(noti, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, NULL, NULL, args);*/
1888
1889                 if ((noti_err = notification_set_display_applist(noti, NOTIFICATION_DISPLAY_APP_ALL)) != NOTIFICATION_ERROR_NONE) {
1890                         EM_DEBUG_EXCEPTION("notification_insert failed [%d]", noti_err);
1891                         err = EMAIL_ERROR_NOTI;
1892                         goto FINISH_OFF;
1893                 }
1894
1895                 if ((noti_err = notification_insert(noti, &private_id)) != NOTIFICATION_ERROR_NONE) {
1896                         EM_DEBUG_EXCEPTION("notification_insert failed [%d]", noti_err);
1897                         err = EMAIL_ERROR_NOTI;
1898                         goto FINISH_OFF;
1899                 }
1900
1901                 g_sending_noti_handle = noti;
1902                 EM_DEBUG_LOG("Private_id = [%d]", private_id);
1903
1904                 SNPRINTF(vconf_private_id, sizeof(vconf_private_id), "%s/%d", VCONF_KEY_NOTI_PRIVATE_ID, account_id);
1905                 if (vconf_set_int (vconf_private_id, private_id) != 0) {
1906                         EM_DEBUG_EXCEPTION("vconf_set_int failed");
1907                         err = EMAIL_ERROR_GCONF_FAILURE;
1908                         goto FINISH_OFF;
1909                 }
1910
1911                 break;
1912
1913         default :
1914                 if ((noti_err = notification_free (noti)) != NOTIFICATION_ERROR_NONE) {
1915                         EM_DEBUG_EXCEPTION ("notification_free error [%d]", noti_err);
1916                         err = EMAIL_ERROR_NOTI;
1917                 }
1918
1919                 break;
1920         }
1921
1922 FINISH_OFF:
1923
1924         if (service)
1925                 app_control_destroy(service);
1926
1927     if ((action == EMAIL_ACTION_SEND_MAIL) && noti) {
1928         if ((noti_err = notification_free(noti)) != NOTIFICATION_ERROR_NONE) {
1929             err = EMAIL_ERROR_NOTI;
1930                 }
1931     }
1932
1933         if ((action != EMAIL_ACTION_SEND_MAIL) && (err != EMAIL_ERROR_NONE)) {
1934                 if (noti) {
1935                         notification_free(noti);
1936                         g_sending_noti_handle = NULL;
1937                 }
1938         }
1939
1940         if (join_zone)
1941                 emcore_unset_join_zone(join_zone);
1942
1943         if (p_mail_data)
1944                 emstorage_free_mail(&p_mail_data, 1, NULL);
1945
1946         if (account_tbl)
1947                 emstorage_free_account(&account_tbl, 1, NULL);
1948
1949         EM_SAFE_FREE(mailbox_name);
1950
1951 #endif  /* __FEATURE_NOTIFICATION_ENABLE__ */
1952
1953         EM_DEBUG_FUNC_END("ret [%d]", err);
1954         return err;
1955 }
1956
1957 INTERNAL_FUNC void emcore_update_notification_for_send(int account_id, int mail_id, double progress)
1958 {
1959         EM_DEBUG_FUNC_BEGIN("account_id: %d, mail_id: %d, progress: %f", account_id, mail_id, progress);
1960
1961         int private_id = 0;
1962         char vconf_private_id[MAX_PATH] = {0, };
1963         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
1964
1965         SNPRINTF(vconf_private_id, sizeof(vconf_private_id), "%s/%d", VCONF_KEY_NOTI_PRIVATE_ID, account_id);
1966         if (vconf_get_int(vconf_private_id, &private_id) != 0) {
1967                 EM_DEBUG_EXCEPTION("vconf_get_int failed");
1968         }
1969
1970         EM_DEBUG_LOG("Private_id = [%d]", private_id);
1971         noti_err = notification_update_progress(g_sending_noti_handle, private_id, progress);
1972         if (noti_err != NOTIFICATION_ERROR_NONE) {
1973                 EM_DEBUG_LOG("notification_update_progress failed [0x%x]", noti_err);
1974         }
1975
1976         EM_DEBUG_FUNC_END();
1977 }
1978
1979 INTERNAL_FUNC int emcore_show_user_message(char *multi_user_name, int id, email_action_t action, int error)
1980 {
1981         EM_DEBUG_FUNC_BEGIN("id[%d], action[%d], error[%d]", id, action, error);
1982
1983         int ret = false;
1984         int display = 0;
1985         emstorage_mail_tbl_t *mail_table_data = NULL;
1986
1987         if ((action == EMAIL_ACTION_SEND_MAIL || action == EMAIL_ACTION_SENDING_MAIL) && error != EMAIL_ERROR_CANCELLED) {
1988         /*  In case email is cancelled using cancel button in Outbox there is no need to show Cancel/Retry Pop up */
1989
1990                 if (id <= 0) {
1991                         EM_DEBUG_LOG("Invalid mail_id");
1992                         return false;
1993                 }
1994
1995                 if (!emstorage_get_mail_by_id(multi_user_name, id, &mail_table_data, true, NULL)) {
1996                         EM_DEBUG_LOG("Mail not found");
1997                         return false;
1998                 }
1999
2000                 EM_DEBUG_LOG ("smtp_stream : sent_end");
2001                 if (emcore_add_notification_for_send(multi_user_name, mail_table_data->account_id, id, action, error, display) != EMAIL_ERROR_NONE) {
2002                         EM_DEBUG_EXCEPTION("emcore_notification_set error");
2003                         goto FINISH_OFF;
2004                 }
2005                 ret = true;
2006         }
2007
2008 FINISH_OFF:
2009
2010         if (mail_table_data && !emstorage_free_mail(&mail_table_data, 1, NULL))
2011                 EM_DEBUG_EXCEPTION("emstorage_free_mail Failed");
2012
2013         EM_DEBUG_FUNC_END("ret [%d]", ret);
2014         return ret;
2015 }
2016
2017
2018 /* storage space handling - 210709 */
2019 int emcore_get_storage_status(void)
2020 {
2021         EM_DEBUG_FUNC_BEGIN();
2022         int storage_status = 0, nError = 0;
2023
2024 #if !GLIB_CHECK_VERSION(2, 36, 0) 
2025         g_type_init(); 
2026 #endif
2027
2028 #ifdef STORAGE_STATUS
2029         nError = vconf_get_int(PS_KEY_SYSTEM_STORAGE_MOVI_STATUS,
2030                                                         &storage_status);
2031 #endif /*  STORAGE_STATUS */
2032
2033         if (nError == -1) {
2034                 EM_DEBUG_EXCEPTION("vconf_get_int Failed");
2035                 return false;
2036         }
2037         EM_DEBUG_FUNC_END();
2038         return storage_status;
2039 }
2040
2041
2042
2043 INTERNAL_FUNC int emcore_is_storage_full()
2044 {
2045         EM_DEBUG_FUNC_BEGIN();
2046         int err = EMAIL_ERROR_NONE;
2047         int ret_from_storage_lib = 0;
2048         struct statvfs stat_result;
2049
2050         ret_from_storage_lib = storage_get_internal_memory_size(&stat_result);
2051         if (ret_from_storage_lib < 0) {
2052                 EM_DEBUG_EXCEPTION("ret_from_storage_lib [%d]", ret_from_storage_lib);
2053                 err = EMAIL_ERROR_SYSTEM_FAILURE;
2054                 goto FINISH_OFF;
2055         }
2056         else {
2057                 unsigned long i_free = (stat_result.f_bsize*stat_result.f_bavail) / (1024 * 1024);
2058                 if (i_free < EMAIL_LIMITATION_FREE_SPACE) {
2059                         EM_DEBUG_EXCEPTION("total[%lf] avail[%lf]", (double)stat_result.f_frsize*stat_result.f_blocks, (double)stat_result.f_bsize*stat_result.f_bavail);
2060                         EM_DEBUG_EXCEPTION("Not enough free space of storage [%ld] MB.", i_free);
2061                         err = EMAIL_ERROR_MAIL_MEMORY_FULL;
2062                 }
2063         }
2064
2065 FINISH_OFF:
2066         EM_DEBUG_FUNC_END("err[%d]", err);
2067         return err;
2068 }
2069
2070 int emcore_calc_mail_size(char *multi_user_name, email_mail_data_t *input_mail_data, email_attachment_data_t *input_attachment_data_list, int input_attachment_count, int *output_size)
2071 {
2072         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);
2073
2074         struct stat            st_buf;
2075         int                    mail_size = 0; /*  size of the plain text body and attachments */
2076         int                    err       = EMAIL_ERROR_NONE;
2077         int                    i         = 0;
2078     char                   *prefix_path = NULL;
2079     char                   real_file_path[255] = {0};
2080
2081         if (!input_mail_data || (input_attachment_count && !input_attachment_data_list) || (!input_attachment_count &&input_attachment_data_list) || !output_size)  {
2082                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2083                 err = EMAIL_ERROR_INVALID_PARAM;
2084                 goto FINISH_OFF;
2085         }
2086
2087     if (EM_SAFE_STRLEN(multi_user_name) > 0) {
2088                 err = emcore_get_container_path(multi_user_name, &prefix_path);
2089                 if (err != EMAIL_ERROR_NONE) {
2090                         EM_DEBUG_EXCEPTION("emcore_get_container_path failed :[%d]", err);
2091                         goto FINISH_OFF;
2092                 }
2093         } else {
2094                 prefix_path = strdup("");
2095         }
2096
2097         if (input_mail_data->file_path_plain != NULL) {
2098         memset(real_file_path, 0x00, sizeof(real_file_path));
2099         SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, input_mail_data->file_path_plain);
2100
2101                 if (stat(real_file_path, &st_buf) < 0)  {
2102                         EM_DEBUG_EXCEPTION_SEC("input_mail_data->file_path_plain : stat(\"%s\") failed...", input_mail_data->file_path_plain);
2103                         err = EMAIL_ERROR_FILE_NOT_FOUND;
2104                         goto FINISH_OFF;
2105                 }
2106
2107                 mail_size += st_buf.st_size;
2108
2109         }
2110
2111         if (input_mail_data->file_path_html != NULL) {
2112         memset(real_file_path, 0x00, sizeof(real_file_path));
2113         SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, input_mail_data->file_path_html);
2114
2115                 if (stat(real_file_path, &st_buf) < 0) {
2116                         EM_DEBUG_EXCEPTION_SEC("input_mail_data->file_path_html : stat(\"%s\") failed...", input_mail_data->file_path_html);
2117                         err = EMAIL_ERROR_FILE_NOT_FOUND;
2118                         goto FINISH_OFF;
2119                 }
2120
2121                 mail_size += st_buf.st_size;
2122         }
2123
2124         for(i = 0; i < input_attachment_count; i++)  {
2125         memset(real_file_path, 0x00, sizeof(real_file_path));
2126         SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, input_attachment_data_list[i].attachment_path);
2127
2128                 if (stat(real_file_path, &st_buf) < 0)  {
2129                         EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", input_attachment_data_list[i].attachment_path);
2130                         err = EMAIL_ERROR_FILE_NOT_FOUND;
2131                         goto FINISH_OFF;
2132                 }
2133                 mail_size += st_buf.st_size;
2134         }
2135
2136         *output_size = mail_size;
2137
2138 FINISH_OFF:
2139
2140         EM_SAFE_FREE(prefix_path);
2141
2142         EM_DEBUG_FUNC_END("mail_size [%d]", mail_size);
2143         return err;
2144 }
2145
2146
2147 /* parse the Full mailbox Path and Get the Alias Name of the Mailbox */
2148 char *emcore_get_alias_of_mailbox(const char *mailbox_path)
2149 {
2150         EM_DEBUG_FUNC_BEGIN();
2151         EM_IF_NULL_RETURN_VALUE(mailbox_path, NULL);
2152
2153         guint index = 0;
2154         gchar **token_list = NULL;
2155         gchar *mailbox = NULL, *name = NULL;
2156         char *converted_name;
2157
2158
2159         mailbox = g_strdup(mailbox_path);
2160         token_list = g_strsplit_set(mailbox, "/", -1);
2161
2162         if(token_list == NULL) {
2163                 EM_DEBUG_LOG("g_strsplit_set failed.");
2164         if (mailbox)
2165             g_free(mailbox);
2166
2167                 return NULL;
2168         }
2169
2170         if (mailbox)
2171                 g_free(mailbox);
2172
2173         while (token_list[index] != NULL)
2174                 index++;
2175
2176         name = g_strdup(token_list[index - 1]);
2177         if (!name) /* prevent 27459 */ {
2178         if (mailbox)
2179             g_free(mailbox);
2180
2181                 return NULL;
2182     }
2183
2184         g_strfreev(token_list);
2185
2186         converted_name = emcore_convert_mutf7_to_utf8(name);
2187         EM_DEBUG_LOG_DEV("converted_name:%s", converted_name);
2188
2189         if (!converted_name || EM_SAFE_STRLEN(converted_name) == 0) {
2190         EM_SAFE_FREE(converted_name);
2191                 converted_name = g_strdup(name);
2192         }
2193
2194         if (name)
2195                 g_free(name);
2196
2197         EM_DEBUG_FUNC_END();
2198         return converted_name;
2199 }
2200
2201
2202 INTERNAL_FUNC int emcore_get_first_address(const char *input_full_address, char **output_display_name, char **output_angle_addr)
2203 {
2204         EM_DEBUG_FUNC_BEGIN("input_full_address[%p], output_display_name[%p], output_angle_addr[%p]", input_full_address, output_display_name, output_angle_addr);
2205
2206         int err = EMAIL_ERROR_NONE;
2207         char *full_address = NULL;
2208         char angle_addr[MAX_EMAIL_ADDRESS_LENGTH] = { 0, };
2209         ADDRESS *address_list = NULL;
2210
2211         if (input_full_address == NULL || output_display_name == NULL || output_angle_addr == NULL) {
2212                 err = EMAIL_ERROR_INVALID_PARAM;
2213                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2214                 goto FINISH_OFF;
2215         }
2216
2217         /* full_address will be tainted by rfc822_parse_adrlist */
2218         /* So, input_full_address should be duplicated */
2219         full_address = EM_SAFE_STRDUP(input_full_address);
2220
2221         if (full_address == NULL) {
2222                 err = EMAIL_ERROR_OUT_OF_MEMORY;
2223                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_OUT_OF_MEMORY");
2224                 goto FINISH_OFF;
2225         }
2226
2227         rfc822_parse_adrlist(&address_list, (char*)full_address, NULL);
2228
2229         if (address_list) {
2230                 if (address_list->host)
2231                         SNPRINTF(angle_addr, MAX_EMAIL_ADDRESS_LENGTH, "%s@%s", address_list->mailbox, address_list->host);
2232                 else /* For SMS of eas-engine  : (example : 01012345678) */
2233                         SNPRINTF(angle_addr, MAX_EMAIL_ADDRESS_LENGTH, "%s", address_list->mailbox);
2234
2235                 *output_display_name  = EM_SAFE_STRDUP(address_list->personal);
2236                 *output_angle_addr    = EM_SAFE_STRDUP(angle_addr);
2237
2238                 mail_free_address(&address_list);
2239         }
2240
2241 FINISH_OFF:
2242         EM_SAFE_FREE(full_address);
2243
2244         EM_DEBUG_FUNC_END();
2245         return err;
2246 }
2247
2248 void emcore_fill_address_information_of_mail_tbl(char *multi_user_name, emstorage_mail_tbl_t *mail_data)
2249 {
2250         EM_DEBUG_FUNC_BEGIN("mail_data [%p]", mail_data);
2251
2252         char *first_alias   = NULL;
2253         char *first_address = NULL;
2254         char *recipient     = NULL;
2255
2256         int i = 0;
2257         int vector_len = 0;
2258         char **vector;
2259         char *recipient_self = NULL;
2260
2261         email_account_t *ref_account = NULL;
2262
2263         ref_account = emcore_get_account_reference(multi_user_name, mail_data->account_id, false);
2264         if (!ref_account)  {
2265                 EM_DEBUG_LOG("emcore_get_account_reference failed [%d]", mail_data->account_id);
2266         }
2267
2268         EM_DEBUG_LOG_SEC("full_address_from [%s]", mail_data->full_address_from);
2269
2270         /*  sender alias & address */
2271         if (emcore_get_first_address(mail_data->full_address_from, &first_alias, &first_address) == true) {
2272                 if (first_alias == NULL) {
2273                         mail_data->alias_sender = EM_SAFE_STRDUP(first_address);
2274                 }
2275                 else {
2276                         mail_data->alias_sender = first_alias;
2277                         first_alias = NULL;
2278                 }
2279                 mail_data->email_address_sender = first_address;
2280                 first_address = NULL;
2281         }
2282
2283         EM_DEBUG_LOG_SEC("full_address_from [%s]", mail_data->full_address_from);
2284
2285         /*  recipient alias & address */
2286         if (mail_data->full_address_to != NULL)
2287                 recipient = mail_data->full_address_to;
2288         else if (mail_data->full_address_cc != NULL)
2289                 recipient = mail_data->full_address_cc;
2290         else if (mail_data->full_address_bcc != NULL)
2291                 recipient = mail_data->full_address_bcc;
2292
2293         /* if full_address_to contains account address, set account address to email_address_recipient */
2294         if (ref_account && mail_data->full_address_to &&
2295                         g_strrstr(mail_data->full_address_to, ref_account->user_email_address)) {
2296                 vector = g_strsplit_set(mail_data->full_address_to, ";", -1);
2297                 vector_len = g_strv_length(vector);
2298
2299                 for (i = 0; i < vector_len; i++) {
2300                         if (g_strrstr(vector[i], ref_account->user_email_address)) {
2301                                 recipient_self = EM_SAFE_STRDUP(vector[i]);
2302                                 break;
2303                         }
2304                 }
2305                 g_strfreev(vector);
2306
2307                 if (recipient_self) {
2308                         recipient = recipient_self;
2309                 }
2310         }
2311
2312         if (emcore_get_first_address(recipient, &first_alias, &first_address) == true) {
2313                 if (first_alias == NULL)
2314                         mail_data->alias_recipient = EM_SAFE_STRDUP(first_address);
2315                 else
2316                         mail_data->alias_recipient = first_alias;
2317
2318                 mail_data->email_address_recipient = first_address;
2319         }
2320
2321         if (ref_account) {
2322                 emcore_free_account(ref_account);
2323                 EM_SAFE_FREE(ref_account);
2324         }
2325
2326         EM_SAFE_FREE(recipient_self);
2327         EM_DEBUG_FUNC_END();
2328 }
2329
2330 struct email_attribute_info {
2331         email_mail_attribute_type        attribute_type;
2332         char                            *attribute_name;
2333         email_mail_attribute_value_type  attribute_value_type;
2334 };
2335
2336 static struct email_attribute_info _mail_attribute_info_array[] = {
2337                 {EMAIL_MAIL_ATTRIBUTE_MAIL_ID, "mail_id", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
2338                 {EMAIL_MAIL_ATTRIBUTE_ACCOUNT_ID, "account_id", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
2339                 {EMAIL_MAIL_ATTRIBUTE_MAILBOX_ID, "mailbox_id", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
2340                 {EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME, "mailbox_name", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_STRING},
2341                 {EMAIL_MAIL_ATTRIBUTE_MAILBOX_TYPE, "mailbox_type", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
2342                 {EMAIL_MAIL_ATTRIBUTE_SUBJECT, "subject", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_STRING},
2343                 {EMAIL_MAIL_ATTRIBUTE_DATE_TIME, "date_time",EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_TIME},
2344                 {EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_STATUS, "server_mail_status", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
2345                 {EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME, "server_mailbox_name", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_STRING},
2346                 {EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID, "server_mail_id", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_STRING},
2347                 {EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID, "message_id", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_STRING},
2348                 {EMAIL_MAIL_ATTRIBUTE_REFERENCE_MAIL_ID, "reference_mail_id", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
2349                 {EMAIL_MAIL_ATTRIBUTE_FROM, "full_address_from", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_STRING},
2350                 {EMAIL_MAIL_ATTRIBUTE_TO, "full_address_to", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_STRING},
2351                 {EMAIL_MAIL_ATTRIBUTE_CC, "full_address_cc", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_STRING},
2352                 {EMAIL_MAIL_ATTRIBUTE_BCC, "full_address_bcc", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_STRING},
2353                 {EMAIL_MAIL_ATTRIBUTE_BODY_DOWNLOAD_STATUS, "body_download_status", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
2354                 {EMAIL_MAIL_ATTRIBUTE_MAIL_SIZE, "mail_size", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
2355                 {EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN, "file_path_plain",EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_STRING},
2356                 {EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML,"file_path_html", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_STRING},
2357                 {EMAIL_MAIL_ATTRIBUTE_FILE_SIZE,"file_size", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
2358                 {EMAIL_MAIL_ATTRIBUTE_FLAGS_SEEN_FIELD,"flags_seen_field", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
2359                 {EMAIL_MAIL_ATTRIBUTE_FLAGS_DELETED_FIELD,"flags_deleted_field",EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER },
2360                 {EMAIL_MAIL_ATTRIBUTE_FLAGS_FLAGGED_FIELD,"flags_flagged_field", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
2361                 {EMAIL_MAIL_ATTRIBUTE_FLAGS_ANSWERED_FIELD,"flags_answered_field", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
2362                 {EMAIL_MAIL_ATTRIBUTE_FLAGS_RECENT_FIELD,"flags_recent_field", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
2363                 {EMAIL_MAIL_ATTRIBUTE_FLAGS_DRAFT_FIELD,"flags_draft_field", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
2364                 {EMAIL_MAIL_ATTRIBUTE_FLAGS_FORWARDED_FIELD,"flags_forwarded_field", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
2365                 {EMAIL_MAIL_ATTRIBUTE_DRM_STATUS,"drm_status", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
2366                 {EMAIL_MAIL_ATTRIBUTE_PRIORITY,"priority", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
2367                 {EMAIL_MAIL_ATTRIBUTE_SAVE_STATUS,"save_status", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
2368                 {EMAIL_MAIL_ATTRIBUTE_LOCK_STATUS,"lock_status", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
2369                 {EMAIL_MAIL_ATTRIBUTE_REPORT_STATUS,"report_status", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
2370                 {EMAIL_MAIL_ATTRIBUTE_ATTACHMENT_COUNT,"attachment_count", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
2371                 {EMAIL_MAIL_ATTRIBUTE_INLINE_CONTENT_COUNT,"inline_content_count", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
2372                 {EMAIL_MAIL_ATTRIBUTE_THREAD_ID,"thread_id", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
2373                 {EMAIL_MAIL_ATTRIBUTE_THREAD_ITEM_COUNT,"thread_item_count", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
2374                 {EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT,"preview_text", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_STRING},
2375                 {EMAIL_MAIL_ATTRIBUTE_MEETING_REQUEST_STATUS,"meeting_request_status", },
2376                 {EMAIL_MAIL_ATTRIBUTE_MESSAGE_CLASS,"message_class", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
2377                 {EMAIL_MAIL_ATTRIBUTE_DIGEST_TYPE,"digest_type", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
2378                 {EMAIL_MAIL_ATTRIBUTE_SMIME_TYPE,"smime_type", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
2379                 {EMAIL_MAIL_ATTRIBUTE_SCHEDULED_SENDING_TIME,"scheduled_sending_time", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_TIME},
2380                 {EMAIL_MAIL_ATTRIBUTE_REMAINING_RESEND_TIMES,"remaining_resend_times", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
2381                 {EMAIL_MAIL_ATTRIBUTE_TAG_ID,"tag_id", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
2382                 {EMAIL_MAIL_ATTRIBUTE_REPLIED_TIME,"replied_time", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_TIME},
2383                 {EMAIL_MAIL_ATTRIBUTE_FORWARDED_TIME,"forwarded_time", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_TIME},
2384                 {EMAIL_MAIL_ATTRIBUTE_RECIPIENT_ADDRESS,"email_recipient_address", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_STRING},
2385                 {EMAIL_MAIL_ATTRIBUTE_EAS_DATA_LENGTH_TYPE,"eas_data_length", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_INTEGER},
2386                 {EMAIL_MAIL_ATTRIBUTE_EAS_DATA_TYPE,"eas_data", EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_BINARY}
2387 };
2388
2389 INTERNAL_FUNC char* emcore_get_mail_field_name_by_attribute_type(email_mail_attribute_type input_attribute_type)
2390 {
2391         EM_DEBUG_FUNC_BEGIN("input_attribute_type [%d]", input_attribute_type);
2392
2393         if(input_attribute_type > EMAIL_MAIL_ATTRIBUTE_EAS_DATA_TYPE || input_attribute_type < 0) {
2394                 EM_DEBUG_EXCEPTION("Invalid input_attribute_type [%d]", input_attribute_type);
2395                 return NULL;
2396         }
2397
2398         EM_DEBUG_LOG_DEV("return [%s]", _mail_attribute_info_array[input_attribute_type].attribute_name);
2399         return _mail_attribute_info_array[input_attribute_type].attribute_name;
2400 }
2401
2402 INTERNAL_FUNC int emcore_get_attribute_type_by_mail_field_name(char *input_mail_field_name, email_mail_attribute_type *output_mail_attribute_type)
2403 {
2404         EM_DEBUG_FUNC_BEGIN("input_mail_field_name [%p], output_mail_attribute_type [%p]", input_mail_field_name, output_mail_attribute_type);
2405         int i = 0;
2406         int err = EMAIL_ERROR_DATA_NOT_FOUND;
2407
2408         if(!input_mail_field_name || !output_mail_attribute_type) {
2409                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2410                 return EMAIL_ERROR_INVALID_PARAM;
2411         }
2412
2413         for (i = 0; i < EMAIL_MAIL_ATTRIBUTE_END; i++) {
2414                 if (EM_SAFE_STRCMP(input_mail_field_name, _mail_attribute_info_array[i].attribute_name) == 0) {
2415                         *output_mail_attribute_type = i;
2416                         err = EMAIL_ERROR_NONE;
2417                         break;
2418                 }
2419         }
2420
2421         EM_DEBUG_FUNC_END("found mail attribute type [%d]", (int)*output_mail_attribute_type);
2422         return err;
2423 }
2424
2425 INTERNAL_FUNC int emcore_get_mail_attribute_value_type(email_mail_attribute_type input_attribute_type, email_mail_attribute_value_type *output_value_type)
2426 {
2427         EM_DEBUG_FUNC_BEGIN("input_attribute_type[%d] output_value_type[%p]", input_attribute_type, output_value_type);
2428         int err = EMAIL_ERROR_NONE;
2429         int i = 0;
2430         email_mail_attribute_value_type value_type = EMAIL_MAIL_ATTRIBUTE_VALUE_TYPE_NONE;
2431
2432         if (output_value_type == NULL) {
2433                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2434                 err = EMAIL_ERROR_INVALID_PARAM;
2435                 goto FINISH_OFF;
2436         }
2437
2438         for(i = 0; i < EMAIL_MAIL_ATTRIBUTE_END; i++) {
2439                 if(input_attribute_type == _mail_attribute_info_array[i].attribute_type) {
2440                         value_type = _mail_attribute_info_array[i].attribute_value_type;
2441                         break;
2442                 }
2443         }
2444
2445         *output_value_type = value_type;
2446
2447         EM_DEBUG_LOG("value_type [%d]", value_type);
2448
2449 FINISH_OFF:
2450         EM_DEBUG_FUNC_END("err [%d]", err);
2451         return err;
2452 }
2453
2454 #ifdef __FEATURE_BODY_SEARCH__
2455 int emcore_strip_mail_body_from_file(char *multi_user_name, emstorage_mail_tbl_t *mail, char **stripped_text, int *err_code)
2456 {
2457         EM_DEBUG_FUNC_BEGIN("mail[%p]", mail);
2458
2459         int ret = false;
2460         int err = EMAIL_ERROR_NONE;
2461         char *buf = NULL;
2462         char *encoding_type = NULL;
2463         char *utf8_encoded_string = NULL;
2464     char *prefix_path = NULL;
2465     char real_html_path[255] = {0};
2466     char real_plain_path[255] = {0};
2467         gsize byte_read = 0;
2468         gsize byte_written = 0;
2469         GError *glib_error = NULL;
2470         FILE *fp_plain = NULL;
2471         struct stat  st_buf;
2472
2473         if (!mail) {
2474                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2475                 err = EMAIL_ERROR_INVALID_PARAM;
2476                 goto FINISH_OFF;
2477         }
2478
2479     if (EM_SAFE_STRLEN(multi_user_name) > 0) {
2480                 err = emcore_get_container_path(multi_user_name, &prefix_path);
2481                 if (err != EMAIL_ERROR_NONE) {
2482                         EM_DEBUG_EXCEPTION("emcore_get_container_path failed :[%d]", err);
2483                         goto FINISH_OFF;
2484                 }
2485         } else {
2486                 prefix_path = strdup("");
2487         }
2488
2489         /* read file to buf & strip if html text */
2490         if (mail->file_path_html) {
2491                 char result_buffer[MAX_PREVIEW_TEXT_LENGTH] = { 0, };
2492
2493                 if (mail->preview_text) {
2494                         *stripped_text = EM_SAFE_STRDUP(mail->preview_text);
2495                         ret = true;
2496                         goto FINISH_OFF;
2497                 }
2498
2499         SNPRINTF(real_html_path, sizeof(real_html_path), "%s%s", prefix_path, mail->file_path_html);
2500
2501                 if ((err = em_get_encoding_type_from_file_path(mail->file_path_plain, &encoding_type)) != EMAIL_ERROR_NONE) {
2502                         EM_DEBUG_EXCEPTION("em_get_encoding_type_from_file_path failed [%d]", err);
2503                         goto FINISH_OFF;
2504                 }
2505
2506                 if ((err = emcore_strip_HTML_tag(real_html_path, encoding_type, result_buffer, MAX_PREVIEW_TEXT_LENGTH)) != EMAIL_ERROR_NONE) {
2507                         EM_DEBUG_EXCEPTION("emcore_strip_HTML_tag failed");
2508                         goto FINISH_OFF;
2509                 }
2510
2511                 *stripped_text = EM_SAFE_STRDUP(result_buffer);
2512         }
2513
2514         if (!buf && mail->file_path_plain) {
2515         SNPRINTF(real_plain_path, sizeof(real_plain_path), "%s%s", prefix_path, mail->file_path_plain);
2516
2517                 if ((err = em_get_encoding_type_from_file_path(mail->file_path_plain, &encoding_type)) != EMAIL_ERROR_NONE) {
2518                         EM_DEBUG_EXCEPTION("em_get_encoding_type_from_file_path failed [%d]", err);
2519                         goto FINISH_OFF;
2520                 }
2521
2522                 memset(&st_buf, 0, sizeof(struct stat));
2523                 if (stat(real_plain_path, &st_buf) < 0) {
2524                         EM_DEBUG_EXCEPTION_SEC("stat(\"%s\") failed...", real_plain_path);
2525                         err = EMAIL_ERROR_INVALID_MAIL;
2526                         goto FINISH_OFF;
2527                 }
2528
2529                 err = em_fopen(real_plain_path, "r", &fp_plain);
2530                 if (err != EMAIL_ERROR_NONE) {
2531                         EM_DEBUG_EXCEPTION_SEC("em_fopen failed [%s]", real_plain_path);
2532                         goto FINISH_OFF;
2533                 }
2534
2535                 if (S_ISREG(st_buf.st_mode) && st_buf.st_size == 0) {
2536                         EM_DEBUG_LOG("text_file is empty size");
2537                         err = EMAIL_ERROR_EMPTY_FILE;
2538                         goto FINISH_OFF;
2539                 }
2540
2541                 if (!(buf = (char*)em_malloc(sizeof(char)*(st_buf.st_size + 1)))) {
2542                         EM_DEBUG_EXCEPTION("em_malloc failed");
2543                         goto FINISH_OFF;
2544                 }
2545
2546                 byte_read = fread(buf, sizeof(char), st_buf.st_size, fp_plain);
2547
2548                 if (byte_read <= 0) {
2549                         EM_SAFE_FREE(buf);
2550                         err = EMAIL_ERROR_NULL_VALUE;
2551                         if (ferror(fp_plain)) {
2552                                 EM_DEBUG_EXCEPTION_SEC("fread failed [%s]", real_plain_path);
2553                                 err = EMAIL_ERROR_SYSTEM_FAILURE;
2554                         }
2555                         goto FINISH_OFF;
2556                 }
2557                 reg_replace(buf, CR_STRING, " ");
2558                 reg_replace(buf, LF_STRING, " ");
2559                 reg_replace(buf, TAB_STRING, " ");
2560         }
2561
2562         if (buf) {
2563                 em_trim_left(buf);
2564                 if(encoding_type && strcasecmp(encoding_type, "UTF-8") != 0) {
2565                         EM_DEBUG_LOG("encoding_type [%s]", encoding_type);
2566
2567                         if (strcasecmp(encoding_type, "ks_c_5601-1987") == 0 ||
2568                                         strcasecmp(encoding_type, "ksc5601") == 0 ||
2569                                         strcasecmp(encoding_type, "cp949") == 0) {
2570                                 EM_SAFE_FREE(encoding_type);
2571                                 encoding_type = EM_SAFE_STRDUP("EUC-KR");
2572                         }
2573
2574                         utf8_encoded_string = (char*)g_convert(buf, -1, "UTF-8", encoding_type, &byte_read, &byte_written, &glib_error);
2575
2576                         if(utf8_encoded_string == NULL) {
2577                                 if (!g_error_matches(glib_error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE)) {
2578                             EM_SAFE_FREE(*stripped_text);
2579                                         *stripped_text = EM_SAFE_STRDUP(buf);
2580                                         goto FINISH_OFF;
2581                                 }
2582                                 EM_DEBUG_LOG("Extract the preview text, again");
2583
2584                                 utf8_encoded_string = (char *)g_convert(buf, -1, "UTF-8", encoding_type, &byte_read, &byte_written, &glib_error);
2585                                 if (utf8_encoded_string == NULL) {
2586                                         EM_SAFE_FREE(*stripped_text);
2587                                         *stripped_text = EM_SAFE_STRDUP(buf);
2588                                         goto FINISH_OFF;
2589                                 }
2590                         }
2591                         EM_SAFE_FREE(buf);
2592
2593             EM_SAFE_FREE(*stripped_text);
2594                         *stripped_text = EM_SAFE_STRDUP(utf8_encoded_string);
2595                 } else {
2596             EM_SAFE_FREE(*stripped_text);
2597                         *stripped_text = EM_SAFE_STRDUP(buf);
2598                 }
2599         }
2600
2601         ret = true;
2602
2603 FINISH_OFF:
2604
2605         if (err_code)
2606                 *err_code = err;
2607
2608         EM_SAFE_FREE(buf);
2609         EM_SAFE_FREE(prefix_path);
2610         EM_SAFE_FREE(encoding_type);
2611         EM_SAFE_FREE(utf8_encoded_string);
2612
2613         if (fp_plain != NULL)
2614                 fclose(fp_plain);
2615
2616         EM_DEBUG_FUNC_END("ret [%d]", ret);
2617         return ret;
2618 }
2619 #endif
2620
2621
2622 int emcore_get_preview_text_from_file(char *multi_user_name, const char *input_plain_path, const char *input_html_path, int input_preview_buffer_length, char **output_preview_buffer)
2623 {
2624         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);
2625
2626         int          err = EMAIL_ERROR_NONE;
2627         gsize        byte_read = 0;
2628         gsize        byte_written = 0;
2629         int          local_preview_buffer_length = 0;
2630         char        *local_preview_text = NULL;
2631         char        *encoding_type = NULL;
2632         char        *utf8_encoded_string = NULL;
2633         FILE        *fp_plain = NULL;
2634         GError      *glib_error = NULL;
2635         struct stat  st_buf;
2636     char        *prefix_path = NULL;
2637     char         real_file_path[255] = {0};
2638
2639         if (!output_preview_buffer) {
2640                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2641                 err = EMAIL_ERROR_INVALID_PARAM;
2642                 goto FINISH_OFF;
2643         }
2644
2645         local_preview_buffer_length = input_preview_buffer_length * 2;
2646
2647     if (EM_SAFE_STRLEN(multi_user_name) > 0) {
2648                 err = emcore_get_container_path(multi_user_name, &prefix_path);
2649                 if (err != EMAIL_ERROR_NONE) {
2650                         EM_DEBUG_EXCEPTION("emcore_get_container_path failed :[%d]", err);
2651                         goto FINISH_OFF;
2652                 }
2653         } else {
2654                 prefix_path = strdup("");
2655         }
2656         
2657         if (input_html_path) { /*prevent 26249*/
2658                 /*      get preview text from html file */
2659                 char result_buffer[MAX_PREVIEW_TEXT_LENGTH] = { 0, };
2660
2661         memset(real_file_path, 0x00, sizeof(real_file_path));
2662         SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, input_html_path);
2663
2664                 if( (err = em_get_encoding_type_from_file_path(input_html_path, &encoding_type)) != EMAIL_ERROR_NONE) {
2665                         EM_DEBUG_EXCEPTION("em_get_encoding_type_from_file_path failed [%d]", err);
2666                         goto FINISH_OFF;
2667                 }
2668
2669                 if ((err = emcore_strip_HTML_tag(real_file_path, encoding_type, result_buffer, MAX_PREVIEW_TEXT_LENGTH)) != EMAIL_ERROR_NONE) {
2670                         EM_DEBUG_EXCEPTION("emcore_strip_HTML_tag failed");
2671                         goto FINISH_OFF;
2672                 }
2673                 local_preview_text = EM_SAFE_STRDUP(result_buffer);
2674         }
2675
2676         if (!local_preview_text && input_plain_path) { /*prevent 26249*/
2677
2678         memset(real_file_path, 0x00, sizeof(real_file_path));
2679         SNPRINTF(real_file_path, sizeof(real_file_path), "%s%s", prefix_path, input_plain_path);
2680
2681                 /*  get preview text from plain text file */
2682                 if( (err = em_get_encoding_type_from_file_path(input_plain_path, &encoding_type)) != EMAIL_ERROR_NONE) {
2683                         EM_DEBUG_EXCEPTION("em_get_encoding_type_from_file_path failed [%d]", err);
2684                         goto FINISH_OFF;
2685                 }
2686
2687                 memset(&st_buf, 0, sizeof(struct stat));
2688                 if (stat(real_file_path, &st_buf) < 0)  {
2689                         EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", input_plain_path);
2690                         err = EMAIL_ERROR_INVALID_MAIL;
2691                         goto FINISH_OFF;
2692                 }
2693
2694                 err = em_fopen(real_file_path, "r", &fp_plain);
2695                 if (err != EMAIL_ERROR_NONE) {
2696                         EM_DEBUG_EXCEPTION("em_fopen failed [%s]", input_plain_path);
2697                         goto FINISH_OFF;
2698                 }
2699
2700                 if (S_ISREG(st_buf.st_mode) && st_buf.st_size == 0) {
2701                         EM_DEBUG_LOG("input_text_file is empty size");
2702                         err = EMAIL_ERROR_EMPTY_FILE;
2703                         goto FINISH_OFF;
2704                 }
2705
2706                 if (!(local_preview_text = (char*)em_malloc(sizeof(char) * local_preview_buffer_length))) {
2707                         EM_DEBUG_EXCEPTION("em_malloc failed");
2708                         goto FINISH_OFF;
2709                 }
2710
2711                 byte_read = fread(local_preview_text, sizeof(char), local_preview_buffer_length - 1, fp_plain);
2712
2713                 if(byte_read <=0) { /*prevent 26249*/
2714                         EM_SAFE_FREE(local_preview_text);
2715                         err = EMAIL_ERROR_NULL_VALUE;
2716                         if (ferror(fp_plain)) {
2717                                 EM_DEBUG_EXCEPTION("fread failed [%s]", input_plain_path);
2718                                 err = EMAIL_ERROR_SYSTEM_FAILURE;
2719                         }
2720                         goto FINISH_OFF;
2721                 }
2722                 reg_replace(local_preview_text, CR_STRING, " ");
2723                 reg_replace(local_preview_text, LF_STRING, " ");
2724                 reg_replace(local_preview_text, TAB_STRING, " ");
2725
2726                 em_trim_left(local_preview_text);
2727
2728                 if(encoding_type && strcasecmp(encoding_type, "UTF-8") != 0) {
2729                         EM_DEBUG_LOG("encoding_type [%s]", encoding_type);
2730
2731                         if (strcasecmp(encoding_type, "ks_c_5601-1987") == 0 ||
2732                                         strcasecmp(encoding_type, "ksc5601") == 0 ||
2733                                         strcasecmp(encoding_type, "cp949") == 0) {
2734                                 EM_SAFE_FREE(encoding_type);
2735                                 encoding_type = EM_SAFE_STRDUP("EUC-KR");
2736                         }
2737
2738                         utf8_encoded_string = (char *)g_convert(local_preview_text, -1, "UTF-8", encoding_type, &byte_read, &byte_written, &glib_error);
2739
2740                         if(utf8_encoded_string == NULL) {
2741                                 if (!g_error_matches (glib_error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE)) {
2742                                         goto FINISH_OFF;
2743                                 }
2744                                 EM_DEBUG_LOG("Extract the preview text, again");
2745
2746                                 utf8_encoded_string = (char *)g_convert(local_preview_text, -1, "UTF-8", encoding_type, &byte_read, &byte_written, &glib_error);
2747                                 if (utf8_encoded_string == NULL) {
2748                                         EM_DEBUG_LOG("g_convert fail, again");
2749                                         goto FINISH_OFF;
2750                                 }
2751                         }
2752                         EM_SAFE_FREE(local_preview_text);
2753                         local_preview_text = utf8_encoded_string;
2754                 }
2755         }
2756
2757 FINISH_OFF:
2758 /*
2759         if (local_preview_text != NULL)
2760                 *output_preview_buffer = EM_SAFE_STRDUP(local_preview_text);
2761 */
2762         if (local_preview_text != NULL) {
2763                 EM_SAFE_FREE(*output_preview_buffer); /* valgrind */
2764                 *output_preview_buffer = EM_SAFE_STRDUP(local_preview_text);
2765         }
2766
2767         EM_SAFE_FREE(local_preview_text);
2768         EM_SAFE_FREE(encoding_type);
2769         EM_SAFE_FREE(prefix_path);
2770
2771         if (fp_plain != NULL)
2772                 fclose(fp_plain);
2773
2774         EM_DEBUG_FUNC_END("err [%d]", err);
2775         return err;
2776 }
2777
2778 INTERNAL_FUNC int emcore_add_transaction_info(int mail_id, int handle , int *err_code)
2779 {
2780         EM_DEBUG_FUNC_BEGIN("mail_id[%d], handle[%d]", mail_id, handle);
2781
2782         int ret = false;
2783         int err = EMAIL_ERROR_NONE ;
2784         em_transaction_info_type_t  *pTransinfo = NULL ;
2785         em_transaction_info_type_t      *pTemp = NULL;
2786
2787         EM_DEBUG_LOG("g_transaction_info_list[%p]", g_transaction_info_list);
2788         pTransinfo = g_transaction_info_list ;
2789
2790         if (!(pTemp = em_malloc(sizeof(em_transaction_info_type_t))))  {
2791                 EM_DEBUG_EXCEPTION("malloc failed...");
2792                 err = EMAIL_ERROR_OUT_OF_MEMORY;
2793                 goto FINISH_OFF;
2794         }
2795         pTemp->mail_id = mail_id ;
2796         pTemp->handle = handle;
2797
2798         if (!pTransinfo) {
2799                 pTransinfo = pTemp ;
2800                 g_transaction_info_list = pTransinfo ;
2801         }
2802         else {
2803                 while (pTransinfo->next)
2804                         pTransinfo = pTransinfo->next;
2805                 pTransinfo->next = pTemp;
2806         }
2807         ret = true ;
2808
2809 FINISH_OFF:
2810
2811         if (err_code)
2812                 *err_code = err;
2813         EM_DEBUG_FUNC_END("g_transaction_info_list[%p]", g_transaction_info_list);
2814         return ret;
2815 }
2816
2817 INTERNAL_FUNC int emcore_get_handle_by_mailId_from_transaction_info(int mail_id, int *pHandle)
2818 {
2819         EM_DEBUG_FUNC_BEGIN("mail_id[%d], handle[%p]", mail_id, pHandle);
2820
2821         int ret = false;
2822         em_transaction_info_type_t  *pTransinfo = NULL ;
2823
2824         if (g_transaction_info_list == NULL) {
2825                 EM_DEBUG_EXCEPTION("g_transaction_info_list NULL");
2826                 return false;
2827         }
2828         pTransinfo = g_transaction_info_list;
2829
2830         do {
2831                 EM_DEBUG_LOG("pTransinfo->mail_id[%d]", pTransinfo->mail_id);
2832                 if (pTransinfo->mail_id == mail_id) {
2833                         *pHandle = pTransinfo->handle;
2834                         ret = true;
2835                         EM_DEBUG_LOG("*pHandle[%d]", *pHandle);
2836                         break;
2837                 }
2838                 else
2839                         pTransinfo = pTransinfo->next ;
2840         }while (pTransinfo);
2841         EM_DEBUG_FUNC_END();
2842         return ret;
2843 }
2844
2845 INTERNAL_FUNC int emcore_delete_transaction_info_by_mailId(int mail_id )
2846 {
2847         EM_DEBUG_FUNC_BEGIN("mail_id[%d]", mail_id);
2848
2849         em_transaction_info_type_t  *pTransinfo ;
2850         em_transaction_info_type_t *pTemp = NULL;
2851
2852         if (g_transaction_info_list == NULL) {
2853                 EM_DEBUG_EXCEPTION("g_transaction_info_list NULL");
2854                 return false;
2855         }
2856         pTransinfo = g_transaction_info_list;
2857
2858         EM_DEBUG_LOG("pTransinfo[%p]", pTransinfo);
2859
2860         do {
2861                 EM_DEBUG_LOG("pTransinfo->mail_id[%d]", pTransinfo->mail_id);
2862                 if (pTransinfo->mail_id == mail_id) {
2863                         pTemp = pTransinfo->next ;
2864                         if (!pTemp) {
2865                                 EM_SAFE_FREE(pTransinfo) ;
2866                                 g_transaction_info_list = NULL;
2867                         }
2868                         else {
2869                                 pTransinfo->mail_id = pTransinfo->next->mail_id;
2870                                 pTransinfo->handle = pTransinfo->next->handle ;
2871                                 pTransinfo->next = pTransinfo->next->next;
2872
2873                                 EM_SAFE_FREE(pTemp);
2874                         }
2875                         break;
2876                 }
2877                 else {
2878                         pTransinfo = pTransinfo->next ;
2879                 }
2880
2881         }while (pTransinfo);
2882         EM_DEBUG_FUNC_END();
2883         return true;
2884 }
2885
2886
2887 #include <regex.h>
2888
2889 INTERNAL_FUNC int reg_replace (char *input_source_text, char *input_old_pattern_string, char *input_new_string)
2890 {
2891         int         error_code = EMAIL_ERROR_NONE;
2892         char       *pos = NULL;
2893         int         so, n, nmatch, source_text_length, n_count = 1;
2894         regmatch_t *pmatch = NULL;
2895         regex_t     reg_pattern;
2896
2897         if(!input_source_text || !input_old_pattern_string || !input_new_string) {
2898                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2899                 error_code = EMAIL_ERROR_INVALID_PARAM;
2900                 goto FINISH_OFF;
2901         }
2902
2903         source_text_length = EM_SAFE_STRLEN(input_source_text);
2904
2905         if (regcomp(&reg_pattern, input_old_pattern_string, REG_EXTENDED | REG_ICASE) != 0) {
2906                 EM_DEBUG_EXCEPTION("regcomp failed");
2907                 goto FINISH_OFF;
2908         }
2909
2910         nmatch = reg_pattern.re_nsub + 1;
2911
2912         EM_DEBUG_LOG("nmatch [%d]", nmatch);
2913
2914         if(nmatch < 1) {
2915                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_DATA");
2916                 error_code = EMAIL_ERROR_INVALID_DATA;
2917                 goto FINISH_OFF;
2918         }
2919
2920         pmatch = (regmatch_t*)em_malloc(sizeof(regmatch_t) * nmatch);
2921
2922         if(pmatch == NULL) {
2923                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_OUT_OF_MEMORY");
2924                 error_code = EMAIL_ERROR_OUT_OF_MEMORY;
2925                 goto FINISH_OFF;
2926         }
2927
2928         for (pos = input_new_string; *pos ; pos++) {
2929                 if (*pos == '\\' && *(pos + 1) > '0' && *(pos + 1) <= '9') {
2930
2931                         so = pmatch[*(pos + 1) - 48].rm_so;
2932                         n  = pmatch[*(pos + 1) - 48].rm_eo - so;
2933
2934                         EM_DEBUG_LOG("so [%d], n [%d]", so, n);
2935
2936                         if (so < 0 || EM_SAFE_STRLEN (input_new_string) + n - 1 > source_text_length)
2937                                 break;
2938
2939                         memmove (pos + n, pos + 2, EM_SAFE_STRLEN (pos) - 1);
2940                         memmove (pos, input_source_text + so, n);
2941                         pos = pos + n - 2;
2942                 }
2943         }
2944
2945         for (pos = input_source_text; !regexec (&reg_pattern, pos, 1, pmatch, 0);) {
2946                 n = pmatch[0].rm_eo - pmatch[0].rm_so;
2947                 pos += pmatch[0].rm_so;
2948
2949                 memmove (pos + EM_SAFE_STRLEN (input_new_string), pos + n, EM_SAFE_STRLEN (pos) - n + 1);
2950                 memmove (pos, input_new_string, EM_SAFE_STRLEN (input_new_string));
2951                 pos += EM_SAFE_STRLEN (input_new_string);
2952                 n_count++;
2953         }
2954
2955 FINISH_OFF:
2956
2957         EM_SAFE_FREE(pmatch);
2958         regfree (&reg_pattern);
2959
2960         EM_DEBUG_FUNC_END("error_code [%d]", error_code);
2961         return error_code;
2962 }
2963
2964
2965 INTERNAL_FUNC char *reg_replace_new(char *input_source_text, char *input_old_pattern_string, char *input_new_string)
2966 {
2967         char *replaced_str = NULL;
2968         GRegex *regex = NULL;
2969         GError *error = NULL;
2970
2971         if (input_source_text == NULL) {
2972                 EM_DEBUG_EXCEPTION("Invalid parameter");
2973                 return NULL;
2974         }
2975
2976         regex = g_regex_new(input_old_pattern_string, G_REGEX_RAW | G_REGEX_OPTIMIZE, 0, &error);
2977         if (!regex) {
2978                 EM_DEBUG_LOG("g_regex_new failed");
2979                 goto FINISH_OFF;
2980         }
2981
2982         replaced_str = g_regex_replace_literal(regex, 
2983                                                                                         input_source_text, 
2984                                                                                         strlen(input_source_text), 
2985                                                                                         0, 
2986                                                                                         input_new_string, 
2987                                                                                         0, 
2988                                                                                         &error);
2989         if (replaced_str == NULL) {
2990                 EM_DEBUG_EXCEPTION("g_regex_replace_literal failed : [%s][%d]", error->message, error->code);
2991                 goto FINISH_OFF;
2992         }
2993
2994 FINISH_OFF:
2995         
2996         if (regex) g_regex_unref(regex);
2997
2998         return replaced_str;
2999 }
3000
3001
3002 #include <libxml/HTMLparser.h>
3003
3004 static void emcore_get_content_string(xmlNode *input_node, char *input_result_buffer, int input_result_buffer_length, int *input_exit_flag)
3005 {
3006     xmlNode *cur_node = NULL;
3007     char    *temp_content_string = NULL;
3008
3009     if (input_node == NULL || input_result_buffer == NULL || input_exit_flag == NULL) {
3010         EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3011         goto FINISH_OFF;
3012     }
3013
3014     for (cur_node = input_node; cur_node; cur_node = cur_node->next) {
3015                 if (cur_node->name && (strcasecmp((const char *)cur_node->name, "head") == 0 || strcasecmp((const char *)cur_node->name, "title") == 0))
3016                         continue;
3017
3018                 if (cur_node->name && (strcasecmp((const char *)cur_node->name, "BR") == 0)) {
3019                         if (EM_SAFE_STRLEN(input_result_buffer) + 1 >= input_result_buffer_length) {
3020                                 *input_exit_flag = 1;
3021                                 break;
3022                         } else {
3023                                 EM_SAFE_STRCAT(input_result_buffer, " ");
3024                                 continue;
3025                         }
3026                 }
3027
3028                 if (cur_node->type == XML_TEXT_NODE && cur_node->content) {
3029                         if ((EM_SAFE_STRLEN(cur_node->content) + EM_SAFE_STRLEN(input_result_buffer)) >= input_result_buffer_length) {
3030                                 int len1 = EM_SAFE_STRLEN(input_result_buffer);
3031
3032                                 if (len1 >= 0 && len1 < input_result_buffer_length) {
3033                                         int remain_size = input_result_buffer_length - len1 - 2;
3034                                         EM_DEBUG_LOG("remain_size : [%d], len1 : [%d]", remain_size, len1);
3035                                         char *remain_str = NULL;
3036
3037                                         if (remain_size > 0)
3038                                                 remain_str = g_strndup((char *)(cur_node->content), remain_size);
3039
3040                                         if (remain_str) {
3041                                                 char *replaced_string = NULL;
3042                                                 replaced_string = reg_replace_new(remain_str, "[ \t\r\n\v\f]+", " ");
3043                                                 EM_SAFE_STRCAT(input_result_buffer, replaced_string);
3044                                                 EM_SAFE_FREE(replaced_string);
3045                                                 free(remain_str);
3046                                         }
3047                                 }
3048
3049                                 *input_exit_flag = 1;
3050                                 break;
3051                         }
3052
3053                         temp_content_string = EM_SAFE_STRDUP((const char *)cur_node->content);
3054                         if (temp_content_string) {
3055                                 char *replaced_string = NULL;
3056                                 replaced_string = reg_replace_new(temp_content_string, "[ \t\r\n\v\f]+", " ");
3057                                 EM_SAFE_STRCAT(input_result_buffer, replaced_string);
3058                                 EM_SAFE_FREE(replaced_string);
3059                                 free(temp_content_string);
3060                         }
3061                 }
3062
3063                 if (cur_node->children)
3064                         emcore_get_content_string(cur_node->children, input_result_buffer, input_result_buffer_length, input_exit_flag);
3065
3066                 if (*input_exit_flag == 1)
3067                         break;
3068     }
3069
3070 FINISH_OFF:
3071
3072         return;
3073 }
3074
3075 static void emcore_default_xml_error_handler(void *ctx, const char *msg, ...) {
3076         EM_DEBUG_EXCEPTION("htmlReadFile failed");
3077 }
3078
3079 int emcore_strip_HTML_tag(const char *input_html_file_path, char *input_encoding_type, char *output_result_buffer, int input_result_buffer_legnth)
3080 {
3081         EM_DEBUG_FUNC_BEGIN("input_html_file_path[%p] input_encoding_type[%s] output_result_buffer[%p] input_result_buffer_legnth[%d]", input_html_file_path, input_encoding_type, output_result_buffer, input_result_buffer_legnth);
3082         int err = EMAIL_ERROR_NONE;
3083         int exit_flag = 0;
3084         char *result_string = NULL;
3085         xmlNode *root_element = NULL;
3086         htmlDocPtr result_html_document = NULL;
3087         char *encoding_type = NULL;
3088         char *utf_charset = "UTF-8";
3089         char *euc_kr_charset = "euc-kr";
3090
3091         if (input_html_file_path == NULL || output_result_buffer == NULL || input_result_buffer_legnth == 0) {
3092                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3093                 err = EMAIL_ERROR_INVALID_PARAM;
3094                 goto FINISH_OFF;
3095         }
3096
3097         result_string = em_malloc(sizeof(char) * input_result_buffer_legnth);
3098         if (result_string == NULL) {
3099                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_OUT_OF_MEMORY");
3100                 err = EMAIL_ERROR_OUT_OF_MEMORY;
3101                 goto FINISH_OFF;
3102         }
3103
3104         if (EM_SAFE_STRCMP(input_encoding_type, "unknown") == 0)
3105                 encoding_type = utf_charset;
3106         else if (input_encoding_type && (strcasecmp(input_encoding_type, "ks_c_5601-1987") == 0 ||
3107                 strcasecmp(input_encoding_type, "ksc5601") == 0 ||
3108                 strcasecmp(input_encoding_type, "cp949") == 0))
3109                 encoding_type = euc_kr_charset;
3110         else
3111                 encoding_type = input_encoding_type;
3112
3113         EM_DEBUG_LOG("encoding_type [%s]", encoding_type);
3114
3115         LIBXML_TEST_VERSION
3116         xmlSetGenericErrorFunc(NULL, emcore_default_xml_error_handler);
3117         result_html_document = htmlReadFile(input_html_file_path, encoding_type, HTML_PARSE_RECOVER | HTML_PARSE_NOWARNING | HTML_PARSE_NOERROR | HTML_PARSE_NONET | HTML_PARSE_NOBLANKS);
3118         if (result_html_document == NULL) {
3119                 EM_DEBUG_EXCEPTION("htmlReadFile failed");
3120                 err = EMAIL_ERROR_ON_PARSING;
3121                 goto FINISH_OFF;
3122         }
3123
3124         root_element = xmlDocGetRootElement(result_html_document);
3125
3126         emcore_get_content_string(root_element, result_string, input_result_buffer_legnth, &exit_flag);
3127         char *replaced_string = reg_replace_new(result_string, "[ \t\r\n\v\f]+", " ");
3128         EM_SAFE_STRCPY(output_result_buffer, replaced_string);
3129         EM_SAFE_FREE(replaced_string);
3130         em_trim_left(output_result_buffer);
3131
3132 FINISH_OFF:
3133         if (result_html_document) {
3134                 xmlFreeDoc(result_html_document);
3135         }
3136
3137         EM_SAFE_FREE(result_string);
3138
3139         EM_DEBUG_FUNC_END("err [%d]", err);
3140         return err;
3141 }
3142
3143 INTERNAL_FUNC int emcore_send_noti_for_new_mail(int account_id, char *mailbox_name, char *subject, char *from, char *uid, char *datetime)
3144 {
3145         EM_DEBUG_FUNC_BEGIN_SEC("mailbox_name(%s) subject(%s), from(%s), uid(%s), datetime(%s)", mailbox_name, subject, from, uid, datetime);
3146         int error_code = EMAIL_ERROR_NONE;
3147         int param_length = 0;
3148         char *param_string = NULL;
3149
3150         if (mailbox_name == NULL || subject == NULL || from == NULL || uid == NULL || datetime == NULL) {
3151                 error_code = EMAIL_ERROR_INVALID_PARAM;
3152                 EM_DEBUG_EXCEPTION("Invalid parameter, mailbox_name(%p), subject(%p), from(%p), uid(%p), datetime(%p)", mailbox_name, subject, from, uid, datetime);
3153                 goto FINISH_OFF;
3154         }
3155
3156         param_length = strlen(mailbox_name) + strlen(subject) + strlen(from) + strlen(uid) + strlen(datetime) + 5; /*prevent 34358*/
3157
3158         param_string = em_malloc(param_length);
3159         if (param_string == NULL) {
3160                 error_code = EMAIL_ERROR_OUT_OF_MEMORY;
3161                 EM_DEBUG_EXCEPTION("Memory allocation for 'param_string' is failed");
3162                 goto FINISH_OFF;
3163         }
3164
3165         memset(param_string, 0x00, param_length);
3166
3167         SNPRINTF(param_string, param_length, "%s%c%s%c%s%c%s%c%s", mailbox_name, 0x01, subject, 0x01, from, 0x01, uid, 0x01, datetime);
3168
3169         if (emcore_notify_network_event(NOTI_DOWNLOAD_NEW_MAIL, account_id, param_string, 0, 0) == 0) { /*  failed */
3170                 error_code = EMAIL_ERROR_UNKNOWN;
3171                 EM_DEBUG_EXCEPTION("emcore_notify_network_event is failed");
3172                 goto FINISH_OFF;
3173         }
3174
3175 FINISH_OFF:
3176
3177         EM_SAFE_FREE(param_string);
3178         EM_DEBUG_FUNC_END();
3179         return error_code;
3180 }
3181
3182 #define MAX_TITLE_LENGTH 1024
3183
3184 #ifdef __FEATURE_DRIVING_MODE__
3185 int convert_app_err_to_email_err(int app_error)
3186 {
3187         int err = EMAIL_ERROR_NONE;
3188
3189         switch(app_error) {
3190         case APP_CONTROL_ERROR_NONE :
3191                 err = EMAIL_ERROR_NONE;
3192                 break;
3193         case APP_CONTROL_ERROR_INVALID_PARAMETER :
3194                 err = EMAIL_ERROR_INVALID_PARAM;
3195                 break;
3196         case APP_CONTROL_ERROR_OUT_OF_MEMORY :
3197                 err = EMAIL_ERROR_OUT_OF_MEMORY;
3198                 break;
3199         default:
3200                 err = EMAIL_ERROR_UNKNOWN;
3201                 break;
3202         }
3203
3204         return err;
3205 }
3206
3207 INTERNAL_FUNC int emcore_start_driving_mode(char *multi_user_name, int mail_id)
3208 {
3209         EM_DEBUG_FUNC_BEGIN();
3210         int err = APP_CONTROL_ERROR_NONE;
3211         int tts_enable = 0;
3212         char string_mail[10] = { 0 };
3213         emstorage_mail_tbl_t *p_mail_data = NULL;
3214         app_control_h service = NULL;
3215
3216         if (vconf_get_bool(VCONFKEY_SETAPPL_DRIVINGMODE_DRIVINGMODE, &tts_enable) != 0) {
3217                 EM_DEBUG_EXCEPTION("vconf_get_int failed");
3218                 goto FINISH_OFF;
3219         }
3220
3221         EM_DEBUG_LOG("Driving Mode : [%d]", tts_enable);
3222
3223         if (!tts_enable)
3224                 goto FINISH_OFF;
3225
3226
3227         if (vconf_get_bool(VCONFKEY_SETAPPL_DRIVINGMODE_NEWEMAILS, &tts_enable) != 0) {
3228                 EM_DEBUG_EXCEPTION("vconf_get_int failed");
3229                 goto FINISH_OFF;
3230         }
3231
3232         EM_DEBUG_LOG("New emails of driving Mode : [%d]", tts_enable);
3233
3234         if (!tts_enable)
3235                 goto FINISH_OFF;
3236
3237         if (!emstorage_get_mail_by_id(multi_user_name, mail_id, &p_mail_data, false, NULL)) {
3238                 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed");
3239                 goto FINISH_OFF;
3240         }
3241
3242         if (p_mail_data->tag_id >= 0)
3243                 goto FINISH_OFF;
3244
3245         SNPRINTF(string_mail, sizeof(string_mail), "%d", mail_id);
3246
3247         err = app_control_create(&service);
3248         if (err != APP_CONTROL_ERROR_NONE) {
3249                 EM_DEBUG_EXCEPTION("app_control_create failed : [%d]", err);
3250                 goto FINISH_OFF;
3251         }
3252
3253         err = app_control_set_app_id(service, "org.tizen.email-tts-play");
3254         if (err != APP_CONTROL_ERROR_NONE) {
3255                 EM_DEBUG_EXCEPTION("app_control_set_app_id failed : [%d]", err);
3256                 goto FINISH_OFF;
3257         }
3258
3259         err = app_control_add_extra_data(service, "tts_email_id", string_mail);
3260         if (err != APP_CONTROL_ERROR_NONE) {
3261                 EM_DEBUG_EXCEPTION("app_control_add_extra_data failed : [%d]", err);
3262                 goto FINISH_OFF;
3263         }
3264
3265         err = app_control_send_launch_request(service, NULL, NULL);
3266         if (err != APP_CONTROL_ERROR_NONE) {
3267                 EM_DEBUG_EXCEPTION("app_control_send_launch_request failed : [%d]", err);
3268                 goto FINISH_OFF;
3269         }
3270
3271 FINISH_OFF:
3272
3273         if (service)
3274                 app_control_destroy(service);
3275
3276         if (p_mail_data)
3277                 emstorage_free_mail(&p_mail_data, 1, NULL);
3278
3279         EM_DEBUG_FUNC_END();
3280         return convert_app_err_to_email_err(err);
3281 }
3282 #endif /* __FEATURE_DRIVING_MODE__ */
3283
3284 INTERNAL_FUNC int emcore_clear_notifications(char *multi_user_name, int account_id)
3285 {
3286         int account_count = 0, i;
3287         emstorage_account_tbl_t *account_list;
3288         int error_code = EMAIL_ERROR_NONE;
3289
3290         if (account_id == ALL_ACCOUNT) {
3291                 if(!emstorage_get_account_list(multi_user_name, &account_count, &account_list, true, false, &error_code)) {
3292                         EM_DEBUG_EXCEPTION("emstorage_get_account_list failed");
3293                         goto FINISH_OFF;
3294                 }
3295
3296                 for(i = 0; i < account_count; i++) {
3297                         error_code = emcore_delete_notification_by_account(multi_user_name, account_list[i].account_id, true);
3298                         if (error_code != EMAIL_ERROR_NONE)
3299                                 EM_DEBUG_EXCEPTION("emcore_delete_notification_by_account failed");
3300                 }
3301         } else {
3302                 error_code = emcore_delete_notification_by_account(multi_user_name, account_id, true);
3303                 if (error_code != EMAIL_ERROR_NONE)
3304                         EM_DEBUG_EXCEPTION("emcore_delete_notification_by_account failed");
3305         }
3306
3307         if (!emstorage_update_save_status(multi_user_name, account_id, &error_code)) {
3308                EM_DEBUG_EXCEPTION("emstorage_update_save_status failed : [%d]", error_code);
3309                 goto FINISH_OFF;
3310         }
3311
3312 FINISH_OFF:
3313
3314         if(account_count) {
3315                 emstorage_free_account(&account_list, account_count, NULL);
3316         }
3317
3318         EM_DEBUG_FUNC_END("return[%d]", error_code);
3319         return error_code;
3320 }
3321
3322 #define EAS_EXECUTABLE_PATH "/usr/bin/eas-engine"
3323
3324 INTERNAL_FUNC int emcore_delete_notification_by_account(char *multi_user_name, int account_id, int with_noti_tray)
3325 {
3326         EM_DEBUG_FUNC_BEGIN("account_id [%d]", account_id);
3327         int error_code = EMAIL_ERROR_NONE;
3328         int private_id = 0;
3329         char vconf_private_id[MAX_PATH] = {0, };
3330
3331         SNPRINTF(vconf_private_id, sizeof(vconf_private_id), "%s/%d", VCONF_KEY_NOTI_PRIVATE_ID, account_id);
3332         if (vconf_get_int(vconf_private_id, &private_id) != 0) {
3333                 EM_DEBUG_EXCEPTION("vconf_get_int failed");
3334         }
3335 #ifdef __FEATURE_NOTIFICATION_ENABLE__
3336         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
3337
3338         if ((noti_err = notification_delete_by_priv_id(NATIVE_EMAIL_APPLICATION_PKG, NOTIFICATION_TYPE_NOTI, private_id)) != NOTIFICATION_ERROR_NONE) {
3339                 EM_DEBUG_EXCEPTION("notification_delete_by_priv_id failed [%d]", noti_err);
3340                 error_code = EMAIL_ERROR_NOTI;
3341         }
3342
3343         if (with_noti_tray) {
3344                 if ((noti_err = notification_delete_by_priv_id(NATIVE_EMAIL_APPLICATION_PKG, NOTIFICATION_TYPE_ONGOING, private_id)) != NOTIFICATION_ERROR_NONE) {
3345                         EM_DEBUG_EXCEPTION("notification_delete_by_priv_id failed [%d]", noti_err);
3346                         error_code = EMAIL_ERROR_NOTI;
3347                 }
3348         }
3349 #endif /* __FEATURE_NOTIFICATION_ENABLE__ */
3350         EM_DEBUG_FUNC_END();
3351         return error_code;
3352 }
3353
3354 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
3355
3356 /**
3357  * @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)
3358  * Prepare a linked list of uid ranges with each node having a uid_range and lowest and highest uid in it.
3359  *
3360  *@author                                       h.gahlaut@samsung.com
3361  * @param[in] id_set                    Specifies the array of mail_id and corresponding server_mail_id sorted by server_mail_ids in ascending order
3362  * @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
3363  * @param[in] range_len         Specifies the maximum length of string of range allowed.
3364  * @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
3365  * @param[out] err_code         Returns the error code.
3366  * @remarks                                     An example of a uid_range formed is 2:6,8,10,14:15,89,
3367  *                                                      While using it the caller should remove the ending, (comma)
3368  * @return This function returns true on success or false on failure.
3369  */
3370
3371 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)
3372 {
3373         EM_DEBUG_FUNC_BEGIN();
3374
3375         int ret = false;
3376         int error = EMAIL_ERROR_NONE;
3377
3378         if (NULL == id_set || id_set_count  <= 0 || NULL == uid_range_set) {
3379                 EM_DEBUG_EXCEPTION(" Invalid Parameter id_set[%p] id_set_count[%d] uid_range_set[%p]", id_set, id_set_count, uid_range_set);
3380                 error = EMAIL_ERROR_INVALID_PARAM;
3381                 goto FINISH_OFF;
3382         }
3383
3384         int i = 0;
3385         unsigned long current_uid = 0;
3386         unsigned long first_uid = 0;
3387         unsigned long last_uid = 0;
3388         const int max_subset_string_size = MAX_SUBSET_STRING_SIZE;
3389         char subset_string[MAX_SUBSET_STRING_SIZE] = {0,};
3390         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*/
3391
3392         if (range_len < (max_subset_string_size + 1))           /* 1 for ending NULL character */ {
3393                 EM_DEBUG_EXCEPTION(" Invalid Parameter range_len[%d]", range_len);
3394                 error = EMAIL_ERROR_INVALID_PARAM;
3395                 goto FINISH_OFF;
3396         }
3397
3398         EM_DEBUG_LOG("id set count[%d] range_len[%d]", id_set_count, range_len);
3399
3400         do {
3401                 first_uid = last_uid = current_uid = id_set[i].server_mail_id;
3402                 /* Start subset string by putting first server mail id in it from id_set*/
3403                 memset(subset_string, 0x00, max_subset_string_size);
3404                 SNPRINTF(subset_string, max_subset_string_size, "%lu", first_uid);
3405                 ++i;
3406
3407                 /* Check if only one server mail id was left in id_set */
3408                 if (i >= id_set_count) {
3409                         /* No more server mail id left in id_set */
3410                         if (false == emcore_append_subset_string_to_uid_range(subset_string, &current_node, uid_range_set, range_len, first_uid, last_uid)) {
3411                                 EM_DEBUG_EXCEPTION("emcore_append_subset_string_to_uid_range failed");
3412                                 goto FINISH_OFF;
3413                         }
3414                         break;
3415                 }
3416                 else {
3417                         /* 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 */
3418                         do {
3419                                 current_uid = id_set[i].server_mail_id;
3420                                 if (current_uid == (last_uid + 1)) {
3421                                         last_uid = current_uid;
3422                                         ++i;
3423                                 }
3424                                 else {
3425                                         memset(subset_string, 0x00, max_subset_string_size);
3426                                         if (first_uid != last_uid)      /* Form subset string by first_uid:last_uid */
3427                                                 SNPRINTF(subset_string, max_subset_string_size, "%lu:%lu", first_uid, last_uid);
3428                                         else    /* Form subset string by first_uid */
3429                                                 SNPRINTF(subset_string, max_subset_string_size, "%lu", first_uid);
3430
3431                                         if (false == emcore_append_subset_string_to_uid_range(subset_string, &current_node, uid_range_set, range_len, first_uid, last_uid)) {
3432                                                 EM_DEBUG_EXCEPTION("emcore_append_subset_string_to_uid_range failed");
3433                                                 goto FINISH_OFF;
3434                                         }
3435                                         /* To Start formation of new subset string break out of inner loop */
3436                                         break;
3437                                 }
3438
3439                         } while (i < id_set_count);
3440
3441                         /* Flow comes here in two cases :
3442                         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
3443                         2. due to break statement */
3444
3445                         if (last_uid == current_uid) {
3446                                 /* Case 1 */
3447
3448                                 memset(subset_string, 0x00, max_subset_string_size);
3449                                 SNPRINTF(subset_string, max_subset_string_size, "%lu:%lu", first_uid, last_uid);
3450
3451                                 if (false == emcore_append_subset_string_to_uid_range(subset_string, &current_node, uid_range_set, range_len, first_uid, last_uid)) {
3452                                         EM_DEBUG_EXCEPTION("emcore_append_subset_string_to_uid_range failed");
3453                                         goto FINISH_OFF;
3454                                 }
3455                         }
3456                         else {
3457                                 /* Case 2: Do Nothing */
3458                         }
3459
3460                 }
3461         } while (i < id_set_count);
3462
3463         ret = true;
3464
3465 FINISH_OFF:
3466         if (NULL != err_code)
3467                 *err_code = error;
3468         EM_DEBUG_FUNC_END();
3469         return ret;
3470
3471 }
3472
3473 /**
3474  * @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)
3475  * 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
3476  * and stores the subset_string in its uid_range. Also sets the lowest and highest uids for the corresponsing uid_range
3477  *
3478  * @author                                      h.gahlaut@samsung.com
3479  * @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.
3480  * @param[in] range_len         Specifies the maximum length of range string allowed.
3481  * @param[in] luid                      Specifies the lowest uid in subset string
3482  * @param[in] huid                      Specifies the highest uid in subset string
3483  * @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
3484  * @param[out] err_code         Returns the error code.
3485  * @remarks
3486  * @return This function returns true on success or false on failure.
3487  */
3488
3489 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)
3490 {
3491         EM_DEBUG_FUNC_BEGIN();
3492         email_uid_range_set *current_node = NULL;
3493
3494         if (NULL == (*uid_range_set)) {
3495                 /*This happens only once when list  creation starts. Head Node is allocated */
3496                 current_node = (email_uid_range_set *)em_malloc(sizeof(email_uid_range_set));
3497                 if (NULL == current_node) {
3498                         EM_DEBUG_EXCEPTION("em_malloc failed");
3499                         return false;
3500                 }
3501
3502                 current_node->uid_range = (char *)em_malloc(range_len);
3503
3504                 if (NULL == current_node->uid_range) {
3505                         EM_DEBUG_EXCEPTION("em_malloc failed");
3506                         EM_SAFE_FREE(current_node);
3507                         return false;
3508                 }
3509
3510                 SNPRINTF(current_node->uid_range, range_len, "%s,", subset_string);
3511
3512                 current_node->lowest_uid = luid;
3513                 current_node->highest_uid = huid;
3514                 (*uid_range_set) = current_node;
3515
3516                 (*current_node_adr) = current_node;
3517
3518         }
3519         else {
3520                 /* Apart from first call to this function flow will always come here */
3521                 current_node = (*current_node_adr);
3522                 int len_sub_string = EM_SAFE_STRLEN(subset_string);
3523                 int space_left_in_buffer = range_len - EM_SAFE_STRLEN(current_node->uid_range);
3524
3525                 if ((len_sub_string + 1 + 1) <= space_left_in_buffer)   /* 1 for comma + 1 for ending null character */ {
3526                         SNPRINTF(current_node->uid_range + EM_SAFE_STRLEN(current_node->uid_range), space_left_in_buffer, "%s,", subset_string);
3527                         current_node->highest_uid = huid;
3528                 }
3529                 else {
3530                         /* No more space left in uid_range string.If continued on it, it will exceeded max size of range_len */
3531                         /* Allocate new node in Uid Range set */
3532                         email_uid_range_set *new_node = NULL;
3533
3534                         new_node = (email_uid_range_set *)em_malloc(sizeof(email_uid_range_set));
3535
3536                         if (NULL == new_node) {
3537                                 EM_DEBUG_EXCEPTION("em_malloc failed");
3538                                 return false;
3539                         }
3540
3541                         /* Allocate uid_range of new node */
3542
3543                         new_node->uid_range =  (char *)em_malloc(range_len);
3544
3545                         if (NULL == new_node->uid_range) {
3546                                 EM_DEBUG_EXCEPTION("em_malloc failed");
3547                                 EM_SAFE_FREE(new_node);
3548                                 return false;
3549                         }
3550
3551                         SNPRINTF(new_node->uid_range, range_len, "%s, ", subset_string);
3552
3553                         new_node->lowest_uid = luid;
3554                         new_node->highest_uid = huid;
3555
3556                         current_node->next = new_node;
3557
3558                         (*current_node_adr) = new_node;
3559                 }
3560         }
3561         EM_DEBUG_FUNC_END();
3562         return true;
3563 }
3564
3565 /**
3566  * void emcore_free_uid_range_set(email_uid_range_set **uid_range_head)
3567  * Frees the linked list of uid ranges
3568  *
3569  * @author                                      h.gahlaut@samsung.com
3570  * @param[in] uid_range_head    Head pointer of linked list of uid ranges
3571  * @remarks
3572  * @return This function does not return anything.
3573  */
3574
3575 INTERNAL_FUNC
3576 void emcore_free_uid_range_set(email_uid_range_set **uid_range_set)
3577 {
3578         EM_DEBUG_FUNC_BEGIN();
3579
3580         email_uid_range_set *current_node = NULL;
3581         email_uid_range_set *uid_range_head = NULL;
3582
3583         current_node = uid_range_head = (*uid_range_set);       /* Make the current node and head ptr point to starting of  uid_range_set */
3584
3585         while (current_node) {
3586                 uid_range_head = current_node->next;            /* Move the head ptr to next node*/
3587
3588                 EM_SAFE_FREE(current_node->uid_range);
3589                 EM_SAFE_FREE(current_node);                             /* Free the current node */
3590
3591                 current_node = uid_range_head;                  /* Make the current node point to head ptr */
3592         }
3593
3594         (*uid_range_set) = NULL;
3595         EM_DEBUG_FUNC_END();
3596 }
3597
3598
3599 /**
3600  * @fn emcore_form_comma_separated_strings(int numbers[], int num_count, int max_string_len, char *** strings, int *string_count, int *err_code)
3601  * Forms comma separated strings of a give max_string_len from an array of numbers
3602  *
3603  * @author                                      h.gahlaut@samsung.com
3604  * @param[in] numbers                   Specifies the array of numbers to be converted into comma separated strings.
3605  * @param[in] num_count         Specifies the count of numbers in numbers array.
3606  * @param[in] max_string_len    Specifies the maximum length of comma separated strings that are to be formed.
3607  * @param[out] strings                  Returns the base address of a double dimension array which stores the strings.
3608  * @param[out] string_count             Returns the number of strings formed.
3609  * @param[out] err_code         Returns the error code.
3610  * @remarks                                     If Input to the function is five numbers like 2755 2754 2748 2749 2750 and a given max_string_len is 20.
3611  *                                                      Then this function will form two comma separated strings as follows -
3612  *                                                      "2755, 2754, 2748"
3613  *                                                      "2749, 2750"
3614  * @return This function returns true on success or false on failure.
3615  */
3616
3617 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)
3618 {
3619         EM_DEBUG_FUNC_BEGIN();
3620
3621         int error = EMAIL_ERROR_NONE;
3622         int ret = false;
3623
3624         char **string_list = NULL;
3625         int num_of_strings = 0;
3626         int i = 0;
3627         int j =0;
3628         char num[MAX_INTEGER_LENGTH + 1] = {0, };
3629         int num_len = 0;
3630         int space_in_buffer = 0;
3631         int len_of_string_formed = 0;
3632
3633         if (NULL == numbers || num_count <= 0 || \
3634                 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.  */ {
3635                 EM_DEBUG_EXCEPTION("Invalid Parameter numbers[%p] num_count [%d] max_string_len [%d] strings [%p] string_count[%p]", \
3636                         numbers, num_count, max_string_len, strings, string_count);
3637                 error = EMAIL_ERROR_INVALID_PARAM;
3638                 goto FINISH_OFF;
3639         }
3640
3641         EM_DEBUG_LOG("num_count [%d] max_string_len [%d]", num_count, max_string_len);
3642
3643         string_list = em_malloc(sizeof(char *));
3644
3645         if (NULL == string_list) {
3646                 EM_DEBUG_EXCEPTION("em_malloc failed ");
3647                 goto FINISH_OFF;
3648         }
3649
3650         string_list[num_of_strings] = em_malloc(max_string_len);
3651
3652         if (NULL == string_list[num_of_strings]) {
3653                 EM_DEBUG_EXCEPTION("em_malloc failed ");
3654                 goto FINISH_OFF;
3655         }
3656
3657         ++num_of_strings;
3658         space_in_buffer = max_string_len;
3659
3660         for (j = 0; j < num_count;++j) {
3661                 memset(num, 0x00, MAX_INTEGER_LENGTH + 1);
3662                 SNPRINTF(num, MAX_INTEGER_LENGTH + 1, "%d", numbers[j]);
3663
3664                 num_len = EM_SAFE_STRLEN(num);
3665
3666                 len_of_string_formed = EM_SAFE_STRLEN(string_list[num_of_strings - 1]);
3667
3668                 space_in_buffer = max_string_len - len_of_string_formed ;
3669
3670                 if (space_in_buffer >= (num_len+1+1))                   /*  1 for comma and 1 for ending NULL */ {
3671                         SNPRINTF(string_list[num_of_strings - 1] + len_of_string_formed, max_string_len, "%d,", numbers[j]);
3672                 }
3673                 else {  /*  Removing comma at end of string  */
3674                         string_list[num_of_strings - 1][len_of_string_formed-1] = '\0';
3675                         char **temp = NULL;
3676                         temp = (char **)realloc(string_list, sizeof(char *) * (num_of_strings + 1));    /*  Allocate new buffer to store a pointer to a new string */
3677
3678                         if (NULL == temp) {
3679                                 EM_DEBUG_EXCEPTION("realloc failed");
3680                                 goto FINISH_OFF;
3681                         }
3682
3683                         memset(temp + num_of_strings, 0X00, sizeof(char *));
3684
3685                         string_list = temp;
3686                         temp = NULL;
3687                         string_list[num_of_strings] = em_malloc(max_string_len);/*  Allocate new buffer to store the string */
3688
3689                         if (NULL == string_list[num_of_strings]) {
3690                                 EM_DEBUG_EXCEPTION(" em_malloc failed ");
3691                                 goto FINISH_OFF;
3692                         }
3693                         ++num_of_strings;
3694                         SNPRINTF(string_list[num_of_strings - 1] , max_string_len, "%d,", numbers[j]);/*  Start making new string */
3695                 }
3696         }
3697
3698         /*  Removing comma at end of string  */
3699         len_of_string_formed = EM_SAFE_STRLEN(string_list[num_of_strings - 1]);
3700         string_list[num_of_strings - 1][len_of_string_formed-1] = '\0';
3701         ret = true;
3702
3703 FINISH_OFF:
3704
3705         if (false == ret)
3706                 emcore_free_comma_separated_strings(&string_list, &num_of_strings);
3707
3708         if (true == ret) {
3709                 for (i = 0; i < num_of_strings;++i)
3710                         EM_DEBUG_LOG("%s", string_list[i]);
3711                 *strings = string_list;
3712                 *string_count = num_of_strings;
3713         }
3714
3715
3716         if (NULL != err_code)
3717                 *err_code = error;
3718
3719         EM_DEBUG_FUNC_END("ret [%d]", ret);
3720         return ret;
3721 }
3722 /**
3723  * @fn emcore_free_comma_separated_strings(char *** string_list, int *string_count)
3724  * Frees the double dimensional array of strings.
3725  *
3726  * @author                                      h.gahlaut@samsung.com
3727  * @param[in] uid_range_head    Address of base address of double dimensional array of strings.
3728  * @param[in] string_count              Address of variable holding the count of strings.
3729  * @remarks
3730  * @return This function does not return anything.
3731  */
3732 INTERNAL_FUNC void emcore_free_comma_separated_strings(char *** string_list, int *string_count)
3733 {
3734         EM_DEBUG_FUNC_BEGIN();
3735         int i = 0;
3736         char **str_list = NULL;
3737         int count = 0;
3738
3739         if (NULL != string_list) {
3740                 str_list = *string_list;
3741
3742                 if (0 != *string_count) {
3743                         count = *string_count;
3744                         for (i = 0; i < count; ++i)
3745                                 EM_SAFE_FREE(str_list[i]);
3746                 }
3747
3748                 EM_SAFE_FREE(str_list);
3749                 *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 */
3750                 *string_count = 0;
3751         }
3752         EM_DEBUG_FUNC_END();
3753 }
3754
3755
3756 #endif
3757
3758
3759
3760
3761 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)
3762 {
3763         EM_DEBUG_FUNC_BEGIN_SEC("source_file_name[%s], sub_type[%s], result_file_name_buffer_length[%d] ", source_file_name, sub_type, result_file_name_buffer_length);
3764         int ret = false, err = EMAIL_ERROR_NONE;
3765         char *extcheck = NULL;
3766         char attachment_file_name[MAX_PATH + 1] = { 0, };
3767
3768         if (!source_file_name || !result_file_name) {
3769                 EM_DEBUG_EXCEPTION("Invalid Parameter");
3770                 err  = EMAIL_ERROR_INVALID_PARAM;
3771                 goto FINISH_OFF;
3772         }
3773
3774         strncpy(attachment_file_name, source_file_name, MAX_PATH);
3775         extcheck = strchr(attachment_file_name, '.');
3776
3777         if (extcheck)
3778                 EM_DEBUG_LOG("Extension Exist in the Attachment [%s] ", extcheck);
3779         else  { /* No extension attached, So add the Extension based on the subtype */
3780                 if (sub_type) {
3781             if ((MAX_PATH + 1) < (strlen(sub_type) + 1)) {
3782                 EM_DEBUG_EXCEPTION("Buffer overflow");
3783                 err = EMAIL_ERROR_OUT_OF_MEMORY;
3784                 goto FINISH_OFF;
3785             }
3786
3787                         strcat(attachment_file_name, ".");
3788                         strcat(attachment_file_name, sub_type);
3789                         EM_DEBUG_LOG_SEC("attachment_file_name with extension[%s] ", attachment_file_name);
3790                 }
3791                 else
3792                         EM_DEBUG_LOG("UnKnown Extesnsion");
3793
3794         }
3795         memset(result_file_name, 0 , result_file_name_buffer_length);
3796         EM_SAFE_STRNCPY(result_file_name, attachment_file_name, result_file_name_buffer_length - 1);
3797         EM_DEBUG_LOG_SEC("*result_file_name[%s]", result_file_name);
3798         ret = true;
3799
3800 FINISH_OFF:
3801         if (err_code)
3802                 *err_code = err;
3803         EM_DEBUG_FUNC_END();
3804         return ret;
3805 }
3806
3807 #ifdef __FEATURE_LOCAL_ACTIVITY__
3808 INTERNAL_FUNC int emcore_add_activity(emstorage_activity_tbl_t *new_activity, int *err_code)
3809 {
3810         EM_DEBUG_FUNC_BEGIN();
3811
3812         EM_DEBUG_LOG("\t new_activity[%p], err_code[%p]", new_activity, err_code);
3813
3814         /*  default variable */
3815         int ret = false;
3816         int err = EMAIL_ERROR_NONE;
3817
3818         if (!new_activity) {
3819                 EM_DEBUG_LOG("\t new_activity[%p]\n", new_activity);
3820                 err = EMAIL_ERROR_INVALID_PARAM;
3821                 goto FINISH_OFF;
3822         }
3823         if (!emstorage_add_activity(new_activity, false, &err)) {
3824                 EM_DEBUG_LOG("\t emstorage_add_activity falied - %d\n", err);
3825
3826                 goto FINISH_OFF;
3827         }
3828         ret = true;
3829
3830 FINISH_OFF:
3831         if (err_code)
3832                 *err_code = err;
3833
3834         return ret;
3835 }
3836
3837 INTERNAL_FUNC int emcore_delete_activity(emstorage_activity_tbl_t *activity, int *err_code)
3838 {
3839         EM_DEBUG_FUNC_BEGIN();
3840
3841         EM_DEBUG_LOG("\t new_activity[%p], err_code[%p]", activity, err_code);
3842
3843         /*  default variable */
3844         int ret = false;
3845         int err = EMAIL_ERROR_NONE;
3846         if (!activity) {
3847                 EM_DEBUG_LOG("\t new_activity[%p]\n", activity);
3848
3849                 err = EMAIL_ERROR_INVALID_PARAM;
3850                 goto FINISH_OFF;
3851         }
3852         if (!emstorage_delete_local_activity(activity, true, &err)) {
3853                 EM_DEBUG_LOG("\t emstorage_delete_local_activity falied - %d\n", err);
3854
3855                 goto FINISH_OFF;
3856         }
3857         ret = true;
3858
3859 FINISH_OFF:
3860         if (err_code)
3861                 *err_code = err;
3862
3863         return ret;
3864 }
3865
3866 INTERNAL_FUNC int emcore_get_next_activity_id(int *activity_id, int *err_code)
3867 {
3868         EM_DEBUG_FUNC_BEGIN();
3869
3870         int ret = false;
3871         int err = EMAIL_ERROR_NONE;
3872
3873         if (NULL == activity_id)
3874         {
3875                 EM_DEBUG_EXCEPTION("\t activity_id[%p]", activity_id);
3876
3877                 err = EMAIL_ERROR_INVALID_PARAM;
3878                 goto FINISH_OFF;
3879         }
3880
3881         if (false == emstorage_get_next_activity_id(activity_id, &err)) {
3882                 EM_DEBUG_LOG("\t emstorage_get_next_activity_id failed - %d\n", err);
3883                 goto FINISH_OFF;
3884         }
3885
3886         ret = true;
3887
3888         FINISH_OFF:
3889         if (NULL != err_code) {
3890                 *err_code = err;
3891         }
3892
3893         return ret;
3894
3895 }
3896
3897 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
3898
3899
3900 INTERNAL_FUNC void emcore_free_rule(email_rule_t* rule)
3901 {
3902         EM_DEBUG_FUNC_BEGIN();
3903
3904         if (!rule)
3905                 return;
3906
3907         EM_SAFE_FREE(rule->filter_name);
3908         EM_SAFE_FREE(rule->value);
3909         EM_SAFE_FREE(rule->value2);
3910
3911         EM_DEBUG_FUNC_END();
3912 }
3913
3914 INTERNAL_FUNC void emcore_free_body_sparep(void **p)
3915 {
3916         EM_DEBUG_FUNC_BEGIN();
3917         EM_SAFE_FREE(*p);
3918         EM_DEBUG_FUNC_END();
3919 }
3920
3921 INTERNAL_FUNC int emcore_search_string_from_file(char *file_path, char *search_string, char *new_string, int *result)
3922 {
3923         EM_DEBUG_FUNC_BEGIN_SEC("file_path : [%s], search_string : [%s]", file_path, search_string);
3924         int error = EMAIL_ERROR_NONE;
3925         int file_size = 0;
3926         int p_result = false;
3927         FILE *fp = NULL;
3928         char *buf = NULL;
3929         char *stripped = NULL;
3930         char *cid_string = NULL;
3931         char *modified_string = NULL;
3932
3933         if (!search_string || !file_path || !result) {
3934                 EM_DEBUG_EXCEPTION("Invalid parameter");
3935                 error = EMAIL_ERROR_INVALID_PARAM;
3936                 return error;
3937         }
3938
3939         error = em_fopen(file_path, "r", &fp);
3940         if (error != EMAIL_ERROR_NONE || fp == NULL) {
3941                 EM_DEBUG_EXCEPTION("em_fopen error [%d] [%s]", error, file_path);
3942                 goto FINISH_OFF;
3943         }
3944
3945         if (!emcore_get_file_size(file_path, &file_size, &error)) {
3946                 EM_DEBUG_EXCEPTION("emcore_get_file_size error [%s]", file_path);
3947                 goto FINISH_OFF;
3948         }
3949
3950         buf = em_malloc(file_size + 1);
3951         if (buf == NULL) {
3952                 EM_DEBUG_EXCEPTION("em_malloc failed");
3953                 error = EMAIL_ERROR_OUT_OF_MEMORY;
3954                 goto FINISH_OFF;
3955         }
3956
3957         if (fread(buf, sizeof(char), file_size, fp) != file_size) {
3958                 EM_DEBUG_EXCEPTION("fread error [%d]", errno);
3959                 error = EMAIL_ERROR_SYSTEM_FAILURE;
3960                 goto FINISH_OFF;
3961         }
3962
3963         /*prevent 35586*/
3964         stripped = em_replace_all_string(buf, CRLF_STRING, "");
3965         if (stripped) {
3966                 if (new_string) {
3967                         cid_string = em_malloc(strlen(search_string) + strlen("cid:") + 1);
3968                         if (cid_string == NULL) {
3969                                 EM_DEBUG_EXCEPTION("em_malloc failed");
3970                                 error = EMAIL_ERROR_OUT_OF_MEMORY;
3971                                 goto FINISH_OFF;
3972                         }
3973
3974                         sprintf(cid_string, "cid:%s", search_string);
3975
3976                         modified_string = em_replace_string(stripped, cid_string, new_string);
3977                         if (modified_string) {
3978                                 if (fp)
3979                                         fclose(fp);
3980
3981                                 error = em_fopen(file_path, "w", &fp);
3982                                 if (error != EMAIL_ERROR_NONE || fp == NULL) {
3983                                         EM_DEBUG_EXCEPTION("em_fopen failed");
3984                                         goto FINISH_OFF;
3985                                 }
3986
3987                                 fprintf(fp, "%s", modified_string);
3988
3989                                 p_result = true;
3990                         }
3991
3992                 } else {
3993                         if (strstr(stripped, search_string))
3994                                 p_result = true;
3995                 }
3996         }
3997
3998 FINISH_OFF:
3999         if(!p_result)
4000                 EM_DEBUG_LOG("Search string[%s] not found",search_string);
4001
4002         *result = p_result;
4003
4004         if (fp)
4005                 fclose(fp);
4006
4007         EM_SAFE_FREE(buf);
4008         EM_SAFE_FREE(stripped);  /*prevent 35586*/
4009         EM_SAFE_FREE(cid_string);
4010         EM_SAFE_FREE(modified_string);
4011
4012         EM_DEBUG_FUNC_END("error:[%d]", error);
4013         return error;
4014 }
4015
4016 INTERNAL_FUNC int emcore_load_query_from_file(char *file_path, char ***query_array, int *array_len)
4017 {
4018         EM_DEBUG_FUNC_BEGIN_SEC("file_path : [%s], query_array : [%p]", file_path, *query_array);
4019         int error = EMAIL_ERROR_NONE;
4020         int file_size = 0;
4021         int vector_len = 0;
4022         int i = 0;
4023         FILE *fp = NULL;
4024         char *buf = NULL;
4025         char **result_vector = NULL;
4026
4027         if (!file_path) {
4028                 EM_DEBUG_EXCEPTION("Invalid parameter");
4029                 error = EMAIL_ERROR_INVALID_PARAM;
4030                 return error;
4031         }
4032
4033         error = em_fopen(file_path, "r", &fp);
4034         if (error != EMAIL_ERROR_NONE) {
4035                 EM_DEBUG_EXCEPTION("em_fopen failed");
4036                 goto FINISH_OFF;
4037         }
4038
4039         if (!emcore_get_file_size(file_path, &file_size, &error)) {
4040                 EM_DEBUG_EXCEPTION("emcore_get_file_size failed");
4041                 goto FINISH_OFF;
4042         }
4043
4044         buf = em_malloc(file_size + 1);
4045         if (buf == NULL) {
4046                 EM_DEBUG_EXCEPTION("em_malloc failed");
4047                 error = EMAIL_ERROR_OUT_OF_MEMORY;
4048                 goto FINISH_OFF;
4049         }
4050
4051         if (fread(buf, sizeof(char), file_size, fp) != file_size) {
4052                 EM_DEBUG_EXCEPTION("Get the data from file : failed");
4053                 error = EMAIL_ERROR_SYSTEM_FAILURE;
4054                 goto FINISH_OFF;
4055         }
4056
4057         result_vector = g_strsplit(buf, ";", -1);
4058         if (!result_vector || g_strv_length(result_vector) <= 0) {
4059                 EM_DEBUG_EXCEPTION("Parsing sql file failed");
4060                 error = EMAIL_ERROR_SYSTEM_FAILURE;
4061                 goto FINISH_OFF;
4062         }
4063
4064         vector_len = g_strv_length(result_vector);
4065         if (vector_len <= 0) {
4066                 EM_DEBUG_EXCEPTION("vector length : [%d]", vector_len);
4067                 error = EMAIL_ERROR_SYSTEM_FAILURE;
4068                 goto FINISH_OFF;
4069         }
4070
4071         *query_array = (char **)calloc(vector_len, sizeof(char *));
4072
4073         for (i = 0; i < vector_len; i++) {
4074                 if (result_vector[i]) {
4075                         char *str = g_strconcat(result_vector[i], ";", NULL);
4076                         if (str) (*query_array)[i] = str;
4077                 }
4078         }
4079
4080         if (array_len) {
4081                 *array_len = vector_len;
4082                 EM_DEBUG_LOG("SQL string array length : [%d]", vector_len);
4083         }
4084
4085 FINISH_OFF:
4086
4087         if (fp)
4088                 fclose(fp);
4089
4090         EM_SAFE_FREE(buf);
4091
4092         if (result_vector) {
4093                 g_strfreev(result_vector);
4094         }
4095
4096         EM_DEBUG_FUNC_END("error:[%d]", error);
4097         return error;
4098 }
4099
4100 /* peak schedule */
4101 static int emcore_get_next_peak_start_time(emstorage_account_tbl_t *input_account_ref, time_t input_current_time, time_t *output_time)
4102 {
4103         EM_DEBUG_FUNC_BEGIN("input_account_ref [%p] input_time[%d] output_time[%p]", input_account_ref, input_current_time, output_time);
4104         int err = EMAIL_ERROR_NONE;
4105         int wday = 1;
4106         int day_count = 0;
4107         int start_hour = 0;
4108         int start_min  = 0;
4109         struct tm *time_info;
4110
4111         if(output_time == NULL) {
4112                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4113                 err = EMAIL_ERROR_INVALID_PARAM;
4114                 goto FINISH_OFF;
4115         }
4116
4117         time_info = localtime (&input_current_time);
4118         if (time_info == NULL) {
4119                 EM_DEBUG_EXCEPTION("localtime failed");
4120                 err = EMAIL_ERROR_SYSTEM_FAILURE;
4121                 goto FINISH_OFF;                
4122         }
4123
4124         EM_DEBUG_LOG("input time and date: %s", asctime(time_info));
4125
4126         wday = wday << time_info->tm_wday;
4127
4128         EM_DEBUG_LOG("wday[%d] peak_days[%d]", wday, input_account_ref->peak_days);
4129
4130         /* find next peak day */
4131
4132         if (wday & input_account_ref->peak_days) {
4133                 start_hour = input_account_ref->peak_start_time / 100;
4134                 start_min  = input_account_ref->peak_start_time % 100;
4135                 if (start_hour < time_info->tm_hour || (start_hour == time_info->tm_hour && start_min < time_info->tm_min)) {
4136                         if(wday == EMAIL_PEAK_DAYS_SATDAY)
4137                                 wday = EMAIL_PEAK_DAYS_SUNDAY;
4138                         else
4139                                 wday = wday << 1;
4140                         day_count++;
4141                 }
4142
4143         }
4144
4145         while (!(wday & input_account_ref->peak_days) && day_count < 7){
4146                 EM_DEBUG_LOG("wday[%d] day_count[%d]", wday, day_count);
4147                 if(wday == EMAIL_PEAK_DAYS_SATDAY)
4148                         wday = EMAIL_PEAK_DAYS_SUNDAY;
4149                 else
4150                         wday = wday << 1;
4151                 day_count++;
4152         }
4153
4154         EM_DEBUG_LOG("day_count[%d]", day_count);
4155
4156         if (day_count < 7) {
4157                 time_info->tm_mday += day_count; /* The other members of time_info will be interpreted or set by mktime */
4158
4159                 time_info->tm_hour = input_account_ref->peak_start_time / 100;
4160                 time_info->tm_min  = input_account_ref->peak_start_time % 100;
4161                 time_info->tm_min  = 0;
4162
4163                 *output_time = mktime(time_info);
4164                 EM_DEBUG_LOG("result_time: %s", asctime(time_info));
4165         }
4166
4167 FINISH_OFF:
4168
4169         EM_DEBUG_FUNC_END("err[%d]", err);
4170         return err;
4171 }
4172
4173 static int emcore_check_time_in_peak_schedule(emstorage_account_tbl_t *input_account_ref, time_t input_time, int *output_result)
4174 {
4175         EM_DEBUG_FUNC_BEGIN("input_account_ref [%p] input_time[%d] output_result[%p]", input_account_ref, input_time, output_result);
4176         int err = EMAIL_ERROR_NONE;
4177         struct tm *time_info;
4178         int wday = 1;
4179         int result = 0;
4180         int start_hour = 0;
4181         int start_min  = 0;
4182         int end_hour = 0;
4183         int end_min  = 0;
4184
4185         if(input_account_ref == NULL || output_result == NULL) {
4186                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4187                 err = EMAIL_ERROR_INVALID_PARAM;
4188                 goto FINISH_OFF;
4189         }
4190
4191         time_info = localtime(&input_time);
4192         if (time_info == NULL) {
4193                 EM_DEBUG_EXCEPTION("localtime failed");
4194                 err = EMAIL_ERROR_SYSTEM_FAILURE;
4195                 goto FINISH_OFF;                
4196         }
4197
4198         EM_DEBUG_LOG("input time and date: %s", asctime(time_info));
4199
4200         wday = wday << time_info->tm_wday;
4201
4202         EM_DEBUG_LOG("wday [%d]", wday);
4203
4204         if(wday & input_account_ref->peak_days) {
4205                 start_hour = input_account_ref->peak_start_time / 100;
4206                 start_min  = input_account_ref->peak_start_time % 100;
4207
4208                 end_hour   = input_account_ref->peak_end_time / 100;
4209                 end_min    = input_account_ref->peak_end_time % 100;
4210
4211                 EM_DEBUG_LOG("start_hour[%d] start_min[%d] end_hour[%d] end_min[%d]", start_hour, start_min, end_hour, end_min);
4212
4213                 if(start_hour <= time_info->tm_hour && time_info->tm_hour <= end_hour) {
4214                         if(time_info->tm_hour == end_hour) {
4215                                 if(time_info->tm_min < end_min)
4216                                         result = 1;
4217                         }
4218                         else
4219                                 result = 1;
4220                 }
4221         }
4222
4223         EM_DEBUG_LOG("result[%d]", result);
4224         *output_result = result;
4225
4226 FINISH_OFF:
4227
4228         EM_DEBUG_FUNC_END("err[%d]", err);
4229         return err;
4230 }
4231
4232 INTERNAL_FUNC int emcore_calc_next_time_to_sync(char *multi_user_name, int input_account_id, time_t input_current_time, time_t *output_time)
4233 {
4234         EM_DEBUG_FUNC_BEGIN("input_account_id[%d] input_current_time[%d] output_time[%p]", input_account_id, input_current_time, output_time);
4235         int err = EMAIL_ERROR_NONE;
4236         emstorage_account_tbl_t *account = NULL;
4237         time_t next_time = 0;
4238         time_t next_peak_start_time = 0;
4239         time_t result_time = 0;
4240         int    is_time_in_peak_schedule = 0;
4241
4242         emstorage_get_account_by_id(multi_user_name, input_account_id, EMAIL_ACC_GET_OPT_DEFAULT, &account, true, &err);
4243         if (account == NULL) {
4244                 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed [%d]",err);
4245                 goto FINISH_OFF;
4246         }
4247
4248         if (account->peak_days < 0) {
4249                 /* peak schedule disabled */
4250                 if (account->check_interval > 0)
4251                         result_time = input_current_time + (account->check_interval * 60);
4252         }
4253         else if(account->peak_days > 0) {
4254                 /* peak schedule enabled */
4255
4256                 /* if current time is in peak schedule */
4257                 emcore_check_time_in_peak_schedule(account, input_current_time, &is_time_in_peak_schedule);
4258                 EM_DEBUG_LOG("is_time_in_peak_schedule [%d]", is_time_in_peak_schedule);
4259                 if(is_time_in_peak_schedule) {
4260                         /* if next time to sync is in peak schedule? */
4261                         next_time = input_current_time + (account->peak_interval * 60);
4262
4263                         emcore_check_time_in_peak_schedule(account, next_time, &is_time_in_peak_schedule);
4264                         if(is_time_in_peak_schedule) {
4265                                 /* create an alarm to sync in peak schedule*/
4266                                 result_time = next_time;
4267                         }
4268                 }
4269
4270                 if(result_time == 0) {
4271                         /* if current time is not in peak schedule */
4272                         if(next_time == 0)  {
4273                                 /* if normal sync schedule is set */
4274                                 if(account->check_interval) {
4275                                         next_time = input_current_time + (account->check_interval * 60);
4276                                 }
4277                         }
4278                         emcore_get_next_peak_start_time(account, input_current_time, &next_peak_start_time);
4279
4280                         EM_DEBUG_LOG("next_time            : %s", ctime(&next_time));
4281                         EM_DEBUG_LOG("next_peak_start_time : %s", ctime(&next_peak_start_time));
4282
4283                         /* if next time to sync is closer than next peak schedule start? */
4284                         if(next_time && (next_time < next_peak_start_time)) {
4285                                 /* create an alarm to sync in check_interval */
4286                                 result_time = next_time;
4287                         }
4288                         /* else check_interval is zero or next peak schedule start is close than next check_interval */
4289                         else {
4290                                 result_time = next_peak_start_time;
4291                         }
4292                 }
4293         }
4294
4295         *output_time = result_time;
4296         EM_DEBUG_LOG("result_time : %s", ctime(&result_time));
4297
4298
4299 FINISH_OFF:
4300         if(account) {
4301                 emstorage_free_account(&account, 1, NULL);
4302         }
4303
4304         EM_DEBUG_FUNC_END("err[%d]", err);
4305         return err;
4306 }
4307
4308 static int convert_contact_err_to_email_err(int contact_err)
4309 {
4310         int err = EMAIL_ERROR_NONE;
4311
4312         switch (contact_err) {
4313         case CONTACTS_ERROR_NONE :
4314                 err = EMAIL_ERROR_NONE;
4315                 break;
4316         case CONTACTS_ERROR_OUT_OF_MEMORY :
4317                 err = EMAIL_ERROR_OUT_OF_MEMORY;
4318                 break;
4319         case CONTACTS_ERROR_INVALID_PARAMETER :
4320                 err = EMAIL_ERROR_INVALID_PARAM;
4321                 break;
4322         case CONTACTS_ERROR_NO_DATA :
4323                 err = EMAIL_ERROR_DATA_NOT_FOUND;
4324                 break;
4325         case CONTACTS_ERROR_DB :
4326                 err = EMAIL_ERROR_DB_FAILURE;
4327                 break;
4328         case CONTACTS_ERROR_IPC :
4329                 err = EMAIL_ERROR_IPC_CONNECTION_FAILURE;
4330                 break;
4331         case CONTACTS_ERROR_SYSTEM:
4332                 err = EMAIL_ERROR_SYSTEM_FAILURE;
4333                 break;
4334         case CONTACTS_ERROR_INTERNAL:
4335         default:
4336                 err = EMAIL_ERROR_UNKNOWN;
4337                 break;
4338         }
4339         return err;
4340 }
4341
4342 int emcore_get_mail_contact_info(char *multi_user_name, email_mail_contact_info_t *contact_info, char *full_address, int *err_code)
4343 {
4344         EM_DEBUG_FUNC_BEGIN_SEC("contact_info[%p], full_address[%s], err_code[%p]", contact_info, full_address, err_code);
4345
4346         int ret = false;
4347         int err = EMAIL_ERROR_NONE;
4348         
4349         if (!emcore_get_mail_contact_info_with_update(multi_user_name, contact_info, full_address, 0, &err))
4350                 EM_DEBUG_EXCEPTION("emcore_get_mail_contact_info_with_update failed [%d]", err);
4351         else
4352                 ret = true;
4353
4354         if (err_code != NULL)
4355                 *err_code = err;
4356
4357         return ret;
4358 }
4359
4360 int emcore_search_contact_info(char *multi_user_name, const char *contact_uri, int address_property_id, char *address, int favorite_property_id, bool is_favorite, int limit, contacts_record_h *contacts_record)
4361 {
4362         EM_DEBUG_FUNC_BEGIN();
4363         int contact_err = CONTACTS_ERROR_NONE;
4364
4365         contacts_query_h query = NULL;
4366         contacts_filter_h filter = NULL;
4367         contacts_list_h list = NULL;
4368
4369         void *join_zone = NULL;
4370
4371         if (EM_SAFE_STRLEN(multi_user_name) > 0) {
4372                 if (emcore_set_join_zone(multi_user_name, &join_zone) != EMAIL_ERROR_NONE) {
4373                         EM_DEBUG_EXCEPTION("emcore_set_join_zone failed");
4374                         goto FINISH_OFF;
4375                 }
4376         }
4377
4378         if ((contact_err = contacts_query_create(contact_uri, &query)) != CONTACTS_ERROR_NONE) {
4379                 EM_DEBUG_EXCEPTION("contacts_query_create failed");
4380                 goto FINISH_OFF;
4381         }
4382
4383         if ((contact_err = contacts_filter_create(contact_uri, &filter)) != CONTACTS_ERROR_NONE) {
4384                 EM_DEBUG_EXCEPTION("contacts_filter_create failed");
4385                 goto FINISH_OFF;
4386         }
4387
4388         if ((contact_err = contacts_filter_add_str(filter, address_property_id, CONTACTS_MATCH_FULLSTRING, address)) != CONTACTS_ERROR_NONE) {
4389                 EM_DEBUG_EXCEPTION("contacts_filter_add_str failed");
4390                 goto FINISH_OFF;
4391         }
4392
4393         if ((contact_err = contacts_filter_add_operator(filter, CONTACTS_FILTER_OPERATOR_AND)) != CONTACTS_ERROR_NONE) {
4394                 EM_DEBUG_EXCEPTION("contacts_filter_add_operator failed");
4395                 goto FINISH_OFF;
4396         }
4397
4398         if ((contact_err = contacts_filter_add_bool(filter, favorite_property_id, is_favorite)) != CONTACTS_ERROR_NONE) {
4399                 EM_DEBUG_EXCEPTION("contacts_filter_add_bool failed");
4400                 goto FINISH_OFF;
4401         }
4402
4403         if ((contact_err = contacts_query_set_filter(query, filter)) != CONTACTS_ERROR_NONE) {
4404                 EM_DEBUG_EXCEPTION("contacts_query_set_filter failed");
4405                 goto FINISH_OFF;
4406         }
4407
4408         if ((contact_err = contacts_db_get_records_with_query(query, 0, limit, &list)) != CONTACTS_ERROR_NONE) {
4409                 EM_DEBUG_EXCEPTION("contacts_db_get_record_with_query failed");
4410                 goto FINISH_OFF;
4411         }
4412
4413         if ((contact_err = contacts_list_get_current_record_p(list, contacts_record)) != CONTACTS_ERROR_NONE) {
4414                 EM_DEBUG_EXCEPTION("contacts_list_get_current_record_p failed");
4415                 goto FINISH_OFF;
4416         }
4417
4418 FINISH_OFF:
4419
4420         if (query != NULL)
4421                 contacts_query_destroy(query);
4422
4423         if (filter != NULL)
4424                 contacts_filter_destroy(filter);
4425
4426         if (list != NULL)
4427                 contacts_list_destroy(list, false);
4428
4429         if (join_zone)
4430                 emcore_unset_join_zone(join_zone);
4431
4432         return contact_err;
4433 }
4434
4435 int emcore_search_contact_info_by_address(char *multi_user_name, const char *contact_uri, int property_id, char *address, int limit, contacts_record_h *contacts_record)
4436 {
4437         EM_DEBUG_FUNC_BEGIN();
4438         int contact_err = CONTACTS_ERROR_NONE;
4439
4440         contacts_query_h query = NULL;
4441         contacts_filter_h filter = NULL;
4442         contacts_list_h list = NULL;
4443
4444         void *join_zone = NULL;
4445
4446         if (EM_SAFE_STRLEN(multi_user_name) > 0) {
4447                 if (emcore_set_join_zone(multi_user_name, &join_zone) != EMAIL_ERROR_NONE) {
4448                         EM_DEBUG_EXCEPTION("emcore_set_join_zone failed");
4449                         goto FINISH_OFF;
4450                 }
4451         }
4452
4453         if ((contact_err = contacts_query_create(contact_uri, &query)) != CONTACTS_ERROR_NONE) {
4454                 EM_DEBUG_EXCEPTION("contacts_query_create failed");
4455                 goto FINISH_OFF;
4456         }
4457
4458         if ((contact_err = contacts_filter_create(contact_uri, &filter)) != CONTACTS_ERROR_NONE) {
4459                 EM_DEBUG_EXCEPTION("contacts_filter_create failed");
4460                 goto FINISH_OFF;
4461         }
4462
4463         if ((contact_err = contacts_filter_add_str(filter, property_id, CONTACTS_MATCH_FULLSTRING, address)) != CONTACTS_ERROR_NONE) {
4464                 EM_DEBUG_EXCEPTION("contacts_filter_add_str failed");
4465                 goto FINISH_OFF;
4466         }
4467
4468         if ((contact_err = contacts_query_set_filter(query, filter)) != CONTACTS_ERROR_NONE) {
4469                 EM_DEBUG_EXCEPTION("contacts_query_set_filter failed");
4470                 goto FINISH_OFF;
4471         }
4472
4473         if ((contact_err = contacts_db_get_records_with_query(query, 0, limit, &list)) != CONTACTS_ERROR_NONE) {
4474                 EM_DEBUG_EXCEPTION("contacts_db_get_record_with_query failed");
4475                 goto FINISH_OFF;
4476         }
4477
4478         if ((contact_err = contacts_list_get_current_record_p(list, contacts_record)) != CONTACTS_ERROR_NONE) {
4479                 if (contact_err != CONTACTS_ERROR_NO_DATA) /* no matching record found */
4480                         EM_DEBUG_EXCEPTION ("contacts_list_get_current_record_p failed [%d]", contact_err);
4481                 goto FINISH_OFF;
4482         }
4483
4484 FINISH_OFF:
4485
4486         if (query != NULL)
4487                 contacts_query_destroy(query);
4488
4489         if (filter != NULL)
4490                 contacts_filter_destroy(filter);
4491
4492         if (list != NULL)
4493                 contacts_list_destroy(list, false);
4494
4495         if (join_zone)
4496                 emcore_unset_join_zone(join_zone);
4497
4498         return contact_err;
4499 }
4500
4501 typedef struct {
4502         int   account_id;
4503         char *email_address;
4504         char *subject;
4505     char *multi_user_name;
4506         time_t date_time;
4507         email_action_t action;
4508 } set_contacts_log_internal_t;
4509
4510 gboolean emcore_set_contacts_log_internal (void* arg)
4511 {
4512         if (!arg) {
4513                 EM_DEBUG_EXCEPTION ("no contact data to add");
4514                 return FALSE;
4515         }
4516         set_contacts_log_internal_t* tmp = (set_contacts_log_internal_t*) arg;
4517         int   account_id    = tmp->account_id;
4518         char *email_address = tmp->email_address;
4519         char *subject       = tmp->subject;
4520         time_t date_time    = tmp->date_time;
4521         email_action_t action = tmp->action;
4522         char *multi_user_name = tmp->multi_user_name;
4523
4524         int contacts_error = CONTACTS_ERROR_NONE;
4525         int person_id = 0;
4526         int action_type = 0;
4527
4528         contacts_record_h phone_record = NULL;
4529         contacts_record_h person_record = NULL;
4530
4531         void *join_zone = NULL;
4532
4533         if (EM_SAFE_STRLEN(multi_user_name) > 0) {
4534                 if (emcore_set_join_zone(multi_user_name, &join_zone) != EMAIL_ERROR_NONE) {
4535                         EM_DEBUG_EXCEPTION("emcore_set_join_zone failed");
4536                         goto FINISH_OFF;
4537                 }
4538         }
4539
4540         if ((contacts_error = contacts_record_create(_contacts_phone_log._uri, &phone_record)) != CONTACTS_ERROR_NONE) {
4541                 EM_DEBUG_EXCEPTION("contacts_query_create failed [%d]", contacts_error);
4542                 goto FINISH_OFF;
4543         }
4544
4545         /* Set email address */
4546         if ((contacts_error = contacts_record_set_str(phone_record, _contacts_phone_log.address, email_address)) != CONTACTS_ERROR_NONE) {
4547                 EM_DEBUG_EXCEPTION("contacts_record_set_str [address] failed [%d]", contacts_error);
4548                 goto FINISH_OFF;
4549         }
4550
4551         /* Search contact person info */
4552         if ((contacts_error = emcore_search_contact_info_by_address(multi_user_name, _contacts_person_email._uri, _contacts_person_email.email, email_address, 1, &person_record)) != CONTACTS_ERROR_NONE) {
4553                 if (contacts_error != CONTACTS_ERROR_NO_DATA) /* no matching record found */
4554                         EM_DEBUG_EXCEPTION("emcore_search_contact_info_by_address failed [%d]", contacts_error);
4555         } else {
4556                 /* Get person_id in contacts_person_email record  */
4557                 if (person_record  && (contacts_error = contacts_record_get_int(person_record, _contacts_person_email.person_id, &person_id)) != CONTACTS_ERROR_NONE) {
4558                         EM_DEBUG_EXCEPTION("contacts_record_get_str failed [%d]", contacts_error);
4559                         goto FINISH_OFF;
4560                 }
4561
4562                 /* Set the person id */
4563                 if ((contacts_error = contacts_record_set_int(phone_record, _contacts_phone_log.person_id, person_id)) != CONTACTS_ERROR_NONE) {
4564                         EM_DEBUG_EXCEPTION("contacts_record_set_int [person id] failed [%d]", contacts_error);
4565                         goto FINISH_OFF;
4566                 }
4567         }
4568
4569         switch (action) {
4570         case EMAIL_ACTION_SEND_MAIL :
4571                 action_type = CONTACTS_PLOG_TYPE_EMAIL_SENT;
4572                 break;
4573         case EMAIL_ACTION_SYNC_HEADER :
4574                 action_type = CONTACTS_PLOG_TYPE_EMAIL_RECEIVED;
4575                 break;
4576         default :
4577                 EM_DEBUG_EXCEPTION("Unknown action type");
4578                 goto FINISH_OFF;
4579         }
4580
4581         /* Set log type */
4582         if ((contacts_error = contacts_record_set_int(phone_record, _contacts_phone_log.log_type, action_type)) != CONTACTS_ERROR_NONE) {
4583                 EM_DEBUG_EXCEPTION("contacts_record_set_int [log_type] failed [%d]", contacts_error);
4584                 goto FINISH_OFF;
4585         }
4586
4587         /* Set log time */
4588         if ((contacts_error = contacts_record_set_int(phone_record, _contacts_phone_log.log_time, (int)date_time)) != CONTACTS_ERROR_NONE) {
4589                 EM_DEBUG_EXCEPTION("contacts_record_set_str [address] failed [%d]", contacts_error);
4590                 goto FINISH_OFF;
4591         }
4592
4593         /* Set subject */
4594         if ((contacts_error = contacts_record_set_str(phone_record, _contacts_phone_log.extra_data2, subject)) != CONTACTS_ERROR_NONE) {
4595                 EM_DEBUG_EXCEPTION("contacts_record_set_str [subject] failed [%d]", contacts_error);
4596                 goto FINISH_OFF;
4597         }
4598
4599         /* Set Mail id */
4600         if ((contacts_error = contacts_record_set_int(phone_record, _contacts_phone_log.extra_data1, account_id)) != CONTACTS_ERROR_NONE) {
4601                 EM_DEBUG_EXCEPTION("contacts_record_set_int [mail id] failed [%d]", contacts_error);
4602                 goto FINISH_OFF;
4603         }
4604
4605         /* Insert the record in DB */
4606         if ((contacts_error = contacts_db_insert_record(phone_record, NULL)) != CONTACTS_ERROR_NONE) {
4607                 EM_DEBUG_EXCEPTION("contacts_db_insert_record failed [%d]",contacts_error );
4608                 goto FINISH_OFF;
4609         }
4610
4611 FINISH_OFF:
4612
4613         if (phone_record != NULL)
4614                 contacts_record_destroy(phone_record, false);
4615
4616         if (person_record != NULL)
4617                 contacts_record_destroy(person_record, false);
4618
4619         if (join_zone)
4620                 emcore_unset_join_zone(join_zone);
4621
4622         EM_SAFE_FREE (email_address);
4623         EM_SAFE_FREE (subject);
4624         EM_SAFE_FREE (tmp);
4625
4626         EM_DEBUG_FUNC_END("contact err [%d]", convert_contact_err_to_email_err(contacts_error));
4627         return FALSE;
4628 }
4629
4630 int emcore_set_contacts_log (char *multi_user_name,
4631                                 int   account_id, 
4632                                 char *email_address, 
4633                                 char *subject, 
4634                                 time_t date_time, 
4635                                 email_action_t action)
4636 {
4637         /* arg shall be destroyed in emcore_set_contacts_log_internal */
4638         set_contacts_log_internal_t *arg = em_malloc (sizeof(set_contacts_log_internal_t));
4639         if (!arg) {
4640                 EM_DEBUG_EXCEPTION ("em_malloc error");
4641                 return EMAIL_ERROR_OUT_OF_MEMORY;
4642         }
4643
4644         /* deep copy */
4645         arg->account_id      = account_id;
4646         arg->email_address   = EM_SAFE_STRDUP (email_address);
4647         arg->subject         = EM_SAFE_STRDUP (subject);
4648         arg->date_time       = date_time;
4649         arg->action          = action;
4650     arg->multi_user_name = EM_SAFE_STRDUP (multi_user_name);
4651
4652         g_main_context_invoke (NULL, emcore_set_contacts_log_internal, arg);
4653
4654         return EMAIL_ERROR_NONE;
4655 }
4656
4657 INTERNAL_FUNC int emcore_set_sent_contacts_log(char *multi_user_name, emstorage_mail_tbl_t *input_mail_data)
4658 {
4659         EM_DEBUG_FUNC_BEGIN("input_mail_data : [%p]", input_mail_data);
4660
4661         int i = 0;
4662         int err = EMAIL_ERROR_NONE;
4663         char email_address[MAX_EMAIL_ADDRESS_LENGTH];
4664         char *address_array[3] = {input_mail_data->full_address_to, input_mail_data->full_address_cc, input_mail_data->full_address_bcc};
4665         ADDRESS *addr = NULL;
4666         ADDRESS *p_addr = NULL;
4667
4668         for (i = 0; i < 3; i++) {
4669                 if (address_array[i] && address_array[i][0] != 0) {
4670                         rfc822_parse_adrlist(&addr, address_array[i], NULL);
4671                         for (p_addr = addr ; p_addr ;p_addr = p_addr->next) {
4672                                 SNPRINTF(email_address, MAX_EMAIL_ADDRESS_LENGTH, "%s@%s", addr->mailbox, addr->host);
4673                                 if ((err = emcore_set_contacts_log(multi_user_name, 
4674                                                     input_mail_data->account_id, 
4675                                                     email_address, 
4676                                                     input_mail_data->subject, 
4677                                                     input_mail_data->date_time, 
4678                                                     EMAIL_ACTION_SEND_MAIL)) != EMAIL_ERROR_NONE) {
4679                                         EM_DEBUG_EXCEPTION("emcore_set_contacts_log failed : [%d]", err);
4680                                         goto FINISH_OFF;
4681                                 }
4682                         }
4683
4684                         if (addr) {
4685                                 mail_free_address(&addr);
4686                                 addr = NULL;
4687                         }
4688                 }
4689         }
4690
4691 FINISH_OFF:
4692
4693         if (addr)
4694                 mail_free_address(&addr);
4695
4696         EM_DEBUG_FUNC_END("err [%d]", err);
4697         return err;
4698 }
4699
4700
4701 INTERNAL_FUNC int emcore_set_received_contacts_log(char *multi_user_name, emstorage_mail_tbl_t *input_mail_data)
4702 {
4703         EM_DEBUG_FUNC_BEGIN("input_mail_data : [%p]", input_mail_data);
4704         int err = EMAIL_ERROR_NONE;
4705
4706         if ((err = emcore_set_contacts_log(multi_user_name, 
4707                                         input_mail_data->account_id, 
4708                                         input_mail_data->email_address_sender, 
4709                                         input_mail_data->subject, 
4710                                         input_mail_data->date_time, 
4711                                         EMAIL_ACTION_SYNC_HEADER)) != EMAIL_ERROR_NONE) {
4712                 EM_DEBUG_EXCEPTION("emcore_set_contacts_log failed [%d]", err);
4713         }
4714
4715         EM_DEBUG_FUNC_END("err [%d]", err);
4716         return err;
4717 }
4718
4719 typedef struct _contacts_delete_data
4720 {
4721     int account_id;
4722     char *multi_user_name;
4723 } contacts_delete_data;
4724
4725 gboolean emcore_delete_contacts_log_internal(void* arg)
4726 {
4727         EM_DEBUG_FUNC_BEGIN();
4728     
4729     contacts_delete_data *data = (contacts_delete_data *)arg;
4730         int contacts_error = CONTACTS_ERROR_NONE;
4731
4732         void *join_zone = NULL;
4733
4734     if (EM_SAFE_STRLEN(data->multi_user_name) > 0) {
4735                 if (emcore_set_join_zone(data->multi_user_name, &join_zone) != EMAIL_ERROR_NONE) {
4736                         EM_DEBUG_EXCEPTION("emcore_set_join_zone failed");
4737                         return FALSE;
4738                 }
4739         }
4740
4741         /* Delete record of the account id */
4742         if ((contacts_error = contacts_phone_log_delete(CONTACTS_PHONE_LOG_DELETE_BY_EMAIL_EXTRA_DATA1, data->account_id)) != CONTACTS_ERROR_NONE) {
4743                 EM_DEBUG_EXCEPTION("contacts_phone_log_delete failed [%d]", contacts_error);
4744         }
4745 /*      err = convert_contact_err_to_email_err(contacts_error); */
4746
4747         if (join_zone)
4748                 emcore_unset_join_zone(join_zone);
4749
4750     EM_SAFE_FREE(data->multi_user_name);
4751     EM_SAFE_FREE(data);
4752
4753         EM_DEBUG_FUNC_END();
4754         return FALSE;
4755 }
4756
4757 INTERNAL_FUNC int emcore_delete_contacts_log(char *multi_user_name, int account_id)
4758 {
4759     contacts_delete_data *data = NULL;
4760
4761     data = (contacts_delete_data *)em_malloc(sizeof(contacts_delete_data));
4762     if (data == NULL) {
4763         EM_DEBUG_EXCEPTION("em_malloc failed");
4764         return EMAIL_ERROR_OUT_OF_MEMORY;
4765     }
4766
4767     data->account_id = account_id;
4768     data->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
4769
4770         g_main_context_invoke (NULL, emcore_delete_contacts_log_internal, (void*) data);
4771
4772         return EMAIL_ERROR_NONE;                
4773 }
4774
4775 INTERNAL_FUNC int emcore_get_mail_display_name (char *multi_user_name, char *email_address, char **contact_display_name)
4776 {
4777         if (!email_address || !contact_display_name) {
4778                 EM_DEBUG_EXCEPTION ("NULL_PARAM email_address[%p] contact_display_name[%p]",
4779                                                 email_address, contact_display_name);
4780                 return EMAIL_ERROR_INVALID_PARAM;
4781         }
4782
4783         GError   *gerror = NULL;
4784         GVariant *result = NULL;
4785         int ret = EMAIL_ERROR_NONE;
4786
4787 #if !GLIB_CHECK_VERSION(2, 36, 0) 
4788         g_type_init(); 
4789 #endif
4790
4791         GDBusProxy* bproxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
4792                                     G_DBUS_PROXY_FLAGS_NONE,
4793                                     NULL,
4794                                     EMAIL_SERVICE_NAME,
4795                                     EMAIL_SERVICE_PATH,
4796                                     EMAIL_SERVICE_NAME,
4797                                     NULL,
4798                                     &gerror);
4799         if (!bproxy) {
4800                 EM_DEBUG_EXCEPTION ("g_dbus_proxy_new_for_bus_sync error [%s]",
4801                                  gerror->message);
4802                 ret = EMAIL_ERROR_IPC_PROTOCOL_FAILURE;
4803                 goto FINISH_OFF;
4804         }
4805
4806         result = g_dbus_proxy_call_sync (bproxy, 
4807                                       "GetDisplayName", 
4808                                       g_variant_new ("(ss)", email_address, multi_user_name), 
4809                                       G_DBUS_CALL_FLAGS_NONE, 
4810                                       -1, 
4811                                       NULL, 
4812                                       &gerror);
4813
4814
4815         if (!result) {
4816                 EM_DEBUG_EXCEPTION ("g_dbus_proxy_call_sync 'GetDisplayName' error [%s]", 
4817                                  gerror->message);
4818                 ret = EMAIL_ERROR_IPC_PROTOCOL_FAILURE;
4819                 goto FINISH_OFF;
4820         }
4821
4822         g_variant_get (result, "(si)", contact_display_name, &ret);
4823
4824         /* replace "" to NULL */
4825         if (!g_strcmp0 (*contact_display_name, ""))
4826                 EM_SAFE_FREE (*contact_display_name);
4827
4828         g_variant_unref (result);
4829
4830 FINISH_OFF:
4831         EM_DEBUG_LOG ("ret [%d]\n", ret);
4832         if (bproxy)
4833                 g_object_unref (bproxy);
4834         if (gerror)
4835                 g_error_free (gerror);
4836
4837         return ret;
4838 }
4839
4840
4841 INTERNAL_FUNC int emcore_get_mail_display_name_internal (char *multi_user_name, 
4842                                                         char *email_address, 
4843                                                         char **contact_display_name)
4844 {
4845         EM_DEBUG_FUNC_BEGIN_SEC("contact_name_value[%s], contact_display_name[%p]", email_address, contact_display_name);
4846
4847         int contact_err = 0;
4848         int err = EMAIL_ERROR_NONE;
4849         int ret = false;
4850         char *display = NULL;
4851         /* Contact variable */
4852         contacts_record_h record = NULL;
4853
4854 /*
4855         if ((contact_err = contacts_connect_on_thread()) != CONTACTS_ERROR_NONE) {
4856                 EM_DEBUG_EXCEPTION("Open connect service failed [%d]", contact_err);
4857                 goto FINISH_OFF;
4858         }
4859 */
4860         if ((contact_err = emcore_search_contact_info_by_address(multi_user_name, _contacts_contact_email._uri, _contacts_contact_email.email, email_address, 1, &record)) != CONTACTS_ERROR_NONE) {
4861                 if (contact_err != CONTACTS_ERROR_NO_DATA) /* no matching record found */
4862                         EM_DEBUG_EXCEPTION("emcore_search_contact_info_by_address failed [%d]", contact_err);
4863                 goto FINISH_OFF;
4864         }
4865 /*
4866         if ((contact_err = contacts_list_get_count(list, &list_count)) != CONTACTS_ERROR_NONE) {
4867                 EM_DEBUG_EXCEPTION("contacts_list_get_count failed");
4868                 goto FINISH_OFF;
4869         }
4870
4871         if (list_count > 1) {
4872                 EM_DEBUG_EXCEPTION("Duplicated contacts info");
4873                 contact_err = CONTACTS_ERROR_INTERNAL;
4874                 goto FINISH_OFF;
4875         } else if (list_count == 0) {
4876                 EM_DEBUG_EXCEPTION("Not found contact info");
4877                 contact_err = CONTACTS_ERROR_NO_DATA;
4878                 goto FINISH_OFF;
4879         }
4880 */
4881
4882         if (contacts_record_get_str(record, _contacts_contact_email.display_name, &display) != CONTACTS_ERROR_NONE) {
4883                 EM_DEBUG_EXCEPTION("contacts_record_get_str failed");
4884                 goto FINISH_OFF;
4885         }
4886
4887         ret = true;
4888
4889 FINISH_OFF:
4890
4891 /*      contacts_disconnect_on_thread(); */
4892
4893         if (record != NULL)
4894                 contacts_record_destroy(record, false);
4895
4896         if (ret) {
4897                 *contact_display_name = display;
4898         } else {
4899                 *contact_display_name = NULL;
4900                 EM_SAFE_FREE(display);
4901         }
4902
4903         err = convert_contact_err_to_email_err(contact_err);
4904
4905         return err;
4906 }
4907
4908 INTERNAL_FUNC int emcore_connect_contacts_service(char *multi_user_name)
4909 {
4910         EM_DEBUG_FUNC_BEGIN();
4911         int contact_err = 0;
4912         int err = EMAIL_ERROR_NONE;
4913
4914         void *join_zone = NULL;
4915
4916     if (EM_SAFE_STRLEN(multi_user_name) > 0) {
4917                 if ((err = emcore_set_join_zone(multi_user_name, &join_zone)) != EMAIL_ERROR_NONE) {
4918                         EM_DEBUG_EXCEPTION("emcore_set_join_zone failed : [%d]", err);
4919                         return err;                     
4920                 }
4921     }
4922
4923         if ((contact_err = contacts_connect()) != CONTACTS_ERROR_NONE) {
4924                 EM_DEBUG_EXCEPTION("contacts_connect failed : [%d]", contact_err);
4925         }
4926
4927         err = convert_contact_err_to_email_err(contact_err);
4928
4929         if (join_zone)
4930                 emcore_unset_join_zone(join_zone);
4931
4932         EM_DEBUG_FUNC_END();
4933         return err;
4934 }
4935
4936 INTERNAL_FUNC int emcore_disconnect_contacts_service(char *multi_user_name)
4937 {
4938         EM_DEBUG_FUNC_BEGIN();
4939         int contact_err = 0;
4940         int err = EMAIL_ERROR_NONE;
4941
4942         void *join_zone = NULL;
4943
4944         if (EM_SAFE_STRLEN(multi_user_name) > 0) {
4945                 if ((err = emcore_set_join_zone(multi_user_name, &join_zone)) != EMAIL_ERROR_NONE) {
4946                         EM_DEBUG_EXCEPTION("emcore_set_join_zone failed : [%d]", err);
4947                         return err;                     
4948                 }
4949     }
4950
4951         if ((contact_err = contacts_disconnect()) != CONTACTS_ERROR_NONE) {
4952                 EM_DEBUG_EXCEPTION("contacts_disconnect failed : [%d]", contact_err);
4953         }
4954
4955         err = convert_contact_err_to_email_err(contact_err);
4956
4957         if (join_zone)
4958                 emcore_unset_join_zone(join_zone);
4959
4960         EM_DEBUG_FUNC_END();
4961         return err;
4962 }
4963
4964 #ifdef __FEATURE_BLOCKING_MODE__
4965
4966 #define ALLOWED_CONTACT_TYPE_NONE      0
4967 #define ALLOWED_CONTACT_TYPE_ALL       1
4968 #define ALLOWED_CONTACT_TYPE_FAVORITES 2
4969 #define ALLOWED_CONTACT_TYPE_CUSTOM    3
4970
4971 static int blocking_mode_status = false;
4972 INTERNAL_FUNC int blocking_mode_of_setting = false;
4973
4974 INTERNAL_FUNC void emcore_set_blocking_mode_of_setting(int input_blocking_mode_of_setting)
4975 {
4976         blocking_mode_of_setting = input_blocking_mode_of_setting;
4977 }
4978
4979 INTERNAL_FUNC int emcore_get_blocking_mode_status()
4980 {
4981         EM_DEBUG_FUNC_BEGIN("blocking_mode_status : [%d]", blocking_mode_status);
4982         EM_DEBUG_FUNC_END();
4983
4984         return blocking_mode_status;
4985 }
4986
4987 INTERNAL_FUNC void emcore_set_blocking_mode_status(int blocking_mode)
4988 {
4989         blocking_mode_status = blocking_mode;
4990 }
4991
4992 INTERNAL_FUNC int emcore_check_blocking_mode (char *multi_user_name, char *sender_address, int *blocking_status)
4993 {
4994         if ( !sender_address || !blocking_status ) {
4995                 EM_DEBUG_EXCEPTION ("NULL_PARAM sender_address[%p] blocking_status[%p]",
4996                                                    sender_address, blocking_status);
4997                 return EMAIL_ERROR_INVALID_PARAM;
4998         }
4999
5000
5001         GError   *gerror = NULL;
5002         GVariant *result = NULL;
5003         int ret = EMAIL_ERROR_NONE;
5004
5005 #if !GLIB_CHECK_VERSION(2, 36, 0) 
5006         g_type_init(); 
5007 #endif
5008
5009         GDBusProxy* bproxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
5010                                     G_DBUS_PROXY_FLAGS_NONE,
5011                                     NULL,
5012                                     EMAIL_SERVICE_NAME,
5013                                     EMAIL_SERVICE_PATH,
5014                                     EMAIL_SERVICE_NAME,
5015                                     NULL,
5016                                     &gerror);
5017         if (!bproxy) {
5018                 EM_DEBUG_EXCEPTION ("g_dbus_proxy_new_for_bus_sync error [%s]",
5019                                  gerror->message);
5020                 ret = EMAIL_ERROR_IPC_PROTOCOL_FAILURE;
5021                 goto FINISH_OFF;
5022         }
5023
5024         result = g_dbus_proxy_call_sync (bproxy, 
5025                                       "CheckBlockingMode", 
5026                                       g_variant_new ("(ss)", sender_address, multi_user_name), 
5027                                       G_DBUS_CALL_FLAGS_NONE, 
5028                                       -1, 
5029                                       NULL, 
5030                                       &gerror);
5031
5032
5033         if (!result) {
5034                 EM_DEBUG_EXCEPTION ("g_dbus_proxy_call_sync 'CheckBlockingMode' error [%s]", 
5035                                  gerror->message);
5036                 ret = EMAIL_ERROR_IPC_PROTOCOL_FAILURE;
5037                 goto FINISH_OFF;
5038         }
5039
5040         g_variant_get (result, "(ii)", blocking_status, &ret);
5041         g_variant_unref (result);
5042
5043 FINISH_OFF:
5044
5045         if (bproxy)
5046                 g_object_unref (bproxy);
5047         if (gerror)
5048                 g_error_free (gerror);
5049
5050         return ret;
5051 }
5052
5053 INTERNAL_FUNC int emcore_check_blocking_mode_internal (char *multi_user_name, char *sender_address, int *blocking_status)
5054 {
5055         EM_DEBUG_FUNC_BEGIN();
5056         int err = EMAIL_ERROR_NONE;
5057         int contact_error = 0;
5058         int person_id = 0;
5059         int allowed_contact_type = 0; /* 0 : NONE, 1 : All contacts, 2 : Favorites, 3 : Custom */
5060         char *person_id_string = NULL;
5061         char *str = NULL;
5062         char *token = NULL;
5063         contacts_record_h record = NULL;
5064
5065         if (!blocking_mode_of_setting) {
5066                 return err;
5067         }
5068
5069         if (vconf_get_int(VCONFKEY_SETAPPL_BLOCKINGMODE_ALLOWED_CONTACT_TYPE, &allowed_contact_type) != 0) {
5070                 EM_DEBUG_EXCEPTION("vconf_get_int failed");
5071                 err = EMAIL_ERROR_SYSTEM_FAILURE;
5072                 goto FINISH_OFF;
5073         }
5074
5075 /*
5076         if ((contact_error = contacts_connect_on_thread()) != CONTACTS_ERROR_NONE) {
5077                 EM_DEBUG_EXCEPTION("Open connect service failed [%d]", contact_error);
5078                 goto FINISH_OFF;
5079         }
5080 */
5081         switch (allowed_contact_type) {
5082         case ALLOWED_CONTACT_TYPE_NONE :
5083                 EM_DEBUG_LOG("allowed_contact_type is none : bloacking all(notification)");
5084                 *blocking_status = true;
5085                 break;
5086         case ALLOWED_CONTACT_TYPE_ALL :
5087                 EM_DEBUG_LOG("allowed_contact_type is ALL");
5088                 /* Search the allowed contact type in contact DB */
5089                 if ((contact_error = emcore_search_contact_info_by_address(multi_user_name, _contacts_person_email._uri, _contacts_person_email.email, sender_address, 1, &record)) != CONTACTS_ERROR_NONE) {
5090                         if (contact_error != CONTACTS_ERROR_NO_DATA) /* no matching record found */
5091                                 EM_DEBUG_EXCEPTION("emcore_search_contact_info_by_address failed [%d]", contact_error);
5092                         goto FINISH_OFF;
5093                 }
5094
5095                 if (record == NULL) {
5096                         EM_DEBUG_LOG("No search contact info");
5097                         goto FINISH_OFF;
5098                 }
5099
5100                 *blocking_status = false;
5101                 break;
5102
5103         case ALLOWED_CONTACT_TYPE_FAVORITES :
5104                 if ((contact_error = emcore_search_contact_info(multi_user_name, _contacts_person_email._uri, _contacts_person_email.email, sender_address, _contacts_person_email.is_favorite, true, 1, &record)) != CONTACTS_ERROR_NONE) {
5105                         EM_DEBUG_EXCEPTION("emcore_search_contact_info failed : [%d]", contact_error);
5106                         goto FINISH_OFF;
5107                 }
5108
5109                 if (record == NULL) {
5110                         EM_DEBUG_LOG("No search contact info");
5111                         goto FINISH_OFF;
5112                 }
5113
5114                 *blocking_status = false;
5115                 break;
5116
5117         case ALLOWED_CONTACT_TYPE_CUSTOM :
5118                 person_id_string = vconf_get_str(VCONFKEY_SETAPPL_BLOCKINGMODE_ALLOWED_CONTACT_LIST);
5119                 if (person_id_string == NULL) {
5120                         EM_DEBUG_LOG("Custom allowed contact type is NULL");
5121                         goto FINISH_OFF;
5122                 }
5123
5124                 if ((contact_error = emcore_search_contact_info_by_address(multi_user_name, _contacts_person_email._uri, _contacts_person_email.email, sender_address, 1, &record)) != CONTACTS_ERROR_NONE) {
5125                         if (contact_error != CONTACTS_ERROR_NO_DATA) /* no matching record found */
5126                                 EM_DEBUG_EXCEPTION("emcore_search_contact_info_by_address failed [%d]", contact_error);
5127                         goto FINISH_OFF;
5128                 }
5129
5130                 if (record == NULL) {
5131                         EM_DEBUG_LOG("No search contact info");
5132                         goto FINISH_OFF;
5133                 }
5134
5135                 if (contacts_record_get_int(record, _contacts_contact_email.person_id, &person_id) != CONTACTS_ERROR_NONE) {
5136                         EM_DEBUG_EXCEPTION("contacts_record_get_int failed");
5137                         goto FINISH_OFF;
5138                 }
5139
5140                 token = strtok_r(person_id_string, ",", &str);
5141                 do {
5142                         if (person_id == atoi(token)) {
5143                                 *blocking_status = false;
5144                                 break;
5145                         }
5146                 } while ((token = strtok_r(NULL, ",", &str)));
5147
5148                 break;
5149
5150         default:
5151                 EM_DEBUG_EXCEPTION("Invalid parameter : [%d]", allowed_contact_type);
5152                 *blocking_status = true;
5153                 err = EMAIL_ERROR_INVALID_PARAM;
5154         }
5155
5156 FINISH_OFF :
5157
5158 /*
5159         contacts_disconnect_on_thread();
5160 */
5161         err = convert_contact_err_to_email_err(contact_error);
5162
5163         EM_SAFE_FREE(person_id_string);
5164
5165         EM_DEBUG_FUNC_END();
5166         return err;
5167 }
5168
5169 #endif     /* __FEATURE_BLOCKING_MODE__ */
5170
5171 INTERNAL_FUNC char *emcore_set_mime_entity(char *mime_path)
5172 {
5173         EM_DEBUG_FUNC_BEGIN("mime_path : [%s]", mime_path);
5174         FILE *fp_read = NULL;
5175         FILE *fp_write = NULL;
5176         char *mime_entity = NULL;
5177         char *mime_entity_path = NULL;
5178         char temp_buffer[255] = {0,};
5179         int err = EMAIL_ERROR_NONE;
5180         int searched = 0;
5181
5182         if (!emcore_get_temp_file_name(&mime_entity_path, &err))  {
5183                 EM_DEBUG_EXCEPTION(" em_core_get_temp_file_name failed[%d]", err);
5184                 goto FINISH_OFF;
5185         }
5186
5187         /* get mime entity */
5188         if (mime_path != NULL) {
5189                 err = em_fopen(mime_path, "r", &fp_read);
5190                 if (err != EMAIL_ERROR_NONE) {
5191                         EM_DEBUG_EXCEPTION_SEC("File em_fopen(read) is failed : filename [%s]", mime_path);
5192                         goto FINISH_OFF;
5193                 }
5194
5195                 err = em_fopen(mime_entity_path, "w", &fp_write);
5196                 if (err != EMAIL_ERROR_NONE) {
5197                         EM_DEBUG_EXCEPTION_SEC("File em_fopen(write) is failed : filename [%s]", mime_entity_path);
5198                         goto FINISH_OFF;
5199                 }
5200
5201                 fseek(fp_read, 0, SEEK_SET);
5202                 fseek(fp_write, 0, SEEK_SET);
5203
5204                 while (fgets(temp_buffer, 255, fp_read) != NULL) {
5205                         mime_entity = strcasestr(temp_buffer, "content-type");
5206                         if (mime_entity != NULL && !searched)
5207                                 searched = 1;
5208
5209                         if (searched) {
5210                                 fprintf(fp_write, "%s", temp_buffer);
5211                         }
5212                 }
5213         }
5214
5215 FINISH_OFF:
5216         if (fp_read)
5217                 fclose(fp_read);
5218
5219         if (fp_write)
5220                 fclose(fp_write);
5221
5222         EM_SAFE_FREE(mime_entity);
5223
5224         EM_DEBUG_FUNC_END();
5225         return mime_entity_path;
5226 }
5227
5228 INTERNAL_FUNC int emcore_get_content_from_file(char *filename, char **contents, int *length)
5229 {
5230         EM_DEBUG_FUNC_BEGIN("filename[%s], contents[%p], length[%p]", filename, contents, length);
5231
5232         struct stat stat_buf;
5233         int fd = 0;
5234         int bytes_read = 0;
5235         int size = 0;
5236         int alloc_size = 0;
5237         int err = EMAIL_ERROR_NONE;
5238         char *buf = NULL;
5239         char errno_buf[ERRNO_BUF_SIZE] = {0};
5240
5241         if (filename == NULL || contents == NULL || length == NULL) {
5242                 err = EMAIL_ERROR_INVALID_PARAM;
5243                 EM_DEBUG_EXCEPTION("Invalid parameter");
5244                 goto FINISH_OFF;
5245         }
5246
5247         err = em_open(filename, O_RDONLY, 0 ,&fd);
5248         if (err != EMAIL_ERROR_NONE) {
5249                 EM_DEBUG_EXCEPTION("em_open error [%s][%d]", filename, err);
5250                 goto FINISH_OFF;
5251         }
5252
5253         if (fstat (fd, &stat_buf) < 0) {
5254                 EM_DEBUG_EXCEPTION("fstat failed for fd [%d]", fd);
5255                 err = EMAIL_ERROR_SYSTEM_FAILURE;
5256                 goto FINISH_OFF;
5257         }
5258
5259         if (stat_buf.st_size > 0 && S_ISREG(stat_buf.st_mode)) {
5260                 size = stat_buf.st_size;
5261                 alloc_size = size + 1;
5262                 buf = em_malloc(alloc_size);
5263                 if (buf == NULL) {
5264                         EM_DEBUG_EXCEPTION("malloc failed...");
5265                         err = EMAIL_ERROR_OUT_OF_MEMORY;
5266                         goto FINISH_OFF;
5267                 }
5268
5269                 bytes_read = 0;
5270                 errno = 0;
5271
5272                 while (bytes_read < size) {
5273                         ssize_t rd_size = 0;
5274                         rd_size = read (fd, buf + bytes_read, size - bytes_read);
5275
5276                         if (rd_size < 0) {
5277                                 if (errno != EINTR) {
5278                                         EM_DEBUG_EXCEPTION("read failed: %s", EM_STRERROR(errno_buf));
5279                                         err = EMAIL_ERROR_SYSTEM_FAILURE;
5280                                         goto FINISH_OFF;
5281                                 }
5282                         }
5283                         else if (rd_size == 0)
5284                                 break;
5285                         else
5286                                 bytes_read += rd_size;
5287                 }
5288
5289                 buf[bytes_read] = '\0';
5290
5291                 if (length)
5292                         *length = bytes_read;
5293
5294                 *contents = buf;
5295         }
5296         EM_SAFE_CLOSE (fd); /* prevent 39093 */
5297
5298         EM_DEBUG_FUNC_END();
5299         return err;
5300
5301 FINISH_OFF:
5302
5303         EM_SAFE_FREE(buf);
5304         EM_SAFE_CLOSE (fd); /* prevent 39093 */
5305         EM_DEBUG_FUNC_END ();
5306
5307         return err;
5308 }
5309
5310 INTERNAL_FUNC int emcore_set_content_to_file(const char *contents, char *dest_path, int length)
5311 {
5312         EM_DEBUG_FUNC_BEGIN("contents[%p], dest_path[%s], length[%d]", contents, dest_path, length);
5313         int fd = 0;
5314         int err = EMAIL_ERROR_NONE;
5315         char *tmp_path = NULL;
5316         char errno_buf[ERRNO_BUF_SIZE] = {0};
5317         int byte_written = 0; /* 39063 */
5318
5319         if (contents == NULL || dest_path == NULL || length <= 0) {
5320                 err = EMAIL_ERROR_INVALID_PARAM;
5321                 EM_DEBUG_EXCEPTION("Invalid parameter");
5322                 goto FINISH_OFF;
5323         }
5324
5325         tmp_path = emcore_mime_get_save_file_name(&err);
5326         if (!tmp_path) { /* prevent 39114 */
5327                 EM_DEBUG_EXCEPTION ("tmp_path NULL");
5328                 goto FINISH_OFF;
5329         }
5330
5331         err = em_open(tmp_path, O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, &fd);
5332         if (err != EMAIL_ERROR_NONE) {
5333                 EM_DEBUG_EXCEPTION("em_open error [%s][%d]", tmp_path, err);
5334                 goto FINISH_OFF;
5335         }
5336
5337         errno = 0;
5338         while(length > 0 && contents && errno == 0) {
5339                 byte_written = write(fd, (void *)contents, length);
5340                 if (byte_written < 0) {
5341                         /* interrupted by a signal */
5342                         if (errno == EINTR) {
5343                                 errno = 0;
5344                                 continue;
5345                         }
5346                         EM_DEBUG_EXCEPTION("write failed: %s", EM_STRERROR(errno_buf));
5347                         err = EMAIL_ERROR_SYSTEM_FAILURE;
5348                         goto FINISH_OFF;
5349                 }
5350
5351                 EM_DEBUG_LOG("NWRITTEN [%d]", byte_written);
5352                 length -= byte_written;
5353                 contents += byte_written;
5354         }
5355
5356         errno = 0;
5357         if (fsync(fd) != 0){
5358                 EM_DEBUG_EXCEPTION("fsync failed: %s", EM_STRERROR(errno_buf));
5359                 err = EMAIL_ERROR_SYSTEM_FAILURE;
5360         }
5361
5362         errno = 0;
5363         if (tmp_path && dest_path && rename(tmp_path, dest_path) != 0) {
5364                 EM_DEBUG_EXCEPTION("rename failed: %s", EM_STRERROR(errno_buf));
5365                 err = EMAIL_ERROR_SYSTEM_FAILURE;
5366                 goto FINISH_OFF;
5367         }
5368
5369 FINISH_OFF:
5370
5371         EM_SAFE_FREE(tmp_path);
5372         EM_SAFE_CLOSE (fd); /* prevent 39114, 39136*/
5373         EM_DEBUG_FUNC_END();
5374         return err;
5375 }
5376
5377 #ifdef __FEATURE_UPDATE_DB_TABLE_SCHEMA__
5378
5379 INTERNAL_FUNC int emcore_update_db_table_schema(char *multi_user_name)
5380 {
5381         EM_DEBUG_FUNC_BEGIN();
5382         int err = EMAIL_ERROR_NONE;
5383
5384         err = emstorage_update_db_table_schema(multi_user_name);
5385
5386         EM_DEBUG_FUNC_END("err[%d]", err);
5387         return err;
5388 }
5389
5390 #endif /* __FEATURE_UPDATE_DB_TABLE_SCHEMA__ */
5391
5392 INTERNAL_FUNC int emcore_unescape_from_url(char *input_url, char **output_url)
5393 {
5394         EM_DEBUG_FUNC_BEGIN("input_url[%p] output_url[%p]", input_url, output_url);
5395         int err = EMAIL_ERROR_NONE;
5396         int input_length = 0;
5397         int output_length = 0;
5398         char *result_string = NULL;
5399         CURL *curl = NULL;
5400
5401         if (input_url == NULL|| output_url == NULL) {
5402                 err = EMAIL_ERROR_INVALID_PARAM;
5403                 EM_DEBUG_EXCEPTION("Invalid parameter");
5404                 goto FINISH_OFF;
5405         }
5406
5407         *output_url = NULL;
5408         curl = curl_easy_init();
5409
5410         input_length = EM_SAFE_STRLEN(input_url);
5411
5412         result_string = curl_easy_unescape(curl, input_url, input_length, &output_length);
5413
5414         if (output_length)
5415                 *output_url = EM_SAFE_STRDUP(result_string);
5416
5417 FINISH_OFF:
5418         if (result_string)
5419                 curl_free(result_string);
5420
5421         if(curl)
5422                 curl_easy_cleanup(curl);
5423
5424         EM_DEBUG_FUNC_END("err[%d]", err);
5425         return err;
5426 }
5427
5428 INTERNAL_FUNC char *__em_get_month_in_string(int month)
5429 {
5430         EM_DEBUG_FUNC_BEGIN("month [%d]", month);
5431
5432         char *mon = NULL;
5433
5434         switch (month){
5435             case 0:
5436                         mon = strdup("jan");
5437                 break;
5438             case 1:
5439                         mon = strdup("feb");
5440                 break;
5441             case 2:
5442                         mon = strdup("mar");
5443                 break;
5444             case 3:
5445                         mon = strdup("apr");
5446                 break;
5447             case 4:
5448                         mon = strdup("may");
5449                 break;
5450             case 5:
5451                         mon = strdup("jun");
5452                 break;
5453             case 6:
5454                         mon = strdup("jul");
5455                 break;
5456             case 7:
5457                         mon = strdup("aug");
5458                 break;
5459             case 8:
5460                         mon = strdup("sep");
5461                 break;
5462             case 9:
5463                         mon = strdup("oct");
5464                 break;
5465             case 10:
5466                         mon = strdup("nov");
5467                 break;
5468             case 11:
5469                         mon = strdup("dec");
5470                 break;
5471         }
5472         return mon;
5473 }
5474
5475 INTERNAL_FUNC int emcore_make_date_string_for_search(time_t input_time, char *output_date_string)
5476 {
5477         EM_DEBUG_FUNC_BEGIN("input_time[%p] output_date_string[%p]", input_time, output_date_string);
5478         int err = EMAIL_ERROR_NONE;
5479         struct tm   *timeinfo = NULL;
5480         char *mon = NULL;
5481
5482         EM_DEBUG_LOG("RawTime Info [%lu]", input_time);
5483
5484         timeinfo = localtime(&input_time);
5485         if (timeinfo == NULL) {
5486                 EM_DEBUG_EXCEPTION("localtime failed");
5487                 err = EMAIL_ERROR_SYSTEM_FAILURE;
5488                 return err;
5489         }
5490
5491         EM_DEBUG_LOG(">>>>>Time %d %d %d %d %d %d", 1900+timeinfo->tm_year, timeinfo->tm_mon+1, timeinfo->tm_mday);
5492
5493         memset(output_date_string, 0x00, 20);
5494
5495         mon = __em_get_month_in_string(timeinfo->tm_mon);
5496         if (mon) {
5497                 snprintf(output_date_string, 16, "%d-%s-%04d", timeinfo->tm_mday, mon, 1900 + timeinfo->tm_year);
5498                 EM_DEBUG_LOG("DATE IS [ %s ] ", output_date_string);
5499                 EM_SAFE_FREE(mon);
5500         }
5501
5502         EM_DEBUG_FUNC_END("err [%d]", err);
5503         return err;
5504 }
5505
5506 INTERNAL_FUNC int emcore_make_uid_range_string(emcore_uid_list *uid_list, int total, char **output_uid_range_string)
5507 {
5508         EM_DEBUG_FUNC_BEGIN("uid_list[%p] total[%d] output_uid_range_string[%p]", uid_list, total, output_uid_range_string);
5509         int err = EMAIL_ERROR_NONE;
5510         emcore_uid_list *uid_list_prev = NULL;
5511         emcore_uid_list *uid_list_fast = uid_list;
5512         int index = 0;
5513         int msg_count = total;
5514         int uid_range_size = msg_count * 8 + 1000;
5515         char *uid_range = NULL;
5516
5517         if (uid_list == NULL || total == 0 || output_uid_range_string == NULL) {
5518                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5519                 err  = EMAIL_ERROR_INVALID_PARAM;
5520                 goto FINISH_OFF;
5521         }
5522
5523         EM_DEBUG_LOG("memory allocation for uid_range [%d, %d]", msg_count, uid_range_size);
5524         uid_range = malloc(sizeof(char) * uid_range_size);
5525
5526         if (uid_range == NULL){
5527                 EM_DEBUG_EXCEPTION("memory allocation for uid_range failed");
5528                 err  = EMAIL_ERROR_OUT_OF_MEMORY;
5529                 goto FINISH_OFF;
5530         }
5531
5532         uid_list_prev = uid_list_fast;
5533
5534         if (uid_list_fast->next == NULL){
5535                 /* Single list entry */
5536                 snprintf(uid_range, uid_range_size, "%d", atoi(uid_list_fast->uid));
5537                 EM_DEBUG_LOG("uid_range [%d]", uid_range);
5538         }
5539         else{
5540                 /* forming range of uids to be passed */
5541                 while (uid_list_fast  != NULL){
5542                         /* uid_list_fast = uid_list_fast->next; */
5543
5544                         if ((uid_list_fast->next != NULL) && (((atoi(uid_list_prev->uid)) - (atoi(uid_list_fast->next->uid))) == 1)){
5545                                 index += snprintf(uid_range+index, uid_range_size, "%d", atoi(uid_list_prev->uid));
5546
5547                                 uid_list_fast = uid_list_fast->next;
5548                                 uid_list_prev = uid_list_fast;
5549
5550                                 /* to make UID range string "abc, XX : YY" */
5551                                 while (uid_list_fast  != NULL){
5552                                         if (uid_list_fast->next == NULL)
5553                                                 break;
5554                                         if (((atoi(uid_list_prev->uid)) - (atoi(uid_list_fast->next->uid))) == 1){
5555                                                 uid_list_fast = uid_list_fast->next;
5556                                                 uid_list_prev = uid_list_fast;
5557                                         }
5558                                         else
5559                                                 break;
5560                                 }
5561                                 if ((uid_list_fast  != NULL) && (uid_list_fast->next  != NULL))
5562                                         index  += snprintf(uid_range+index, uid_range_size, ":%d,", atoi(uid_list_prev->uid));
5563                                 else
5564                                         index  += snprintf(uid_range+index, uid_range_size, ":%d", atoi(uid_list_prev->uid));
5565
5566                                 uid_list_fast = uid_list_fast->next;
5567                                 uid_list_prev = uid_list_fast;
5568                         }
5569                         else{
5570                                 if (uid_list_fast->next  != NULL)
5571                                         index  += snprintf(uid_range+index, uid_range_size, "%d,", atoi(uid_list_prev->uid));
5572                                 else
5573                                         index  += snprintf(uid_range+index, uid_range_size, "%d", atoi(uid_list_prev->uid));
5574                                 uid_list_fast = uid_list_fast->next;
5575                                 uid_list_prev = uid_list_fast;
5576                         }
5577                 }
5578         }
5579         EM_DEBUG_LOG("index [%d]", index);
5580
5581         *output_uid_range_string = uid_range;
5582
5583 FINISH_OFF:
5584
5585         EM_DEBUG_FUNC_END("err [%d]", err);
5586         return err;
5587 }
5588 /* EOF */