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