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