2.0_alpha release commit
[framework/messaging/email-service.git] / email-core / email-core-utils.c
1 /*
2 *  email-service
3 *
4 * Copyright (c) 2000 - 2011 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 <glib.h>
40 #include <glib-object.h>
41 #include <sys/time.h>
42 #include <unistd.h>
43 #include <sys/vfs.h>
44 #include <sys/stat.h>
45 #include <vconf.h>
46 #include <regex.h>
47 #include <pthread.h>
48
49 #include <contacts-svc.h>
50 #include <notification.h>
51
52 #include "email-types.h"
53 #include "email-core-global.h"
54 #include "email-core-utils.h"
55 #include "email-debug-log.h"
56 #include "email-core-mail.h"
57 #include "email-core-event.h"
58 #include "email-core-mailbox.h"
59 #include "email-core-account.h" 
60 #include "email-core-mailbox-sync.h" 
61 #include "email-core-mime.h"
62 #include "email-core-sound.h" 
63 #include "email-utilities.h"
64 #include "email-convert.h"
65
66 #define LED_TIMEOUT_SECS          12 
67 #define G_DISPLAY_LENGTH          256
68
69 #define DIR_SEPERATOR_CH          '/'
70 #define EMAIL_CH_QUOT             '"'
71 #define EMAIL_CH_BRACKET_S        '<'
72 #define EMAIL_CH_BRACKET_E        '>'
73 #define EMAIL_CH_COMMA            ','
74 #define EMAIL_CH_SEMICOLON        ';'
75 #define EMAIL_CH_ROUND_BRACKET_S  '('
76 #define EMAIL_CH_ROUND_BRACKET_E  ')'
77 #define EMAIL_CH_SQUARE_BRACKET_S '['
78 #define EMAIL_CH_SQUARE_BRACKET_E ']'
79 #define EMAIL_CH_SPACE            ' '
80 #define EMAIL_NOTI_ICON_PATH      "/opt/data/email/res/image/Q02_Notification_email.png"
81
82 static char _g_display[G_DISPLAY_LENGTH];
83
84
85 typedef struct  _em_transaction_info_type_t {
86         int mail_id;
87         int     handle; 
88         struct _em_transaction_info_type_t *next;
89
90 } em_transaction_info_type_t;
91
92 em_transaction_info_type_t  *g_transaction_info_list;
93
94 static email_option_t g_mail_option = 
95 {
96         0, /* priority                  */
97         1, /* keep_local_copy */
98         0, /* req_delivery_receipt */
99         0, /* req_read_receipt */
100         0, /* download_limit */
101         0, /* block_address */
102         0, /* block_subject */
103         NULL, /*  diplay name */
104 };
105
106 static email_session_t g_session_list[SESSION_MAX] = { {0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0},};
107
108
109 typedef struct emcore_account_list_t emcore_account_list_t;
110 struct emcore_account_list_t {
111         email_account_t *account;
112         emcore_account_list_t *next;
113 };
114
115 static emcore_account_list_t **g_account_reference = NULL;
116
117 INTERNAL_FUNC char *emcore_convert_mutf7_to_utf8(char *mailbox_name)
118 {
119         EM_DEBUG_FUNC_BEGIN();
120         return (char *)(utf8_from_mutf7((unsigned char *)mailbox_name));
121 }
122
123 INTERNAL_FUNC int emcore_set_account_reference(emcore_account_list_t **account_list, int account_num, int *err_code)
124 {
125         g_account_reference = (emcore_account_list_t **)account_list;
126         return 1;
127 }
128
129 email_option_t *emcore_get_option(int *err_code)
130 {
131         if (err_code != NULL)
132                 *err_code = EMAIL_ERROR_NONE;
133
134         return &g_mail_option;
135 }
136
137 INTERNAL_FUNC int emcore_set_option(email_option_t *opt, int *err_code)
138 {
139         EM_DEBUG_FUNC_BEGIN("opt[%p], err_code[%p]", opt, err_code);
140         
141         int err = EMAIL_ERROR_NONE;
142         
143         if (!opt) {
144                 EM_DEBUG_EXCEPTION("opt[%p]", opt);
145                 
146                 if (err_code != NULL)
147                         *err_code = EMAIL_ERROR_INVALID_PARAM;
148                 return false;
149         }
150         
151         memset(_g_display, 0, G_DISPLAY_LENGTH);
152         memcpy(&g_mail_option, opt, sizeof(g_mail_option));
153
154         if (opt->display_name_from && opt->display_name_from[0] != '\0')  {
155                 strncpy(_g_display, opt->display_name_from, G_DISPLAY_LENGTH - 1);
156                 g_mail_option.display_name_from = _g_display;
157         }
158         else
159                 g_mail_option.display_name_from = NULL;
160         
161         if (err_code != NULL)
162                 *err_code = err;
163         
164         return true;
165 }
166
167         
168
169
170
171 /*  in smtp case, path argument must be ENCODED_PATH_SMTP */
172 int emcore_get_long_encoded_path_with_account_info(email_account_t *account, char *path, int delimiter, char **long_enc_path, int *err_code)
173 {
174         EM_PROFILE_BEGIN(emCorelongEncodedpath);
175         EM_DEBUG_FUNC_BEGIN("account[%p], path[%s], delimiter[%d], long_enc_path[%p], err_code[%p]", account, path, delimiter, long_enc_path, err_code);
176         
177         int ret = false;
178         int error = EMAIL_ERROR_NONE;
179         char *p = NULL;
180         
181         size_t long_enc_path_len = 0;
182         
183         if (path == NULL || (path && strncmp(path, ENCODED_PATH_SMTP, strlen(ENCODED_PATH_SMTP)) != 0)) {               /*  imap or pop3 */
184                 EM_DEBUG_LOG("account->incoming_server_address[%p]", account->incoming_server_address);
185                 EM_DEBUG_LOG("account->incoming_server_address[%s]", account->incoming_server_address);
186                 
187                 if (!account->incoming_server_address) {
188                         EM_DEBUG_EXCEPTION("account->incoming_server_address is null");
189                         error = EMAIL_ERROR_INVALID_ACCOUNT;
190                         goto FINISH_OFF;
191                 }
192         
193                 long_enc_path_len = strlen(account->incoming_server_address) + (path ? strlen(path) : 0) + 64;
194                 
195                 *long_enc_path = em_malloc(long_enc_path_len);
196                 if (!*long_enc_path)  {
197                         EM_DEBUG_EXCEPTION("malloc failed...");
198                         error = EMAIL_ERROR_OUT_OF_MEMORY;
199                         goto FINISH_OFF;
200                 }
201                 
202                 p = *long_enc_path;
203                 
204                 /*  ex:"{mai.test.com:143/imap} or {mai.test.com:143/imap/tls}my-mailbox" */
205
206                 SNPRINTF(p, long_enc_path_len, "{%s:%d/%s/user=%d",
207                         account->incoming_server_address,
208                         account->incoming_server_port_number,
209                         account->incoming_server_type == EMAIL_SERVER_TYPE_POP3 ? "pop3" : "imap", account->account_id);
210                 
211                 if (account->incoming_server_secure_connection & 0x01)  {
212                         strncat(p, "/ssl", long_enc_path_len-(strlen(p)+1));
213                         /* strcat(p, "/tryssl"); */
214                 }
215
216                 /* Currently, receiving servers doesn't require tls. 
217                 if (account->incoming_server_secure_connection & 0x02)
218                         strncat(p, "/tls", long_enc_path_len-(strlen(p)+1));
219                 else
220                         strncat(p, "/notls", long_enc_path_len-(strlen(p)+1));
221                 */
222
223                 if (account->incoming_server_requires_apop) {
224                         EM_DEBUG_LOG("emcore_get_long_encoded_path - incoming_server_requires_apop - %d", account->incoming_server_requires_apop);
225                         strncat(p, "/apop", long_enc_path_len-(strlen(p)+1));
226                         EM_DEBUG_LOG("long_enc_path - %s", p);
227                 }
228         }
229         else  {         /*  smtp */
230                 long_enc_path_len = strlen(account->outgoing_server_address) + 64;
231                 
232                 *long_enc_path = em_malloc(strlen(account->outgoing_server_address) + 64);
233                 if (!*long_enc_path) {
234                         EM_DEBUG_EXCEPTION("\t malloc failed...\n");
235                         
236                         error = EMAIL_ERROR_OUT_OF_MEMORY;
237                         goto FINISH_OFF;
238                 }
239                 
240                 p = *long_enc_path;
241                 
242                 /*  ex:"mail.test.com:25/smtp" */
243
244                 SNPRINTF(p, long_enc_path_len, "%s:%d/%s",
245                         account->outgoing_server_address,
246                         account->outgoing_server_port_number,
247                         "smtp");
248                 
249                 if (account->outgoing_server_need_authentication) {
250                         SNPRINTF(p + strlen(p), long_enc_path_len-(strlen(p)), "/user=%d", account->account_id);
251                 }
252                 
253                 if (account->outgoing_server_secure_connection & 0x01) {
254                         strncat(p, "/ssl", long_enc_path_len-(strlen(p)+1));
255                         /* strcat(p, "/tryssl"); */
256                 }
257                 if (account->outgoing_server_secure_connection & 0x02)
258                         strncat(p, "/tls", long_enc_path_len-(strlen(p)+1));
259                 else
260                         strncat(p, "/notls", long_enc_path_len-(strlen(p)+1));
261         }
262
263         if (path == NULL || (path && strncmp(path, ENCODED_PATH_SMTP, strlen(ENCODED_PATH_SMTP)) != 0)) {
264                 strncat(p, "}", long_enc_path_len-(strlen(p)+1));
265                 
266                 if (path != NULL) {
267                         char *enc_name = NULL;
268                         
269                         if (!emcore_get_encoded_mailbox_name(path, &enc_name, &error))  {
270                                 EM_DEBUG_EXCEPTION("emcore_get_encoded_mailbox_name failed - %d", error);
271                                 *long_enc_path = NULL;
272                                 goto FINISH_OFF;
273                         }
274                         
275                         if (enc_name)  {
276                                 strncat(p, enc_name, long_enc_path_len-(strlen(p)+1));
277                                 EM_SAFE_FREE(enc_name);
278                         }
279                 }
280         }
281         
282         ret = true;
283         
284 FINISH_OFF:
285         if (ret != true)
286                 EM_SAFE_FREE(p);
287         
288         if (err_code != NULL)
289                 *err_code = error;
290         EM_PROFILE_END(emCorelongEncodedpath);
291         return ret;
292 }
293
294 int emcore_get_long_encoded_path(int account_id, char *path, int delimiter, char **long_enc_path, int *err_code)
295 {
296         EM_PROFILE_BEGIN(emCorelongEncodedpath);
297         EM_DEBUG_FUNC_BEGIN("account_id[%d], delimiter[%d], long_enc_path[%p], err_code[%p]", account_id, delimiter, long_enc_path, err_code);
298         
299         int ret = false;
300         int error = EMAIL_ERROR_NONE;
301         
302         email_account_t *ref_account = emcore_get_account_reference(account_id);
303         if (!ref_account)  {
304                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
305                 error = EMAIL_ERROR_INVALID_ACCOUNT;
306                 goto FINISH_OFF;
307         }
308
309         if (emcore_get_long_encoded_path_with_account_info(ref_account, path, delimiter, long_enc_path, &error) == false) {
310                 EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path_with_account_info failed [%d]", error);
311                 goto FINISH_OFF;
312         }
313
314         ret = true;
315         
316 FINISH_OFF:
317         if (err_code != NULL)
318                 *err_code = error;
319         EM_PROFILE_END(emCorelongEncodedpath);
320         return ret;
321 }
322
323 int emcore_get_encoded_mailbox_name(char *name, char **enc_name, int *err_code)
324 {
325         EM_DEBUG_FUNC_BEGIN("name[%s], enc_name[%p], err_code[%p]", name, enc_name, err_code);
326         
327         if (!name || !enc_name)  {
328                 if (err_code != NULL)
329                         *err_code = EMAIL_ERROR_INVALID_PARAM;
330                 EM_DEBUG_FUNC_END();
331                 return false;
332         }
333         
334         /* encoding mailbox name (Charset->UTF8->UTF7) */
335
336         *enc_name = em_malloc(strlen(name)+1);
337         if (*enc_name == NULL) {
338                 EM_DEBUG_EXCEPTION("malloc failed...");
339                 if (err_code != NULL)
340                         *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
341                 EM_DEBUG_FUNC_END();
342                 return false;
343         }
344         
345         strcpy(*enc_name, name);
346         
347         if (err_code != NULL)
348                 *err_code = EMAIL_ERROR_NONE;
349
350         EM_DEBUG_FUNC_END();
351         return true;
352 }
353
354 int emcore_get_temp_file_name(char **filename, int *err_code)
355 {
356         EM_DEBUG_FUNC_BEGIN("filename[%p], err_code[%p]", filename, err_code);
357         
358         int ret = false;
359         int error = EMAIL_ERROR_NONE;
360         
361         if (filename == NULL) {
362                 EM_DEBUG_EXCEPTION("\t filename[%p]\n", filename);
363                 error = EMAIL_ERROR_INVALID_PARAM;
364                 goto FINISH_OFF;
365         }
366         
367         char tempname[512] = {0x00, };
368         struct timeval tv;
369         
370
371         gettimeofday(&tv, NULL);
372         srand(tv.tv_usec);
373
374         /* Create Directory If deleted by user*/
375         emstorage_create_dir_if_delete();
376         
377         SNPRINTF(tempname, sizeof(tempname), "%s%c%s%c%d", MAILHOME, DIR_SEPERATOR_CH, MAILTEMP, DIR_SEPERATOR_CH, rand());
378         
379         char *p = EM_SAFE_STRDUP(tempname);
380         if (p == NULL) {
381                 EM_DEBUG_EXCEPTION("\t strdup failed...\n");
382                 error = EMAIL_ERROR_OUT_OF_MEMORY;
383                 goto FINISH_OFF;
384         }
385         
386         *filename = p;
387         
388         ret = true;
389
390 FINISH_OFF:
391         if (err_code != NULL)
392                 *err_code = error;
393         EM_DEBUG_FUNC_END();
394         return ret;
395 }
396
397 int emcore_get_file_name(char *path, char **filename, int *err_code)
398 {
399         EM_DEBUG_FUNC_BEGIN("path[%s], filename[%p], err_code[%p]", path, filename, err_code);
400         
401         int ret = false;
402         int error = EMAIL_ERROR_NONE;
403         
404         if (!path || !filename) {
405                 EM_DEBUG_EXCEPTION("path[%p], filename[%p]", path, filename);
406                 
407                 error = EMAIL_ERROR_INVALID_PARAM;
408                 goto FINISH_OFF;
409         }
410         
411         int i = (int)strlen(path);
412         
413         /*  get filename */
414         for (; i >= 0; i--)
415                 if (path[i] == DIR_SEPERATOR_CH)
416                         break;
417         
418         *filename = path + i + 1;
419         
420         ret = true;
421         
422 FINISH_OFF:
423                 if (err_code != NULL)
424                 *err_code = error;
425         EM_DEBUG_FUNC_END();
426         return ret;
427 }
428
429 int emcore_get_file_size(char *path, int *size, int *err_code)
430 {
431         EM_DEBUG_FUNC_BEGIN("path[%s], size[%p], err_code[%p]", path, size, err_code);
432         
433         int ret = false;
434         int error = EMAIL_ERROR_NONE;
435         
436         if ((path == NULL) || (size == NULL)) {
437                 EM_DEBUG_EXCEPTION("\t path[%p], size[%p]\n", path, size);
438                 
439                 error = EMAIL_ERROR_INVALID_PARAM;
440                 goto FINISH_OFF;
441         }
442         
443         struct stat st_buf;
444         
445         if (stat(path, &st_buf) < 0)  {
446                 EM_DEBUG_EXCEPTION("\t stat failed - %s\n", path);
447                 
448                 error = EMAIL_ERROR_SYSTEM_FAILURE;
449                 goto FINISH_OFF;
450         }
451         
452         *size = st_buf.st_size;
453         
454         ret = true;
455         
456 FINISH_OFF:
457         if (err_code != NULL)
458                 *err_code = error;
459         EM_DEBUG_FUNC_END();
460         return ret;
461 }
462
463
464
465 static int _emcore_check_host(char *host)
466 {
467         if (!host)
468                 return 0;
469         return strncmp(host, ".SYNTAX-ERROR.", strlen(".SYNTAX-ERROR."));
470 }
471
472
473
474 int emcore_get_address_count(char *addr_str, int *count, int *err_code)
475 {
476         EM_DEBUG_FUNC_BEGIN("addr_str[%s], count[%p], err_code[%p]", addr_str, count, err_code);
477         
478         int ret = false;
479         int error = EMAIL_ERROR_NONE;
480         
481         ADDRESS *addr = NULL;
482         ADDRESS *p_addr = NULL;
483         int i = 0, j;
484         char *p = NULL;
485
486
487         if (!count)  {
488                 EM_DEBUG_EXCEPTION("addr_str[%s], count[%p]", addr_str, count);
489                 error = EMAIL_ERROR_INVALID_PARAM;
490                 goto FINISH_OFF;
491         }
492         
493         if (addr_str != NULL)  {
494                 em_skip_whitespace(addr_str, &p);
495                 EM_DEBUG_LOG("em_skip_whitespace[p][%s]", p);
496
497
498                 for (i = 0, j = strlen(p); i < j; i++) 
499                         if (p[i] == ';') p[i] = ',';
500                 rfc822_parse_adrlist(&addr, p, NULL);
501                 EM_SAFE_FREE(p);
502
503         
504                 for (p_addr = addr, i = 0; p_addr; p_addr = p_addr->next, i++)  {
505                         if (p_addr->mailbox && p_addr->host) {  
506                                 if (!strncmp(p_addr->mailbox, "UNEXPECTED_DATA_AFTER_ADDRESS", strlen("UNEXPECTED_DATA_AFTER_ADDRESS")) || !strncmp(p_addr->mailbox, "INVALID_ADDRESS", strlen("INVALID_ADDRESS")) || !strncmp(p_addr->host, ".SYNTAX-ERROR.", strlen(".SYNTAX-ERROR."))) {
507                                         EM_DEBUG_LOG("Invalid address ");
508                                         continue;
509                                 }
510                         }                       
511                         if ((!p_addr->mailbox) || (_emcore_check_host(p_addr->host) == 0)) {
512                                 EM_DEBUG_EXCEPTION("\t invalid address : mailbox[%s], host[%s]\n", p_addr->mailbox, p_addr->host);
513                                 
514                                 error = EMAIL_ERROR_INVALID_ADDRESS;
515                                 /* goto FINISH_OFF; */
516                         }
517                 }
518         }
519
520         *count = i;
521         if (error != EMAIL_ERROR_INVALID_ADDRESS)
522         ret = true;
523         
524 FINISH_OFF:
525         if (addr) 
526                 mail_free_address(&addr);
527         
528         if (err_code != NULL)
529                 *err_code = error;
530         EM_DEBUG_FUNC_END();
531         return ret;
532 }
533
534 INTERNAL_FUNC int emcore_set_network_error(int err_code)
535 {
536         email_session_t *session = NULL;
537
538         EM_DEBUG_FUNC_BEGIN();
539
540         emcore_get_current_session(&session);
541
542         if (!session)
543                 return false;
544
545         session->network = err_code;
546         EM_DEBUG_FUNC_END();
547         return true;
548 }
549
550 int emcore_get_empty_session(email_session_t **session)
551 {
552         EM_DEBUG_FUNC_BEGIN("session[%p]", session);
553         
554         /*  lock()... */
555         
556         int i;
557         
558         for (i = 0; i < SESSION_MAX; i++)  {
559                 if (!g_session_list[i].status)  {
560                         memset(g_session_list+i, 0x00, sizeof(email_session_t));
561                         g_session_list[i].tid = GPOINTER_TO_INT(THREAD_SELF());
562                         g_session_list[i].status = true;
563                         break;
564                 }
565         }
566         
567         /*  unlock()... */
568         
569         if (session != NULL)
570                 *session = (i != SESSION_MAX) ? &g_session_list[i] : NULL;
571         EM_DEBUG_FUNC_END();
572         return (i != SESSION_MAX) ? true : false;
573 }
574
575 int emcore_clear_session(email_session_t *session)
576 {
577         EM_DEBUG_FUNC_BEGIN();
578         
579         if (session)
580                 memset(session, 0x00, sizeof(email_session_t));
581         EM_DEBUG_FUNC_END();
582         return true;
583 }
584
585 int emcore_get_current_session(email_session_t **session)
586 {
587         EM_DEBUG_FUNC_BEGIN("session[%p]", session);
588         
589         int i;
590         
591         for (i = 0; i < SESSION_MAX; i++)  {
592                 if (g_session_list[i].tid == GPOINTER_TO_INT(THREAD_SELF())) {
593                         if (session)
594                                 *session = g_session_list + i;
595                         
596                         break;
597                 }
598         }
599         
600         if (session)
601                 *session = (i != SESSION_MAX) ? g_session_list + i : NULL;
602         EM_DEBUG_FUNC_END();
603         return (i != SESSION_MAX) ? true : false;
604 }
605
606 int emcore_check_unread_mail()
607 {
608         EM_DEBUG_FUNC_BEGIN();
609         
610         int ret = false;
611         int err = EMAIL_ERROR_NONE;
612         int total_unread_count = 0;
613         int total_mail_count = 0;
614         email_mailbox_t mailbox;
615         
616         memset(&mailbox, 0x00, sizeof(email_mailbox_t));
617
618         /* ALL_ACCOUNT used, so not calling emstorage_get_mailbox_name_by_mailbox_type to get mailbox name */
619         mailbox.account_id = ALL_ACCOUNT;
620         mailbox.mailbox_name = NULL;
621         
622         if (!emcore_get_mail_count(&mailbox, &total_mail_count, &total_unread_count, &err))  {
623                 EM_DEBUG_EXCEPTION("emcore_get_mail_count failed [%d]", err);
624                 goto FINISH_OFF;
625         }
626         
627         EM_DEBUG_LOG("total_unread_count [%d]", total_unread_count);
628         
629         /* temporarily disable : set unread count to badge */
630         /*
631         if ( vconf_set_int(VCONF_KEY_UNREAD_MAIL_COUNT, total_unread_count) != 0 ) {
632                 EM_DEBUG_EXCEPTION("vconf_set_int failed");
633                 err = EMAIL_ERROR_GCONF_FAILURE;
634                 goto FINISH_OFF;
635         }
636
637         */
638         ret = true;
639 FINISH_OFF:
640
641         return ret;
642 }
643
644 static int emcore_add_notification_for_user_message(int account_id, int mail_id, char *title, char *content, time_t log_time)
645 {
646         EM_DEBUG_FUNC_BEGIN();
647         int ret = true;
648         int err = EMAIL_ERROR_NONE;
649         notification_h noti = NULL;
650         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
651         emstorage_account_tbl_t *account_tbl = NULL;
652
653         if (!emstorage_get_account_by_id(account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME, &account_tbl, true, &err)) {
654                 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed [%d]", err);
655                 goto FINISH_OFF;
656         }
657
658         noti = notification_new(NOTIFICATION_TYPE_NOTI, account_id, mail_id);
659
660         if(noti == NULL) {
661                 EM_DEBUG_EXCEPTION("notification_new failed");
662                 goto FINISH_OFF;
663         }
664         
665         if( (noti_err = notification_set_time(noti, log_time)) != NOTIFICATION_ERROR_NONE) {
666                 EM_DEBUG_EXCEPTION("notification_set_time failed [%d]", noti_err);
667                 goto FINISH_OFF;
668         }
669
670         if( (noti_err = notification_set_text_domain(noti, NATIVE_EMAIL_APPLICATION_PKG, "/opt/apps/org.tizen.email/res/locale/")) != NOTIFICATION_ERROR_NONE) {
671                 EM_DEBUG_EXCEPTION("notification_set_text_domain failed [%d]", noti_err);
672                 goto FINISH_OFF;
673         }
674
675         if( (noti_err = notification_set_title(noti, title, NULL)) != NOTIFICATION_ERROR_NONE) {
676                 EM_DEBUG_EXCEPTION("notification_set_title failed [%d]", noti_err);
677                 goto FINISH_OFF;
678         }
679
680         if( (noti_err = notification_set_content(noti, content, NULL)) != NOTIFICATION_ERROR_NONE) {
681                 EM_DEBUG_EXCEPTION("notification_set_content failed [%d]", noti_err);
682                 goto FINISH_OFF;
683         }
684         
685         if ((noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_GROUP_TITLE, title, NULL, NOTIFICATION_VARIABLE_TYPE_NONE)) != NOTIFICATION_ERROR_NONE) {
686                 EM_DEBUG_EXCEPTION("notification_set_text failed [%d]", noti_err);
687                 goto FINISH_OFF;
688         }
689
690         if ((noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_GROUP_CONTENT, content, NULL, NOTIFICATION_VARIABLE_TYPE_STRING, NOTIFICATION_COUNT_DISPLAY_TYPE_LEFT, NOTIFICATION_VARIABLE_TYPE_NONE)) != NOTIFICATION_ERROR_NONE) {
691                 EM_DEBUG_EXCEPTION("notification_set_text failed [%d]", noti_err);
692                 goto FINISH_OFF;
693         }
694
695         if ((noti_err = notification_set_image(noti, NOTIFICATION_IMAGE_TYPE_ICON, EMAIL_NOTI_ICON_PATH)) != NOTIFICATION_ERROR_NONE) {
696                 EM_DEBUG_EXCEPTION("notification_set_image failed [%d]", noti_err);
697                 goto FINISH_OFF;
698         }
699
700         if ((noti_err = notification_set_pkgname(noti, NATIVE_EMAIL_APPLICATION_PKG)) != NOTIFICATION_ERROR_NONE) {
701                 EM_DEBUG_EXCEPTION("notification_set_pkgname failed [%d]", noti_err);
702                 goto FINISH_OFF;
703         }               
704
705         if( (noti_err = notification_set_application(noti, NATIVE_EMAIL_APPLICATION_PKG)) != NOTIFICATION_ERROR_NONE) {
706                 EM_DEBUG_EXCEPTION("notification_set_application failed [%d]", noti_err);
707                 goto FINISH_OFF;
708         }
709
710         if( (noti_err = notification_insert(noti, NULL)) != NOTIFICATION_ERROR_NONE) {
711                 EM_DEBUG_EXCEPTION("notification_insert failed [%d]", noti_err);
712                 goto FINISH_OFF;
713         }
714
715 FINISH_OFF:
716         if( (noti_err = notification_free(noti)) != NOTIFICATION_ERROR_NONE) {
717                 EM_DEBUG_EXCEPTION("notification_free failed [%d]", noti_err);
718         }
719
720         if (noti_err != NOTIFICATION_ERROR_NONE)
721                 ret = false;
722
723         EM_DEBUG_FUNC_END("ret [%d]", ret);
724         return ret;     
725 }
726
727 INTERNAL_FUNC int emcore_show_user_message(int id, email_action_t action, int error)
728 {
729         EM_DEBUG_FUNC_BEGIN("id[%d], action[%d], error[%d]", id, action, error);
730
731         int ret = false;
732         time_t log_time = 0;
733         struct tm *log_time_tm;
734
735         time(&log_time);
736         log_time_tm = localtime(&log_time);
737         log_time = mktime(log_time_tm);
738
739         EM_DEBUG_LOG("sec[%d], min[%d], hour[%d], day[%d], month[%d], year[%d]" ,log_time_tm->tm_sec, log_time_tm->tm_min, log_time_tm->tm_hour, log_time_tm->tm_mday, log_time_tm->tm_mon, log_time_tm->tm_year);
740
741         if (action == EMAIL_ACTION_SEND_MAIL && error != EMAIL_ERROR_CANCELLED) {
742         /*  In case email is cancelled using cancel button in Outbox there is no need to show Cancel/Retry Pop up */
743                 emstorage_mail_tbl_t *mail_table_data = NULL;
744
745                 if (error == 0) /*  error 0 means 'this is not error' */
746                         return true;
747
748                 if (id <= 0) {
749                         EM_DEBUG_LOG("Invalid mail_id");
750                         return false;
751                 }
752                 
753                 if (!emstorage_get_mail_by_id(id, &mail_table_data, true, NULL)) {
754                         EM_DEBUG_LOG("Mail not found");
755                         return false;
756                 }
757
758                 if (!emcore_add_notification_for_user_message(mail_table_data->account_id, id, "Failed to send a mail.", mail_table_data->subject, log_time)) {
759                         EM_DEBUG_EXCEPTION("emcore_notification_set error");
760                         return false;
761                 }
762
763                 if (!emstorage_free_mail(&mail_table_data, 1, NULL))
764                         EM_DEBUG_EXCEPTION("emstorage_free_mail Failed");
765                 
766                 ret = true;
767         }
768         EM_DEBUG_FUNC_END("ret [%d]", ret);
769         return ret;
770 }
771         
772
773 /* storage space handling - 210709 */
774 int emcore_get_storage_status(void)
775 {
776         EM_DEBUG_FUNC_BEGIN();
777         int storage_status = 0, nError = 0;
778         
779         g_type_init();
780
781 #ifdef STORAGE_STATUS   
782         nError = vconf_get_int(PS_KEY_SYSTEM_STORAGE_MOVI_STATUS,
783                                                         &storage_status);       
784 #endif /*  STORAGE_STATUS */
785
786         if (nError == -1) {
787                 EM_DEBUG_EXCEPTION("vconf_get_int Failed");
788                 return false;
789         }
790         EM_DEBUG_FUNC_END();
791         return storage_status; 
792 }
793
794 int emcore_is_storage_full(int *err_code)
795 {
796         EM_DEBUG_FUNC_BEGIN();
797         
798         int ret = false;
799         int err = EMAIL_ERROR_NONE;
800         struct statfs buf = {0}; 
801         
802         if (statfs(DATA_PATH, &buf) == -1) {
803                 EM_DEBUG_EXCEPTION("statfs(\"%s\") failed - %d", DATA_PATH, errno);
804                 err = EMAIL_ERROR_SYSTEM_FAILURE;
805                 goto FINISH_OFF;
806         }
807         else  {
808                 long i_free = (buf.f_bfree * buf.f_bsize) / (1024 * 1024);
809                 EM_DEBUG_LOG("f_bfree[%d] f_bsize[%d]", buf.f_bfree, buf.f_bsize);
810                 EM_DEBUG_LOG("Free space of storage is[%ld] MB.", i_free);
811                 if (i_free < EMAIL_LIMITATION_FREE_SPACE)
812                         err = EMAIL_ERROR_MAIL_MEMORY_FULL;
813         }
814         
815         if (err == EMAIL_ERROR_MAIL_MEMORY_FULL)
816                 ret = true;
817         
818 FINISH_OFF:
819         if (err_code != NULL)
820                 *err_code = err;
821         EM_DEBUG_FUNC_END("ret[%d]", ret);
822         return ret;
823 }
824
825 int emcore_calc_mail_size(email_mail_data_t *input_mail_data, email_attachment_data_t *input_attachment_data_list, int input_attachment_count, int *output_size)
826 {
827         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);
828
829         struct stat            st_buf;
830         int                    mail_size = 0; /*  size of the plain text body and attachments */
831         int                    err       = EMAIL_ERROR_NONE;
832         int                    i         = 0;
833         
834         if (!input_mail_data || (input_attachment_count && !input_attachment_data_list) || (!input_attachment_count &&input_attachment_data_list) || !output_size)  {   
835                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
836                 err = EMAIL_ERROR_INVALID_PARAM;
837                 goto FINISH_OFF;
838         }
839         
840         if (input_mail_data->file_path_plain != NULL) {
841                 if (stat(input_mail_data->file_path_plain, &st_buf) < 0)  {
842                         EM_DEBUG_EXCEPTION("input_mail_data->file_path_plain : stat(\"%s\") failed...", input_mail_data->file_path_plain);
843                         err = EMAIL_ERROR_INVALID_MAIL;
844                         goto FINISH_OFF;
845                 }
846                 
847                 mail_size += st_buf.st_size;
848
849         }
850
851         if (input_mail_data->file_path_html != NULL) {
852                 if (stat(input_mail_data->file_path_html, &st_buf) < 0) {
853                         EM_DEBUG_EXCEPTION("input_mail_data->file_path_html : stat(\"%s\") failed...", input_mail_data->file_path_html);
854                         err = EMAIL_ERROR_INVALID_MAIL;
855                         goto FINISH_OFF;
856                 }
857                 
858                 mail_size += st_buf.st_size;
859         }
860         
861         for(i = 0; i < input_attachment_count; i++)  {
862                 if (stat(input_attachment_data_list[i].attachment_path, &st_buf) < 0)  {
863                         EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", input_attachment_data_list[i].attachment_path);
864                         err = EMAIL_ERROR_INVALID_MAIL;
865                         goto FINISH_OFF;
866                 }
867                 mail_size += st_buf.st_size;
868         }
869
870         *output_size = mail_size;
871         
872 FINISH_OFF:
873         
874         EM_DEBUG_FUNC_END("mail_size [%d]", mail_size);
875         return err;
876 }
877
878
879 /* parse the Full mailbox Path and Get the Alias Name of the Mailbox */
880 char *emcore_get_alias_of_mailbox(const char *mailbox_path)
881 {
882         EM_DEBUG_FUNC_BEGIN();
883         EM_IF_NULL_RETURN_VALUE(mailbox_path, NULL);
884
885         guint index = 0;
886         gchar **token_list = NULL;
887         gchar *mailbox = NULL, *name = NULL;
888         char *converted_name;
889         
890         
891         mailbox = g_strdup(mailbox_path);
892         token_list = g_strsplit_set(mailbox, "/", -1);
893         
894         if (mailbox)
895                 g_free(mailbox);
896
897         while (token_list[index] != NULL)
898                 index++;
899
900         name = g_strdup(token_list[index - 1]);
901         g_strfreev(token_list);
902
903         converted_name = emcore_convert_mutf7_to_utf8(name);
904         
905         if (name)
906                 g_free(name);
907
908         EM_DEBUG_FUNC_END();
909         return converted_name;
910 }
911
912
913 static int emcore_get_first_address(const char *full_address, char **alias, char **address)
914 {
915         EM_DEBUG_FUNC_BEGIN();
916
917         if (full_address == NULL || alias == NULL || address == NULL){
918                 EM_DEBUG_EXCEPTION("Invalid Param  :  full_address[%p], alias[%p], address[%p]", full_address, alias, address);
919                 return false;
920         }
921
922         char *s = NULL;
923         char *alias_start = NULL;
924         char *alias_end = NULL;
925         char *alias_cursor = NULL;
926         char *address_start = NULL;
927         char *address_end = NULL;
928         char *first_address = NULL;
929         
930         if (full_address){
931                 s = (char *)strchr((char *)full_address, ';');
932                 if (s == NULL)
933                         first_address = strdup(full_address);   /*  only one  address */
934                 else
935                         first_address = strndup(full_address, s - full_address);        /*  over two addresses */
936
937                 /*  get alias */
938                 *alias = NULL;
939                 if ((alias_start = (char *)strchr((char *)first_address, '\"'))){       
940                         alias_start++;
941                         alias_cursor = alias_start;
942                         while ((alias_cursor = (char *)strchr((char *)(alias_cursor), '\"'))){
943                                 alias_end = alias_cursor;
944                                 alias_cursor++;
945                                 if (*alias_cursor == 0)
946                                         break;
947                         }
948                         if (alias_end)  {       /*  there is "alias" */
949                                 *alias = strndup(alias_start, alias_end - alias_start); 
950                                 EM_DEBUG_LOG("alias [%s]", *alias);
951                         }
952                 }
953
954                 /*  get address */
955                 *address = NULL;
956                 if (alias_end == NULL)
957                         s = first_address;
958                 else
959                         s = alias_end+1;
960                 if ((address_start = (char *)strchr((char  *)s, '<'))){ 
961                         address_start++;
962                         if ((address_end = (char *)strchr((char  *)address_start, '>')))
963                                 *address = strndup(address_start, address_end - address_start); /*  (alias) <(addr)>  ... */
964                         else
965                                 *address = strdup(s);
966                 }
967                 else
968                *address = strdup(s);    /*  (addr) ; ...                 :  no alias */
969         }
970
971         EM_SAFE_FREE(first_address);
972         EM_DEBUG_FUNC_END();    
973         return true;
974 }               
975
976 void emcore_fill_address_information_of_mail_tbl(emstorage_mail_tbl_t *mail_data)
977 {
978         EM_DEBUG_FUNC_BEGIN("mail_data [%p]", mail_data);
979
980         char *first_alias   = NULL;
981         char *first_address = NULL;
982         char *recipient     = NULL;
983
984         /*  sender alias & address */
985         if (emcore_get_first_address(mail_data->full_address_from, &first_alias, &first_address) == true) {
986                 if (first_alias == NULL) {
987                         mail_data->alias_sender = EM_SAFE_STRDUP(first_address);
988                 }
989                 else {
990                         mail_data->alias_sender = first_alias;
991                         first_alias = NULL;
992                 }
993                 mail_data->email_address_sender = first_address;
994                 first_address = NULL;
995         }
996
997         /*  recipient alias & address */
998         if (mail_data->full_address_to != NULL)
999                 recipient = mail_data->full_address_to;
1000         else if (mail_data->full_address_cc != NULL)
1001                 recipient = mail_data->full_address_cc;
1002         else if (mail_data->full_address_bcc != NULL)
1003                 recipient = mail_data->full_address_bcc;
1004         
1005         if (emcore_get_first_address(recipient, &first_alias, &first_address) == true) {
1006                 if (first_alias == NULL)
1007                         mail_data->alias_recipient = EM_SAFE_STRDUP(first_address);
1008                 else
1009                         mail_data->alias_recipient = first_alias;
1010
1011                 mail_data->email_address_recipient = first_address;
1012         }
1013         EM_DEBUG_FUNC_END();    
1014 }
1015
1016
1017 int emcore_get_preview_text_from_file(const char *input_plain_path, const char *input_html_path, int input_preview_buffer_length, char **output_preview_buffer)
1018 {
1019         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);
1020         
1021         int          err = EMAIL_ERROR_NONE;
1022         unsigned int byte_read = 0;
1023         unsigned int byte_written = 0;
1024         int          result_strlen = 0;
1025         int          local_preview_buffer_length = 0;
1026         char        *local_preview_text = NULL;
1027         char        *encoding_type = NULL;
1028         char        *utf8_encoded_string = NULL;
1029         FILE        *fp = NULL;
1030         GError      *glib_error = NULL;
1031         struct stat  st_buf;
1032
1033         if (!output_preview_buffer) {
1034                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1035                 err = EMAIL_ERROR_INVALID_PARAM;
1036                 goto FINISH_OFF;
1037         }
1038
1039         local_preview_buffer_length = input_preview_buffer_length * 2;
1040
1041         if (input_html_path != NULL) {  
1042                 /*      get preview text from html file */
1043                 if( (err = em_get_encoding_type_from_file_path(input_html_path, &encoding_type)) != EMAIL_ERROR_NONE) {
1044                         EM_DEBUG_EXCEPTION("em_get_encoding_type_from_file_path failed [%s]", err);
1045                         goto FINISH_OFF;
1046                 }
1047                 
1048                 if (stat(input_html_path, &st_buf) < 0)  {
1049                         EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", input_html_path);
1050                         err = EMAIL_ERROR_INVALID_MAIL;
1051                         goto FINISH_OFF;
1052                 }
1053                 
1054                 if (!(fp = fopen(input_html_path, "r")))        {
1055                         EM_DEBUG_EXCEPTION("fopen failed [%s]", input_html_path);
1056                         err = EMAIL_ERROR_SYSTEM_FAILURE;
1057                         goto FINISH_OFF;
1058                 }
1059
1060                 if (!(local_preview_text = (char*)em_malloc(sizeof(char) * (st_buf.st_size + 1)))) {
1061                         EM_DEBUG_EXCEPTION("em_malloc failed");
1062                         err = EMAIL_ERROR_OUT_OF_MEMORY;
1063                         goto FINISH_OFF;
1064                 }
1065                 
1066                 byte_read = fread(local_preview_text, sizeof(char), st_buf.st_size, fp);
1067                 
1068                 if (ferror(fp)) {
1069                         EM_DEBUG_EXCEPTION("fread failed [%s]", input_plain_path);
1070                         err = EMAIL_ERROR_SYSTEM_FAILURE;
1071                         goto FINISH_OFF;
1072                 }
1073
1074                 if ( (err = emcore_strip_HTML(local_preview_text)) != EMAIL_ERROR_NONE) {
1075                         EM_DEBUG_EXCEPTION("emcore_strip failed");
1076                         goto FINISH_OFF;
1077                 }
1078
1079                 result_strlen = EM_SAFE_STRLEN(local_preview_text);
1080         }
1081
1082         if (local_preview_text == NULL && input_plain_path != NULL) {   
1083                 /*  get preview text from plain text file */
1084                 if( (err = em_get_encoding_type_from_file_path(input_plain_path, &encoding_type)) != EMAIL_ERROR_NONE) {
1085                         EM_DEBUG_EXCEPTION("em_get_encoding_type_from_file_path failed [%s]", err);
1086                         goto FINISH_OFF;
1087                 }
1088                 
1089                 if (!(fp = fopen(input_plain_path, "r")))  {
1090                         EM_DEBUG_EXCEPTION("fopen failed [%s]", input_plain_path);
1091                         err = EMAIL_ERROR_SYSTEM_FAILURE;
1092                         goto FINISH_OFF;
1093                 }
1094
1095                 if (!(local_preview_text = (char*)em_malloc(sizeof(char) * local_preview_buffer_length))) {
1096                         EM_DEBUG_EXCEPTION("em_malloc failed");
1097                         goto FINISH_OFF;
1098                 }
1099                 
1100                 byte_read = fread(local_preview_text, sizeof(char), local_preview_buffer_length - 1, fp);
1101                 
1102                 if (ferror(fp)) {
1103                         EM_DEBUG_EXCEPTION("fread failed [%s]", input_plain_path);
1104                         err = EMAIL_ERROR_SYSTEM_FAILURE;
1105                         goto FINISH_OFF;
1106                 }
1107
1108                 reg_replace(local_preview_text, CR_STRING, "");
1109                 reg_replace(local_preview_text, LF_STRING, "");
1110                 reg_replace(local_preview_text, TAB_STRING, "");
1111                         
1112                 result_strlen = EM_SAFE_STRLEN(local_preview_text);
1113         }       
1114         
1115
1116         if(local_preview_text) {
1117                 if(encoding_type && strcasecmp(encoding_type, "UTF-8") != 0) {
1118                         EM_DEBUG_LOG("encoding_type [%s]", encoding_type);
1119                         utf8_encoded_string = (char*)g_convert (local_preview_text, -1, "UTF-8", encoding_type, &byte_read, &byte_written, &glib_error);
1120
1121                         if(utf8_encoded_string) {
1122                                 EM_SAFE_FREE(local_preview_text);
1123                                 local_preview_text = utf8_encoded_string;
1124                         }
1125                         else 
1126                                 EM_DEBUG_EXCEPTION("g_convert failed");
1127                 }
1128                 
1129                 if (!(*output_preview_buffer = (char*)em_malloc(sizeof(char) * (result_strlen + 1)))) {
1130                         EM_DEBUG_EXCEPTION("em_malloc failed");
1131                         err = EMAIL_ERROR_OUT_OF_MEMORY;
1132                         goto FINISH_OFF;
1133                 }
1134
1135                 EM_SAFE_STRNCPY(*output_preview_buffer, local_preview_text, result_strlen);
1136                 /* EM_DEBUG_LOG("local_preview_text[%s], byte_read[%d], result_strlen[%d]", local_preview_text, byte_read, result_strlen); */
1137         }
1138
1139 FINISH_OFF:
1140
1141         EM_SAFE_FREE(local_preview_text);
1142         EM_SAFE_FREE(encoding_type);
1143         
1144         if (fp != NULL)
1145                 fclose(fp);
1146
1147         EM_DEBUG_FUNC_END("err [%d]", err);
1148         return err;
1149 }
1150
1151 INTERNAL_FUNC int emcore_add_transaction_info(int mail_id, int handle , int *err_code)
1152 {
1153         EM_DEBUG_FUNC_BEGIN("mail_id[%d], handle[%d]", mail_id, handle);
1154
1155         int ret = false;
1156         int err = EMAIL_ERROR_NONE ;
1157         em_transaction_info_type_t  *pTransinfo = NULL ;
1158         em_transaction_info_type_t      *pTemp = NULL;
1159
1160         EM_DEBUG_LOG("g_transaction_info_list[%p]", g_transaction_info_list);
1161         pTransinfo = g_transaction_info_list ;
1162         
1163         if (!(pTemp = em_malloc(sizeof(em_transaction_info_type_t))))  {
1164                 EM_DEBUG_EXCEPTION("malloc failed...");
1165                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1166                 goto FINISH_OFF;
1167         }
1168         pTemp->mail_id = mail_id ;
1169         pTemp->handle = handle;
1170                 
1171         if (!pTransinfo) {
1172                 pTransinfo = pTemp ;
1173                 g_transaction_info_list = pTransinfo ;
1174         }
1175         else {
1176                 while (pTransinfo->next)
1177                         pTransinfo = pTransinfo->next;
1178                 pTransinfo->next = pTemp;
1179         }
1180         ret = true ;
1181         
1182 FINISH_OFF:
1183
1184         if (err_code)
1185                 *err_code = err;
1186         EM_DEBUG_FUNC_END("g_transaction_info_list[%p]", g_transaction_info_list);
1187         return ret;     
1188 }
1189
1190 INTERNAL_FUNC int emcore_get_handle_by_mailId_from_transaction_info(int mail_id, int *pHandle)
1191 {
1192         EM_DEBUG_FUNC_BEGIN("mail_id[%d], handle[%p]", mail_id, pHandle);
1193
1194         int ret = false;
1195         em_transaction_info_type_t  *pTransinfo = NULL ;
1196         
1197         if (g_transaction_info_list == NULL) {
1198                 EM_DEBUG_EXCEPTION("g_transaction_info_list NULL");
1199                 return false;
1200         }
1201         pTransinfo = g_transaction_info_list;
1202
1203         do {
1204                 EM_DEBUG_LOG("pTransinfo->mail_id[%d]", pTransinfo->mail_id);
1205                 if (pTransinfo->mail_id == mail_id) {
1206                         *pHandle = pTransinfo->handle;
1207                         ret = true;
1208                         EM_DEBUG_LOG("*pHandle[%d]", *pHandle);
1209                         break;
1210                 }
1211                 else
1212                         pTransinfo = pTransinfo->next ;
1213         }while (pTransinfo);
1214         EM_DEBUG_FUNC_END();
1215         return ret;
1216 }
1217
1218 INTERNAL_FUNC int emcore_delete_transaction_info_by_mailId(int mail_id )
1219 {
1220         EM_DEBUG_FUNC_BEGIN("mail_id[%d]", mail_id);
1221
1222         em_transaction_info_type_t  *pTransinfo ;
1223         em_transaction_info_type_t *pTemp = NULL;
1224
1225         if (g_transaction_info_list == NULL) {
1226                 EM_DEBUG_EXCEPTION("g_transaction_info_list NULL");
1227                 return false;
1228         }
1229         pTransinfo = g_transaction_info_list;
1230
1231         EM_DEBUG_LOG("pTransinfo[%p]", pTransinfo);
1232
1233         do {
1234                 EM_DEBUG_LOG("pTransinfo->mail_id[%d]", pTransinfo->mail_id);
1235                 if (pTransinfo->mail_id == mail_id) {
1236                         pTemp = pTransinfo->next ;
1237                         if (!pTemp) {
1238                                 EM_SAFE_FREE(pTransinfo) ;
1239                                 g_transaction_info_list = NULL;
1240                         }
1241                         else {
1242                                 pTransinfo->mail_id = pTransinfo->next->mail_id;
1243                                 pTransinfo->handle = pTransinfo->next->handle ;
1244                                 pTransinfo->next = pTransinfo->next->next;
1245
1246                                 EM_SAFE_FREE(pTemp);
1247                         }
1248                         break;
1249                 }
1250                 else {
1251                         pTransinfo = pTransinfo->next ;
1252                 }       
1253
1254         }while (pTransinfo);
1255         EM_DEBUG_FUNC_END();
1256         return true;
1257 }
1258
1259
1260 #include <regex.h>
1261
1262 int reg_replace (char *input_source_text, char *input_old_pattern_string, char *input_new_string)
1263 {
1264         EM_DEBUG_FUNC_BEGIN("input_source_text [%p], input_old_pattern_string [%p], input_new_string [%p]", input_source_text, input_old_pattern_string, input_new_string);
1265         int         error_code = EMAIL_ERROR_NONE; 
1266         char       *pos = NULL;
1267         int         so, n, nmatch, source_text_length, n_count = 1;
1268         regmatch_t *pmatch = NULL; 
1269         regex_t     reg_pattern;
1270
1271         if(!input_source_text || !input_old_pattern_string || !input_new_string) {
1272                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1273                 error_code = EMAIL_ERROR_INVALID_PARAM;
1274                 goto FINISH_OFF;                
1275         }
1276
1277         source_text_length = strlen(input_source_text);
1278
1279         regcomp(&reg_pattern, input_old_pattern_string, REG_ICASE);
1280
1281         nmatch = reg_pattern.re_nsub + 1;
1282
1283         EM_DEBUG_LOG("nmatch [%d]", nmatch);
1284
1285         if(nmatch < 1) {
1286                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_DATA");
1287                 error_code = EMAIL_ERROR_INVALID_DATA;
1288                 goto FINISH_OFF;                
1289         }
1290         
1291         pmatch = (regmatch_t*)em_malloc(sizeof(regmatch_t) * nmatch);
1292
1293         if(pmatch == NULL) {
1294                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_OUT_OF_MEMORY");
1295                 error_code = EMAIL_ERROR_OUT_OF_MEMORY;
1296                 goto FINISH_OFF;                
1297         }
1298         
1299         for (pos = input_new_string; *pos ; pos++) {
1300                 if (*pos == '\\' && *(pos + 1) > '0' && *(pos + 1) <= '9') {
1301                 
1302                         so = pmatch[*(pos + 1) - 48].rm_so;
1303                         n  = pmatch[*(pos + 1) - 48].rm_eo - so;
1304
1305                         EM_DEBUG_LOG("so [%d], n [%d]", so, n);
1306                         
1307                         if (so < 0 || strlen (input_new_string) + n - 1 > source_text_length) 
1308                                 break;
1309
1310                         memmove (pos + n, pos + 2, strlen (pos) - 1);
1311                         memmove (pos, input_source_text + so, n);
1312                         pos = pos + n - 2;
1313                 }
1314         }
1315         
1316         for (pos = input_source_text; !regexec (&reg_pattern, pos, 1, pmatch, 0);) {
1317                 n = pmatch[0].rm_eo - pmatch[0].rm_so;
1318                 pos += pmatch[0].rm_so;
1319
1320                 memmove (pos + strlen (input_new_string), pos + n, strlen (pos) - n + 1);
1321                 memmove (pos, input_new_string, strlen (input_new_string));
1322                 pos += strlen (input_new_string);
1323                 n_count++;
1324         }
1325         
1326 FINISH_OFF:
1327
1328         EM_SAFE_FREE(pmatch);
1329         regfree (&reg_pattern);
1330         
1331         EM_DEBUG_FUNC_END("error_code [%d]", error_code);
1332         return error_code;
1333 }
1334
1335
1336 int emcore_strip_HTML(char *source_string)
1337 {
1338         EM_DEBUG_FUNC_BEGIN("source_string [%p]", source_string);
1339
1340         int result = EMAIL_ERROR_NONE;
1341         
1342         reg_replace(source_string, CR_STRING, " ");
1343         reg_replace(source_string, LF_STRING, " ");
1344         reg_replace(source_string, TAB_STRING, " ");
1345         reg_replace(source_string, "<head[^>]*>", "<head>"); /*  "<()*head([^>])*>", "<head>" */
1346         reg_replace(source_string, "<*/head>", "</head>");  /*  "(<()*(/)()*head()*>)", "</head>" */
1347         reg_replace(source_string, "<head>.*</head>", ""); /*  "(<head>).*(</head>)", "" */
1348
1349         reg_replace(source_string, "<*/p>", " ");
1350         reg_replace(source_string, "<br>", " ");
1351
1352         /*   "<[^>]*>", " */
1353         reg_replace(source_string, "<[^>]*>", "");
1354
1355
1356         /*   "&bull;", " *  */
1357         /* reg_replace(source_string, "&bull;", " * "); */
1358
1359         /*   "&lsaquo;", "< */
1360         /* reg_replace(source_string, "&lsaquo;", "<"); */
1361
1362         /*   "&rsaquo;", "> */
1363         /* reg_replace(source_string, "&rsaquo;", ">"); */
1364
1365         /*   "&trade;", "(tm) */
1366         /* reg_replace(source_string, "&trade;", "(tm)"); */
1367
1368         /*   "&frasl;", "/ */
1369         /* reg_replace(source_string, "&frasl;", "/"); */
1370
1371         /*  "&lt;", "< */
1372         reg_replace(source_string, "&lt;", "<");
1373
1374         /*  "&gt;", "> */
1375         reg_replace(source_string, "&gt;", ">");
1376
1377         /*  "&copy;", "(c) */
1378         /* reg_replace(source_string, "&copy;", "(c)"); */
1379
1380         /* "&quot;", "\' */
1381         reg_replace(source_string, "&quot;", "\'");
1382
1383         /*  "&nbsp;", " */
1384         reg_replace(source_string, "&nbsp;", " ");
1385
1386         reg_replace(source_string, "  ", " ");
1387
1388         EM_DEBUG_FUNC_END();
1389
1390         return result;
1391 }
1392
1393 #define MAX_NOTI_STRING_LENGTH          8096
1394
1395 INTERNAL_FUNC int emcore_convert_structure_to_string(void *struct_var, char **encoded_string, email_convert_struct_type_e type)
1396 {
1397         EM_DEBUG_FUNC_BEGIN("Struct type[%d]", type);
1398         
1399         char *buf = NULL;
1400         char delimiter[] = {0x01, 0x00};
1401         int error_code = EMAIL_ERROR_NONE;
1402
1403         buf = (char *) malloc(MAX_NOTI_STRING_LENGTH * sizeof(char));
1404         if (NULL == buf) {
1405                 error_code = EMAIL_ERROR_OUT_OF_MEMORY;
1406                 goto FINISH_OFF;                
1407         }
1408         switch (type) {
1409                 case EMAIL_CONVERT_STRUCT_TYPE_MAIL_LIST_ITEM: {
1410                         email_mail_list_item_t *item = (email_mail_list_item_t *)struct_var;
1411                         SNPRINTF(buf, MAX_NOTI_STRING_LENGTH, 
1412                                 "%d%c"  /*      int  mail_id                                    ; */
1413                                 "%d%c"  /*      int  account_id                                                                 ; */
1414                                 "%d%c"  /*      int  mailbox_id                                 ; */
1415                                 "%s%c"  /*      char from[STRING_LENGTH_FOR_DISPLAY]                    ; */
1416                                 "%s%c"  /*      char from_email_address[MAX_EMAIL_ADDRESS_LENGTH]; */
1417                                 "%s%c"  /*      char recipients[STRING_LENGTH_FOR_DISPLAY]      ; */
1418                                 "%s%c"  /*      char subject[STRING_LENGTH_FOR_DISPLAY]                 ; */
1419                                 "%d%c"  /*      int  is_text_downloaded                                                 ; */
1420                                 "%d%c"  /*      time_t date_time                                ; */
1421                                 "%d%c"  /*      int  flags_seen_field                           ; */
1422                                 "%d%c"  /*      int  priority                                                                   ; */
1423                                 "%d%c"  /*      int  save_status                                ; */
1424                                 "%d%c"  /*      int  is_locked                                                                  ; */
1425                                 "%d%c"  /*      int  is_report_mail                                                             ; */
1426                                 "%d%c"  /*      int  recipients_count                                                   ; */
1427                                 "%d%c"  /*      int  has_attachment                             ; */
1428                                 "%d%c"  /*      int  has_drm_attachment                                                 ; */
1429                                 "%s%c"  /*      char previewBodyText[MAX_PREVIEW_TEXT_LENGTH]   ; */
1430                                 "%d%c"  /*      int  thread_id                                                                  ; */
1431                                 "%d%c", /*      int  thread_item_count                                                  ; */
1432
1433                                 item->mail_id,              delimiter[0],
1434                                 item->account_id,           delimiter[0],
1435                                 item->mailbox_id,           delimiter[0],
1436                                 item->from,                 delimiter[0],
1437                                 item->from_email_address,   delimiter[0],
1438                                 item->recipients,           delimiter[0],
1439                                 item->subject,              delimiter[0],
1440                                 item->is_text_downloaded,   delimiter[0],
1441                                 (unsigned int)item->date_time,       delimiter[0],
1442                                 item->flags_seen_field,     delimiter[0],
1443                                 item->priority,                         delimiter[0],
1444                                 item->save_status,          delimiter[0],
1445                                 item->is_locked,            delimiter[0],
1446                                 item->is_report_mail,       delimiter[0],
1447                                 item->recipients_count,     delimiter[0],
1448                                 item->has_attachment,       delimiter[0],
1449                                 item->has_drm_attachment,   delimiter[0],
1450                                 item->previewBodyText,      delimiter[0],
1451                                 item->thread_id,            delimiter[0],
1452                                 item->thread_item_count,    delimiter[0]
1453                         );
1454                 }               
1455                         break;
1456         }
1457
1458 FINISH_OFF:
1459         if (encoded_string)
1460                 *encoded_string = buf;
1461         EM_DEBUG_FUNC_END("Struct -> String:[%s]\n", buf);
1462         return error_code;
1463 }
1464
1465 INTERNAL_FUNC int emcore_convert_string_to_structure(const char *encoded_string, void **struct_var, email_convert_struct_type_e type)
1466 {
1467         EM_DEBUG_FUNC_BEGIN();
1468         
1469         int ret = false;
1470         void *temp_struct = NULL;
1471         char *buff = NULL;
1472         char *current_pos = NULL;
1473         char *found_pos = NULL;
1474         char delimiter[] = {0x01, 0x00};
1475         int error_code = EMAIL_ERROR_NONE;
1476
1477         EM_DEBUG_LOG("Struct Type[%d], String:[%s]", type, encoded_string);
1478
1479         buff = (char *)EM_SAFE_STRDUP(encoded_string);
1480         if (NULL == buff) {
1481                 error_code = EMAIL_ERROR_OUT_OF_MEMORY;
1482                 goto FINISH_OFF;                
1483         }
1484         
1485         switch (type) {
1486                 case EMAIL_CONVERT_STRUCT_TYPE_MAIL_LIST_ITEM: {
1487                         email_mail_list_item_t *item = (email_mail_list_item_t *)malloc(sizeof(email_mail_list_item_t));
1488                         if (NULL == item) {
1489                                 error_code = EMAIL_ERROR_OUT_OF_MEMORY;
1490                                 goto FINISH_OFF;                
1491                         }
1492                         temp_struct = (void *)item;
1493
1494                         current_pos = buff;
1495
1496                         /*  mail_id */
1497                         found_pos = strstr(current_pos, delimiter);
1498                         if (NULL == found_pos) {
1499                                 error_code = EMAIL_ERROR_INVALID_DATA;
1500                                 goto FINISH_OFF;                
1501                         }
1502                         *found_pos = NULL_CHAR;
1503                         item->mail_id = atoi(current_pos);
1504                         EM_DEBUG_LOG("mail_id[%d]", item->mail_id);
1505                         current_pos = found_pos + 1;
1506
1507                         /*  account_id */
1508                         found_pos = strstr(current_pos, delimiter);
1509                         if (NULL == found_pos) {
1510                                 error_code = EMAIL_ERROR_INVALID_DATA;
1511                                 goto FINISH_OFF;                
1512                         }
1513                         *found_pos = NULL_CHAR;
1514                         item->account_id = atoi(current_pos);
1515                         EM_DEBUG_LOG("account_id[%d]", item->account_id);
1516                         current_pos = found_pos + 1;
1517
1518                         /*  mailbox_id */
1519                         found_pos = strstr(current_pos, delimiter);
1520                         if (NULL == found_pos) {
1521                                 error_code = EMAIL_ERROR_INVALID_DATA;
1522                                 goto FINISH_OFF;                
1523                         }
1524                         *found_pos = NULL_CHAR;
1525                         item->mailbox_id = atoi(current_pos);
1526                         EM_DEBUG_LOG("mailbox_id[%s]", item->mailbox_id);
1527                         current_pos = found_pos + 1;
1528
1529                         /*  from */
1530                         found_pos = strstr(current_pos, delimiter);
1531                         if (NULL == found_pos) {
1532                                 error_code = EMAIL_ERROR_INVALID_DATA;
1533                                 goto FINISH_OFF;                
1534                         }
1535                         *found_pos = NULL_CHAR;
1536                         strncpy(item->from, current_pos, STRING_LENGTH_FOR_DISPLAY-1);
1537                         EM_DEBUG_LOG("from[%s]", item->from);
1538                         current_pos = found_pos + 1;
1539
1540                         /*  from_email_address */
1541                         found_pos = strstr(current_pos, delimiter);
1542                         if (NULL == found_pos) {
1543                                 error_code = EMAIL_ERROR_INVALID_DATA;
1544                                 goto FINISH_OFF;                
1545                         }
1546                         *found_pos = NULL_CHAR;
1547                         strncpy(item->from_email_address, current_pos, STRING_LENGTH_FOR_DISPLAY-1);
1548                         EM_DEBUG_LOG("from_email_address[%s]", item->from_email_address);
1549                         current_pos = found_pos + 1;
1550
1551                         /*  recipients */
1552                         found_pos = strstr(current_pos, delimiter);
1553                         if (NULL == found_pos) {
1554                                 error_code = EMAIL_ERROR_INVALID_DATA;
1555                                 goto FINISH_OFF;                
1556                         }
1557                         *found_pos = NULL_CHAR;
1558                         strncpy(item->recipients, current_pos, STRING_LENGTH_FOR_DISPLAY-1);
1559                         EM_DEBUG_LOG("recipients[%s]", item->recipients);
1560                         current_pos = found_pos + 1;                    
1561
1562                         /*  subject */
1563                         found_pos = strstr(current_pos, delimiter);
1564                         if (NULL == found_pos) {
1565                                 error_code = EMAIL_ERROR_INVALID_DATA;
1566                                 goto FINISH_OFF;                
1567                         }
1568                         *found_pos = NULL_CHAR;
1569                         strncpy(item->subject, current_pos, STRING_LENGTH_FOR_DISPLAY-1);
1570                         EM_DEBUG_LOG("subject[%s]", item->subject);
1571                         current_pos = found_pos + 1;                    
1572
1573                         /*  is_text_downloaded */
1574                         found_pos = strstr(current_pos, delimiter);
1575                         if (NULL == found_pos) {
1576                                 error_code = EMAIL_ERROR_INVALID_DATA;
1577                                 goto FINISH_OFF;                
1578                         }
1579                         *found_pos = NULL_CHAR;
1580                         item->is_text_downloaded = atoi(current_pos);
1581                         EM_DEBUG_LOG("is_text_downloaded[%d]", item->is_text_downloaded);
1582                         current_pos = found_pos + 1;
1583
1584                         /*  datatime */
1585                         found_pos = strstr(current_pos, delimiter);
1586                         if (NULL == found_pos) {
1587                                 error_code = EMAIL_ERROR_INVALID_DATA;
1588                                 goto FINISH_OFF;                
1589                         }
1590                         *found_pos = NULL_CHAR;
1591                         item->date_time = atoi(current_pos);
1592                         EM_DEBUG_LOG("date_time[%d]", item->date_time);
1593                         current_pos = found_pos + 1;
1594
1595                         /*  flags_seen_field */
1596                         found_pos = strstr(current_pos, delimiter);
1597                         if (NULL == found_pos) {
1598                                 error_code = EMAIL_ERROR_INVALID_DATA;
1599                                 goto FINISH_OFF;                
1600                         }
1601                         *found_pos = NULL_CHAR;
1602                         item->flags_seen_field = atoi(current_pos);
1603                         EM_DEBUG_LOG("flags_seen_field[%d]", item->flags_seen_field);
1604                         current_pos = found_pos + 1;
1605
1606                         /*  priority */
1607                         found_pos = strstr(current_pos, delimiter);
1608                         if (NULL == found_pos) {
1609                                 error_code = EMAIL_ERROR_INVALID_DATA;
1610                                 goto FINISH_OFF;                
1611                         }
1612                         *found_pos = NULL_CHAR;
1613                         item->priority = atoi(current_pos);
1614                         EM_DEBUG_LOG("priority[%d]", item->priority);
1615                         current_pos = found_pos + 1;                    
1616
1617                         /*  save_status */
1618                         found_pos = strstr(current_pos, delimiter);
1619                         if (NULL == found_pos) {
1620                                 error_code = EMAIL_ERROR_INVALID_DATA;
1621                                 goto FINISH_OFF;                
1622                         }
1623                         *found_pos = NULL_CHAR;
1624                         item->save_status = atoi(current_pos);
1625                         EM_DEBUG_LOG("save_status[%d]", item->save_status);
1626                         current_pos = found_pos + 1;    
1627
1628                         /*  is_locked */
1629                         found_pos = strstr(current_pos, delimiter);
1630                         if (NULL == found_pos) {
1631                                 error_code = EMAIL_ERROR_INVALID_DATA;
1632                                 goto FINISH_OFF;                
1633                         }
1634                         *found_pos = NULL_CHAR;
1635                         item->is_locked = atoi(current_pos);
1636                         EM_DEBUG_LOG("is_locked[%d]", item->is_locked);
1637                         current_pos = found_pos + 1;    
1638
1639                         /*  is_report_mail */
1640                         found_pos = strstr(current_pos, delimiter);
1641                         if (NULL == found_pos) {
1642                                 error_code = EMAIL_ERROR_INVALID_DATA;
1643                                 goto FINISH_OFF;                
1644                         }
1645                         *found_pos = NULL_CHAR;
1646                         item->is_report_mail = atoi(current_pos);
1647                         EM_DEBUG_LOG("is_report_mail[%d]", item->is_report_mail);
1648                         current_pos = found_pos + 1;    
1649
1650                         /*  recipients_count */
1651                         found_pos = strstr(current_pos, delimiter);
1652                         if (NULL == found_pos) {
1653                                 error_code = EMAIL_ERROR_INVALID_DATA;
1654                                 goto FINISH_OFF;                
1655                         }
1656                         *found_pos = NULL_CHAR;
1657                         item->recipients_count = atoi(current_pos);
1658                         EM_DEBUG_LOG("is_report_mail[%d]", item->recipients_count);
1659                         current_pos = found_pos + 1;                            
1660
1661                         /*  has_attachment */
1662                         found_pos = strstr(current_pos, delimiter);
1663                         if (NULL == found_pos) {
1664                                 error_code = EMAIL_ERROR_INVALID_DATA;
1665                                 goto FINISH_OFF;                
1666                         }
1667                         *found_pos = NULL_CHAR;
1668                         item->has_attachment = atoi(current_pos);
1669                         EM_DEBUG_LOG("has_attachment[%d]", item->has_attachment);
1670                         current_pos = found_pos + 1;                            
1671
1672                         /*  has_drm_attachment */
1673                         found_pos = strstr(current_pos, delimiter);
1674                         if (NULL == found_pos) {
1675                                 error_code = EMAIL_ERROR_INVALID_DATA;
1676                                 goto FINISH_OFF;                
1677                         }
1678                         *found_pos = NULL_CHAR;
1679                         item->has_drm_attachment = atoi(current_pos);
1680                         EM_DEBUG_LOG("has_drm_attachment[%d]", item->has_drm_attachment);
1681                         current_pos = found_pos + 1;                            
1682
1683                         /*  previewBodyText */
1684                         found_pos = strstr(current_pos, delimiter);
1685                         if (NULL == found_pos) {
1686                                 error_code = EMAIL_ERROR_INVALID_DATA;
1687                                 goto FINISH_OFF;                
1688                         }
1689                         *found_pos = NULL_CHAR;
1690                         strncpy(item->previewBodyText, current_pos, MAX_PREVIEW_TEXT_LENGTH-1);
1691                         EM_DEBUG_LOG("previewBodyText[%s]", item->previewBodyText);
1692                         current_pos = found_pos + 1;    
1693
1694                         /*  thread_id */
1695                         found_pos = strstr(current_pos, delimiter);
1696                         if (NULL == found_pos) {
1697                                 error_code = EMAIL_ERROR_INVALID_DATA;
1698                                 goto FINISH_OFF;                
1699                         }
1700                         *found_pos = NULL_CHAR;
1701                         item->thread_id = atoi(current_pos);
1702                         EM_DEBUG_LOG("thread_id[%d]", item->thread_id);
1703                         current_pos = found_pos + 1;    
1704
1705                         /*  thread_item_count - the last item */
1706                         item->thread_item_count = atoi(current_pos);
1707                         EM_DEBUG_LOG("thread_item_count[%d]", item->thread_item_count);
1708                         
1709                 }
1710                         break;
1711                         
1712                 default:
1713                         EM_DEBUG_EXCEPTION("Unknown structure type");
1714                         break;
1715         }
1716
1717         ret = true;
1718         
1719 FINISH_OFF:
1720         EM_SAFE_FREE(buff);
1721         if (ret == true) {
1722                 if (struct_var)
1723                         *struct_var = temp_struct;
1724         }
1725         else {
1726                 switch (type) {
1727                         case EMAIL_CONVERT_STRUCT_TYPE_MAIL_LIST_ITEM:
1728                                 EM_SAFE_FREE(temp_struct);
1729                                 break;
1730                         default:
1731                                 break;
1732                 }
1733         }
1734         EM_DEBUG_FUNC_END();
1735         return error_code;
1736 }
1737
1738
1739 /*  emcore_send_noti_for_new_mail is not used currently because DBUS could not send very long message.*/
1740 /*  But I think it can be used to notify incomming new mail for replacing NOTI_MAIL_ADD with some modification(uid should be replaced with mail_id).  */
1741 /*  This notification is including addtional information comparing NOTI_MAIL_ADD. */
1742 /*  By this change, email application will be able to add email item without additional DB query.  */
1743 /*  It might improve performance of sync email.  */
1744 /*  kyuho.jo 2010-09-07 */
1745
1746 INTERNAL_FUNC int emcore_send_noti_for_new_mail(int account_id, char *mailbox_name, char *subject, char *from, char *uid, char *datetime)
1747 {
1748         EM_DEBUG_FUNC_BEGIN("mailbox_name(%s) subject(%s), from(%s), uid(%s), datetime(%s)", mailbox_name, subject, from, uid, datetime);
1749         int error_code = EMAIL_ERROR_NONE;
1750         char *param_string = NULL;
1751
1752         if (mailbox_name == NULL || subject == NULL || from == NULL || uid == NULL || datetime == NULL) {
1753                 error_code = EMAIL_ERROR_INVALID_PARAM;
1754                 EM_DEBUG_EXCEPTION("Invalid parameter, mailbox_name(%p), subject(%p), from(%p), uid(%p), datetime(%p)", mailbox_name, subject, from, uid, datetime);
1755                 goto FINISH_OFF;
1756         }
1757
1758         param_string = malloc(strlen(mailbox_name) + strlen(subject) + strlen(from) + strlen(uid) + strlen(datetime) + 5);
1759
1760         if (param_string == NULL) {
1761                 error_code = EMAIL_ERROR_OUT_OF_MEMORY;
1762                 EM_DEBUG_EXCEPTION("Memory allocation for 'param_string' is failed");
1763                 goto FINISH_OFF;
1764         }
1765
1766         memset(param_string, 0x00, sizeof(param_string));
1767
1768         SNPRINTF(param_string, sizeof(param_string), "%s%c%s%c%s%c%s%c%s", mailbox_name, 0x01, subject, 0x01, from, 0x01, uid, 0x01, datetime);
1769
1770         if (emstorage_notify_network_event(NOTI_DOWNLOAD_NEW_MAIL, account_id, param_string, 0, 0) == 0) {      /*  failed */
1771                 error_code = EMAIL_ERROR_UNKNOWN;
1772                 EM_DEBUG_EXCEPTION("emstorage_notify_network_event is failed");
1773                 goto FINISH_OFF;
1774         }
1775
1776 FINISH_OFF:
1777         if (param_string)
1778                 free(param_string);     
1779         EM_DEBUG_FUNC_END();
1780         return error_code;
1781 }
1782
1783
1784
1785 #define MAX_TITLE_LENGTH 1024
1786 int emcore_update_notification_for_unread_mail(int account_id)
1787 {
1788         EM_DEBUG_FUNC_BEGIN("account_id[%d]", account_id); 
1789         int error_code = EMAIL_ERROR_NONE;
1790         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
1791
1792         if((noti_err = notification_update(NULL)) != NOTIFICATION_ERROR_NONE) {
1793                 EM_DEBUG_EXCEPTION("notification_update failed");
1794                 goto FINISH_OFF;
1795         }
1796
1797 FINISH_OFF:
1798
1799         EM_DEBUG_FUNC_END("return [%d]", error_code);
1800         return error_code;
1801 }
1802
1803 INTERNAL_FUNC int emcore_finalize_sync(int account_id, int *error)
1804 {
1805         EM_DEBUG_FUNC_BEGIN("account_id [%d], error [%p]", account_id, error);
1806         int err = EMAIL_ERROR_NONE, ret = true, result_sync_status = SYNC_STATUS_FINISHED;
1807         emstorage_account_tbl_t *account_tbl = NULL;
1808
1809         if ((err = emcore_update_sync_status_of_account(account_id, SET_TYPE_MINUS, SYNC_STATUS_SYNCING)) != EMAIL_ERROR_NONE)
1810                 EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
1811
1812         if (!emstorage_get_sync_status_of_account(ALL_ACCOUNT, &result_sync_status, &err))
1813                 EM_DEBUG_EXCEPTION("emstorage_get_sync_status_of_account failed [%d]", err);
1814
1815         if (result_sync_status == SYNC_STATUS_HAVE_NEW_MAILS) {
1816                 if (!emcore_update_notification_for_unread_mail(ALL_ACCOUNT))
1817                         EM_DEBUG_EXCEPTION("emcore_update_notification_for_unread_mail failed");        
1818                         emcore_check_unread_mail();
1819                         /* Temp.. exception for EAS */
1820                         if(account_id >= FIRST_ACCOUNT_ID)
1821                                 emstorage_get_account_by_id(account_id, EMAIL_ACC_GET_OPT_DEFAULT, &account_tbl, true, &err);
1822                         if(account_tbl && account_tbl->incoming_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC)
1823                                 emcore_start_alert();
1824
1825                         if ((err = emcore_update_sync_status_of_account(account_id, SET_TYPE_MINUS, SYNC_STATUS_HAVE_NEW_MAILS)) != EMAIL_ERROR_NONE)
1826                                 EM_DEBUG_EXCEPTION("emcore_update_sync_status_of_account failed [%d]", err);
1827
1828         }
1829         EM_DEBUG_FUNC_END();
1830         return ret;
1831 }
1832
1833 INTERNAL_FUNC int emcore_clear_all_notifications()
1834 {
1835         int account_count = 0, i;
1836         emstorage_account_tbl_t *account_list;
1837         int error_code = EMAIL_ERROR_NONE;
1838         
1839         if(!emstorage_get_account_list(&account_count, &account_list, true, false, &error_code)) {
1840                 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed");
1841                 goto FINISH_OFF;
1842         }
1843
1844         for(i = 0; i < account_count; i++) {
1845                 emcore_delete_notification_by_account(account_list[i].account_id);
1846         }
1847
1848 FINISH_OFF:
1849         if(account_count) {
1850                 emstorage_free_account(&account_list, account_count, NULL);
1851         }
1852
1853         EM_DEBUG_FUNC_END("return[%d]", error_code);
1854         return error_code;
1855 }
1856
1857 INTERNAL_FUNC int emcore_add_notification_for_unread_mail(emstorage_mail_tbl_t *input_mail_tbl_data)
1858 {
1859         EM_DEBUG_FUNC_BEGIN("input_mail_tbl_data[%p]", input_mail_tbl_data);
1860
1861         int err = EMAIL_ERROR_NONE;
1862
1863         if (input_mail_tbl_data == NULL) {
1864                 EM_DEBUG_EXCEPTION("input_mail_tbl_data is NULL");
1865                 return EMAIL_ERROR_INVALID_PARAM;
1866         }
1867
1868         EM_DEBUG_FUNC_END("err[%d]", err);
1869         return err;
1870 }
1871
1872
1873 INTERNAL_FUNC int emcore_delete_notification_for_read_mail(int mail_id)
1874 {
1875         EM_DEBUG_FUNC_BEGIN();
1876         int error_code = EMAIL_ERROR_NONE;
1877
1878         EM_DEBUG_FUNC_END();
1879         return error_code;
1880 }
1881
1882 #define EAS_EXECUTABLE_PATH "/usr/bin/eas-engine"
1883
1884 INTERNAL_FUNC int emcore_delete_notification_by_account(int account_id)
1885 {
1886         EM_DEBUG_FUNC_BEGIN("account_id [%d]", account_id);
1887         int error_code = EMAIL_ERROR_NONE;
1888         EM_DEBUG_FUNC_END();
1889         return error_code;
1890 }
1891
1892 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
1893
1894 /**
1895  * @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)
1896  * Prepare a linked list of uid ranges with each node having a uid_range and lowest and highest uid in it.
1897  *
1898  *@author                                       h.gahlaut@samsung.com
1899  * @param[in] id_set                    Specifies the array of mail_id and corresponding server_mail_id sorted by server_mail_ids in ascending order
1900  * @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
1901  * @param[in] range_len         Specifies the maximum length of string of range allowed. 
1902  * @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
1903  * @param[out] err_code         Returns the error code.
1904  * @remarks                                     An example of a uid_range formed is 2:6,8,10,14:15,89, 
1905  *                                                      While using it the caller should remove the ending, (comma)
1906  * @return This function returns true on success or false on failure.
1907  */
1908  
1909 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)
1910 {
1911         EM_DEBUG_FUNC_BEGIN();
1912
1913         int ret = false;
1914         int error = EMAIL_ERROR_NONE;
1915
1916         if (NULL == id_set || id_set_count  <= 0 || NULL == uid_range_set) {
1917                 EM_DEBUG_EXCEPTION(" Invalid Parameter id_set[%p] id_set_count[%d] uid_range_set[%p]", id_set, id_set_count, uid_range_set);
1918                 error = EMAIL_ERROR_INVALID_PARAM;
1919                 goto FINISH_OFF;
1920         }
1921
1922         int i = 0;
1923         unsigned long current_uid = 0;
1924         unsigned long first_uid = 0;
1925         unsigned long last_uid = 0;
1926         const int max_subset_string_size = MAX_SUBSET_STRING_SIZE;                      
1927         char subset_string[MAX_SUBSET_STRING_SIZE] = {0,};      
1928         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*/
1929
1930         if (range_len < (max_subset_string_size + 1))           /* 1 for ending NULL character */ {
1931                 EM_DEBUG_EXCEPTION(" Invalid Parameter range_len[%d]", range_len);
1932                 error = EMAIL_ERROR_INVALID_PARAM;
1933                 goto FINISH_OFF;
1934         }
1935         
1936         EM_DEBUG_LOG("id set count[%d] range_len[%d]", id_set_count, range_len);
1937         
1938         do {
1939                 first_uid = last_uid = current_uid = id_set[i].server_mail_id;
1940                 /* Start subset string by putting first server mail id in it from id_set*/
1941                 memset(subset_string, 0x00, max_subset_string_size);
1942                 SNPRINTF(subset_string, max_subset_string_size, "%lu", first_uid);
1943                 ++i;
1944                 
1945                 /* Check if only one server mail id was left in id_set */
1946                 if (i >= id_set_count) {
1947                         /* No more server mail id left in id_set */
1948                         if (false == emcore_append_subset_string_to_uid_range(subset_string, &current_node, uid_range_set, range_len, first_uid, last_uid)) {
1949                                 EM_DEBUG_EXCEPTION("emcore_append_subset_string_to_uid_range failed");
1950                                 goto FINISH_OFF;
1951                         }
1952                         break;
1953                 }
1954                 else {
1955                         /* 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 */
1956                         do {
1957                                 current_uid = id_set[i].server_mail_id;
1958                                 if (current_uid == (last_uid + 1)) {
1959                                         last_uid = current_uid;
1960                                         ++i;                    
1961                                 }
1962                                 else {  
1963                                         memset(subset_string, 0x00, max_subset_string_size);
1964                                         if (first_uid != last_uid)      /* Form subset string by first_uid:last_uid */
1965                                                 SNPRINTF(subset_string, max_subset_string_size, "%lu:%lu", first_uid, last_uid);
1966                                         else    /* Form subset string by first_uid */
1967                                                 SNPRINTF(subset_string, max_subset_string_size, "%lu", first_uid);
1968                                         
1969                                         if (false == emcore_append_subset_string_to_uid_range(subset_string, &current_node, uid_range_set, range_len, first_uid, last_uid)) {
1970                                                 EM_DEBUG_EXCEPTION("emcore_append_subset_string_to_uid_range failed");
1971                                                 goto FINISH_OFF;
1972                                         }
1973                                         /* To Start formation of new subset string break out of inner loop */
1974                                         break;                                  
1975                                 }
1976                                 
1977                         } while (i < id_set_count);             
1978
1979                         /* Flow comes here in two cases :
1980                         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
1981                         2. due to break statement */
1982
1983                         if (last_uid == current_uid) {
1984                                 /* Case 1 */
1985                                 
1986                                 memset(subset_string, 0x00, max_subset_string_size);
1987                                 SNPRINTF(subset_string, max_subset_string_size, "%lu:%lu", first_uid, last_uid);
1988                         
1989                                 if (false == emcore_append_subset_string_to_uid_range(subset_string, &current_node, uid_range_set, range_len, first_uid, last_uid)) {
1990                                         EM_DEBUG_EXCEPTION("emcore_append_subset_string_to_uid_range failed");
1991                                         goto FINISH_OFF;
1992                                 }
1993                         }
1994                         else {
1995                                 /* Case 2: Do Nothing */
1996                         }
1997                                 
1998                 }               
1999         } while (i < id_set_count);
2000
2001         ret = true;
2002         
2003 FINISH_OFF:
2004         if (NULL != err_code)
2005                 *err_code = error;
2006         EM_DEBUG_FUNC_END();
2007         return ret;
2008         
2009 }  
2010
2011 /**
2012  * @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)
2013  * 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 
2014  * and stores the subset_string in its uid_range. Also sets the lowest and highest uids for the corresponsing uid_range
2015  * 
2016  * @author                                      h.gahlaut@samsung.com
2017  * @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.
2018  * @param[in] range_len         Specifies the maximum length of range string allowed. 
2019  * @param[in] luid                      Specifies the lowest uid in subset string
2020  * @param[in] huid                      Specifies the highest uid in subset string
2021  * @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
2022  * @param[out] err_code         Returns the error code.
2023  * @remarks                                                                             
2024  * @return This function returns true on success or false on failure.
2025  */
2026  
2027 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)
2028 {
2029         EM_DEBUG_FUNC_BEGIN();
2030         email_uid_range_set *current_node = NULL;
2031
2032         if (NULL == (*uid_range_set)) {
2033                 /*This happens only once when list  creation starts. Head Node is allocated */
2034                 current_node = (email_uid_range_set *)em_malloc(sizeof(email_uid_range_set));
2035                 if (NULL == current_node) {
2036                         EM_DEBUG_EXCEPTION("em_malloc failed");
2037                         return false;
2038                 }       
2039
2040                 current_node->uid_range = (char *)em_malloc(range_len);
2041                         
2042                 if (NULL == current_node->uid_range) {
2043                         EM_DEBUG_EXCEPTION("em_malloc failed");
2044                         EM_SAFE_FREE(current_node);
2045                         return false;
2046                 }
2047
2048                 SNPRINTF(current_node->uid_range, range_len, "%s,", subset_string);
2049
2050                 current_node->lowest_uid = luid;
2051                 current_node->highest_uid = huid;
2052                 (*uid_range_set) = current_node;
2053
2054                 (*current_node_adr) = current_node; 
2055                 
2056         }
2057         else {
2058                 /* Apart from first call to this function flow will always come here */
2059                 current_node = (*current_node_adr);
2060                 int len_sub_string = strlen(subset_string);
2061                 int space_left_in_buffer = range_len - strlen(current_node->uid_range);
2062
2063                 if ((len_sub_string + 1 + 1) <= space_left_in_buffer)   /* 1 for comma + 1 for ending null character */ {
2064                         SNPRINTF(current_node->uid_range + strlen(current_node->uid_range), space_left_in_buffer, "%s,", subset_string);
2065                         current_node->highest_uid = huid;
2066                 }
2067                 else {
2068                         /* No more space left in uid_range string.If continued on it, it will exceeded max size of range_len */
2069                         /* Allocate new node in Uid Range set */
2070                         email_uid_range_set *new_node = NULL;
2071
2072                         new_node = (email_uid_range_set *)em_malloc(sizeof(email_uid_range_set));
2073
2074                         if (NULL == new_node) {
2075                                 EM_DEBUG_EXCEPTION("em_malloc failed");
2076                                 return false;
2077                         }
2078
2079                         /* Allocate uid_range of new node */
2080                 
2081                         new_node->uid_range =  (char *)em_malloc(range_len);
2082
2083                         if (NULL == new_node->uid_range) {
2084                                 EM_DEBUG_EXCEPTION("em_malloc failed");
2085                                 EM_SAFE_FREE(new_node);
2086                                 return false;
2087                         }
2088
2089                         SNPRINTF(new_node->uid_range, range_len, "%s, ", subset_string);
2090
2091                         new_node->lowest_uid = luid;
2092                         new_node->highest_uid = huid;
2093
2094                         current_node->next = new_node;
2095
2096                         (*current_node_adr) = new_node; 
2097                 }
2098         }
2099         EM_DEBUG_FUNC_END();
2100         return true;
2101 }
2102
2103 /**
2104  * void emcore_free_uid_range_set(email_uid_range_set **uid_range_head)
2105  * Frees the linked list of uid ranges 
2106  *
2107  * @author                                      h.gahlaut@samsung.com
2108  * @param[in] uid_range_head    Head pointer of linked list of uid ranges               
2109  * @remarks                                                                     
2110  * @return This function does not return anything.
2111  */
2112  
2113 INTERNAL_FUNC
2114 void emcore_free_uid_range_set(email_uid_range_set **uid_range_set)
2115 {
2116         EM_DEBUG_FUNC_BEGIN();
2117
2118         email_uid_range_set *current_node = NULL;
2119         email_uid_range_set *uid_range_head = NULL;
2120         
2121         current_node = uid_range_head = (*uid_range_set);       /* Make the current node and head ptr point to starting of  uid_range_set */
2122
2123         while (current_node) {          
2124                 uid_range_head = current_node->next;            /* Move the head ptr to next node*/
2125
2126                 EM_SAFE_FREE(current_node->uid_range);
2127                 EM_SAFE_FREE(current_node);                             /* Free the current node */
2128
2129                 current_node = uid_range_head;                  /* Make the current node point to head ptr */
2130         }
2131
2132         (*uid_range_set) = NULL;
2133         EM_DEBUG_FUNC_END();
2134 }
2135
2136
2137 /**
2138  * @fn emcore_form_comma_separated_strings(int numbers[], int num_count, int max_string_len, char *** strings, int *string_count, int *err_code)
2139  * Forms comma separated strings of a give max_string_len from an array of numbers 
2140  * 
2141  * @author                                      h.gahlaut@samsung.com
2142  * @param[in] numbers                   Specifies the array of numbers to be converted into comma separated strings.
2143  * @param[in] num_count         Specifies the count of numbers in numbers array. 
2144  * @param[in] max_string_len    Specifies the maximum length of comma separated strings that are to be formed.
2145  * @param[out] strings                  Returns the base address of a double dimension array which stores the strings.
2146  * @param[out] string_count             Returns the number of strings formed.
2147  * @param[out] err_code         Returns the error code.
2148  * @remarks                                     If Input to the function is five numbers like 2755 2754 2748 2749 2750 and a given max_string_len is 20.
2149  *                                                      Then this function will form two comma separated strings as follows -
2150  *                                                      "2755, 2754, 2748"
2151  *                                                      "2749, 2750"
2152  * @return This function returns true on success or false on failure.
2153  */
2154
2155 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)
2156 {
2157         EM_DEBUG_FUNC_BEGIN();
2158
2159         int error = EMAIL_ERROR_NONE;
2160         int ret = false;
2161
2162         char **string_list = NULL;
2163         int num_of_strings = 0;
2164         int i = 0;
2165         int j =0;
2166         char num[MAX_INTEGER_LENGTH + 1] = {0, };
2167         int num_len = 0;
2168         int space_in_buffer = 0;
2169         int len_of_string_formed = 0;
2170
2171         if (NULL == numbers || num_count <= 0 || \
2172                 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.  */ {
2173                 EM_DEBUG_EXCEPTION("Invalid Parameter numbers[%p] num_count [%d] max_string_len [%d] strings [%p] string_count[%p]", \
2174                         numbers, num_count, max_string_len, strings, string_count);
2175                 error = EMAIL_ERROR_INVALID_PARAM;
2176                 goto FINISH_OFF;
2177         }
2178
2179         EM_DEBUG_LOG("num_count [%d] max_string_len [%d]", num_count, max_string_len);
2180
2181         string_list = em_malloc(sizeof(char *));
2182
2183         if (NULL == string_list) {
2184                 EM_DEBUG_EXCEPTION("em_malloc failed ");
2185                 goto FINISH_OFF;
2186         }
2187         
2188         string_list[num_of_strings] = em_malloc(max_string_len);
2189         
2190         if (NULL == string_list[num_of_strings]) {
2191                 EM_DEBUG_EXCEPTION("em_malloc failed ");
2192                 goto FINISH_OFF;
2193         }
2194         
2195         ++num_of_strings;
2196         space_in_buffer = max_string_len;
2197         
2198         for (j = 0; j < num_count;++j) {
2199                 memset(num, 0x00, MAX_INTEGER_LENGTH + 1);
2200                 SNPRINTF(num, MAX_INTEGER_LENGTH + 1, "%d", numbers[j]);
2201
2202                 num_len = strlen(num);
2203                 
2204                 len_of_string_formed = strlen(string_list[num_of_strings - 1]);
2205
2206                 space_in_buffer = max_string_len - len_of_string_formed ;
2207
2208                 if (space_in_buffer >= (num_len+1+1))                   /*  1 for comma and 1 for ending NULL */ {
2209                         SNPRINTF(string_list[num_of_strings - 1] + len_of_string_formed, max_string_len, "%d,", numbers[j]);
2210                 }
2211                 else {  /*  Removing comma at end of string  */
2212                         string_list[num_of_strings - 1][len_of_string_formed-1] = '\0';         
2213                         char **temp = NULL;
2214                         temp = (char **)realloc(string_list, sizeof(char *) * (num_of_strings + 1));    /*  Allocate new buffer to store a pointer to a new string */
2215
2216                         if (NULL == temp) {     
2217                                 EM_DEBUG_EXCEPTION("realloc failed");
2218                                 goto FINISH_OFF;
2219                         }
2220
2221                         memset(temp + num_of_strings, 0X00, sizeof(char *));
2222
2223                         string_list = temp;
2224                         temp = NULL;
2225                         string_list[num_of_strings] = em_malloc(max_string_len);/*  Allocate new buffer to store the string */
2226
2227                         if (NULL == string_list[num_of_strings]) {
2228                                 EM_DEBUG_EXCEPTION(" em_malloc failed ");
2229                                 goto FINISH_OFF;
2230                         }
2231                         ++num_of_strings;
2232                         SNPRINTF(string_list[num_of_strings - 1] , max_string_len, "%d,", numbers[j]);/*  Start making new string */
2233                 }
2234         }
2235
2236         /*  Removing comma at end of string  */
2237         len_of_string_formed = strlen(string_list[num_of_strings - 1]);
2238         string_list[num_of_strings - 1][len_of_string_formed-1] = '\0'; 
2239         ret = true;
2240
2241 FINISH_OFF:
2242
2243         if (false == ret)
2244                 emcore_free_comma_separated_strings(&string_list, &num_of_strings);
2245
2246         if (true == ret) {
2247                 for (i = 0; i < num_of_strings;++i)
2248                         EM_DEBUG_LOG("%s", string_list[i]);
2249                 *strings = string_list;
2250                 *string_count = num_of_strings;
2251         }
2252         
2253
2254         if (NULL != err_code)
2255                 *err_code = error;
2256
2257         EM_DEBUG_FUNC_END("ret [%d]", ret);
2258         return ret;
2259 }
2260 /**
2261  * @fn emcore_free_comma_separated_strings(char *** string_list, int *string_count)
2262  * Frees the double dimensional array of strings. 
2263  *
2264  * @author                                      h.gahlaut@samsung.com
2265  * @param[in] uid_range_head    Address of base address of double dimensional array of strings.
2266  * @param[in] string_count              Address of variable holding the count of strings.
2267  * @remarks                                                                     
2268  * @return This function does not return anything.
2269  */
2270 INTERNAL_FUNC void emcore_free_comma_separated_strings(char *** string_list, int *string_count)
2271 {
2272         EM_DEBUG_FUNC_BEGIN();
2273         int i = 0;
2274         char **str_list = NULL;
2275         int count = 0;
2276
2277         if (NULL != string_list) {
2278                 str_list = *string_list;
2279
2280                 if (0 != *string_count) {
2281                         count = *string_count;
2282                         for (i = 0; i < count; ++i)
2283                                 EM_SAFE_FREE(str_list[i]);
2284                 }
2285                 
2286                 EM_SAFE_FREE(str_list);
2287                 *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 */
2288                 *string_count = 0;
2289         }
2290         EM_DEBUG_FUNC_END();
2291 }
2292
2293
2294 #endif
2295
2296
2297
2298
2299 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)
2300 {
2301         EM_DEBUG_FUNC_BEGIN("source_file_name[%s], sub_type[%s], result_file_name_buffer_length[%d] ", source_file_name, sub_type, result_file_name_buffer_length);
2302         int ret = false, err = EMAIL_ERROR_NONE;
2303         char *extcheck = NULL;
2304         char attachment_file_name[MAX_PATH + 1] = { 0, };
2305
2306         if (!source_file_name || !result_file_name) {
2307                 EM_DEBUG_EXCEPTION("Invalid Parameter");
2308                 err  = EMAIL_ERROR_INVALID_PARAM;
2309                 goto FINISH_OFF;
2310         }
2311                                 
2312         strncpy(attachment_file_name, source_file_name, MAX_PATH);
2313         extcheck = strchr(attachment_file_name, '.');
2314
2315         if (extcheck)
2316                 EM_DEBUG_LOG("Extension Exist in the Attachment [%s] ", extcheck);
2317         else  { /* No extension attached, So add the Extension based on the subtype */
2318                 if (sub_type) {
2319                         strcat(attachment_file_name, ".");
2320                         strcat(attachment_file_name, sub_type);
2321                         EM_DEBUG_LOG("attachment_file_name with extension[%s] ", attachment_file_name);
2322                 }
2323                 else
2324                         EM_DEBUG_LOG("UnKnown Extesnsion");
2325
2326         }
2327         memset(result_file_name, 0 , result_file_name_buffer_length);
2328         EM_SAFE_STRNCPY(result_file_name, attachment_file_name, result_file_name_buffer_length - 1);
2329         EM_DEBUG_LOG("*result_file_name[%s]", result_file_name);
2330         ret = true;
2331
2332 FINISH_OFF:
2333         if (err_code)
2334                 *err_code = err;
2335         EM_DEBUG_FUNC_END();
2336         return ret;
2337 }
2338
2339 #ifdef __FEATURE_LOCAL_ACTIVITY__
2340 INTERNAL_FUNC int emcore_add_activity(emstorage_activity_tbl_t *new_activity, int *err_code)
2341 {
2342         EM_DEBUG_FUNC_BEGIN();
2343         
2344         EM_DEBUG_LOG("\t new_activity[%p], err_code[%p]", new_activity, err_code);
2345
2346         /*  default variable */
2347         int ret = false;
2348         int err = EMAIL_ERROR_NONE;
2349         
2350         if (!new_activity) {
2351                 EM_DEBUG_LOG("\t new_activity[%p]\n", new_activity);
2352                 err = EMAIL_ERROR_INVALID_PARAM;
2353                 goto FINISH_OFF;
2354         }
2355         if (!emstorage_add_activity(new_activity, false, &err)) {
2356                 EM_DEBUG_LOG("\t emstorage_add_activity falied - %d\n", err);
2357                 
2358                 goto FINISH_OFF;
2359         }
2360         ret = true;
2361         
2362 FINISH_OFF:
2363         if (err_code)
2364                 *err_code = err;
2365         
2366         return ret;
2367 }
2368
2369 INTERNAL_FUNC int emcore_delete_activity(emstorage_activity_tbl_t *activity, int *err_code)
2370 {
2371         EM_DEBUG_FUNC_BEGIN();
2372         
2373         EM_DEBUG_LOG("\t new_activity[%p], err_code[%p]", activity, err_code);
2374         
2375         /*  default variable */
2376         int ret = false;
2377         int err = EMAIL_ERROR_NONE;
2378         if (!activity) {
2379                 EM_DEBUG_LOG("\t new_activity[%p]\n", activity);
2380                 
2381                 err = EMAIL_ERROR_INVALID_PARAM;
2382                 goto FINISH_OFF;
2383         }
2384         if (!emstorage_delete_local_activity(activity, true, &err)) {
2385                 EM_DEBUG_LOG("\t emstorage_delete_local_activity falied - %d\n", err);
2386                 
2387                 goto FINISH_OFF;
2388         }
2389         ret = true;
2390         
2391 FINISH_OFF:
2392         if (err_code)
2393                 *err_code = err;
2394         
2395         return ret;
2396 }
2397
2398 INTERNAL_FUNC int emcore_get_next_activity_id(int *activity_id, int *err_code)
2399 {
2400         EM_DEBUG_FUNC_BEGIN();
2401         
2402         int ret = false;
2403         int err = EMAIL_ERROR_NONE;
2404
2405         if (NULL == activity_id)
2406         {
2407                 EM_DEBUG_EXCEPTION("\t activity_id[%p]", activity_id);
2408                 
2409                 err = EMAIL_ERROR_INVALID_PARAM;
2410                 goto FINISH_OFF;
2411         }
2412         
2413         if (false == emstorage_get_next_activity_id(activity_id, &err)) {
2414                 EM_DEBUG_LOG("\t emstorage_get_next_activity_id failed - %d\n", err);
2415                 goto FINISH_OFF;
2416         }
2417         
2418         ret = true;
2419         
2420         FINISH_OFF:
2421         if (NULL != err_code) {
2422                 *err_code = err;
2423         }
2424         
2425         return ret;
2426
2427 }
2428
2429 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
2430
2431
2432 INTERNAL_FUNC void emcore_free_rule(email_rule_t* rule)
2433 {
2434         EM_DEBUG_FUNC_BEGIN();
2435
2436         if (!rule)
2437                 return;
2438
2439         EM_SAFE_FREE(rule->value);
2440
2441         EM_DEBUG_FUNC_END();
2442 }
2443
2444
2445 /* EOF */