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