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