4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 /******************************************************************************
24 * File: email-core-utils.c
30 * 2006.08.16 : created
31 *****************************************************************************/
40 #include <glib-object.h>
49 #include <contacts-svc.h>
50 #include <notification.h>
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"
66 #define LED_TIMEOUT_SECS 12
67 #define G_DISPLAY_LENGTH 256
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"
82 static char _g_display[G_DISPLAY_LENGTH];
85 typedef struct _em_transaction_info_type_t {
88 struct _em_transaction_info_type_t *next;
90 } em_transaction_info_type_t;
92 em_transaction_info_type_t *g_transaction_info_list;
94 static email_option_t g_mail_option =
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 */
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},};
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;
115 static emcore_account_list_t **g_account_reference = NULL;
117 INTERNAL_FUNC char *emcore_convert_mutf7_to_utf8(char *mailbox_name)
119 EM_DEBUG_FUNC_BEGIN();
120 return (char *)(utf8_from_mutf7((unsigned char *)mailbox_name));
123 INTERNAL_FUNC int emcore_set_account_reference(emcore_account_list_t **account_list, int account_num, int *err_code)
125 g_account_reference = (emcore_account_list_t **)account_list;
129 email_option_t *emcore_get_option(int *err_code)
131 if (err_code != NULL)
132 *err_code = EMAIL_ERROR_NONE;
134 return &g_mail_option;
137 INTERNAL_FUNC int emcore_set_option(email_option_t *opt, int *err_code)
139 EM_DEBUG_FUNC_BEGIN("opt[%p], err_code[%p]", opt, err_code);
141 int err = EMAIL_ERROR_NONE;
144 EM_DEBUG_EXCEPTION("opt[%p]", opt);
146 if (err_code != NULL)
147 *err_code = EMAIL_ERROR_INVALID_PARAM;
151 memset(_g_display, 0, G_DISPLAY_LENGTH);
152 memcpy(&g_mail_option, opt, sizeof(g_mail_option));
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;
159 g_mail_option.display_name_from = NULL;
161 if (err_code != NULL)
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)
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);
178 int error = EMAIL_ERROR_NONE;
181 size_t long_enc_path_len = 0;
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);
187 if (!account->incoming_server_address) {
188 EM_DEBUG_EXCEPTION("account->incoming_server_address is null");
189 error = EMAIL_ERROR_INVALID_ACCOUNT;
193 long_enc_path_len = strlen(account->incoming_server_address) + (path ? strlen(path) : 0) + 64;
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;
204 /* ex:"{mai.test.com:143/imap} or {mai.test.com:143/imap/tls}my-mailbox" */
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);
211 if (account->incoming_server_secure_connection & 0x01) {
212 strncat(p, "/ssl", long_enc_path_len-(strlen(p)+1));
213 /* strcat(p, "/tryssl"); */
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));
220 strncat(p, "/notls", long_enc_path_len-(strlen(p)+1));
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);
230 long_enc_path_len = strlen(account->outgoing_server_address) + 64;
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");
236 error = EMAIL_ERROR_OUT_OF_MEMORY;
242 /* ex:"mail.test.com:25/smtp" */
244 SNPRINTF(p, long_enc_path_len, "%s:%d/%s",
245 account->outgoing_server_address,
246 account->outgoing_server_port_number,
249 if (account->outgoing_server_need_authentication) {
250 SNPRINTF(p + strlen(p), long_enc_path_len-(strlen(p)), "/user=%d", account->account_id);
253 if (account->outgoing_server_secure_connection & 0x01) {
254 strncat(p, "/ssl", long_enc_path_len-(strlen(p)+1));
255 /* strcat(p, "/tryssl"); */
257 if (account->outgoing_server_secure_connection & 0x02)
258 strncat(p, "/tls", long_enc_path_len-(strlen(p)+1));
260 strncat(p, "/notls", long_enc_path_len-(strlen(p)+1));
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));
267 char *enc_name = NULL;
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;
276 strncat(p, enc_name, long_enc_path_len-(strlen(p)+1));
277 EM_SAFE_FREE(enc_name);
288 if (err_code != NULL)
290 EM_PROFILE_END(emCorelongEncodedpath);
294 int emcore_get_long_encoded_path(int account_id, char *path, int delimiter, char **long_enc_path, int *err_code)
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);
300 int error = EMAIL_ERROR_NONE;
302 email_account_t *ref_account = emcore_get_account_reference(account_id);
304 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
305 error = EMAIL_ERROR_INVALID_ACCOUNT;
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);
317 if (err_code != NULL)
319 EM_PROFILE_END(emCorelongEncodedpath);
323 int emcore_get_encoded_mailbox_name(char *name, char **enc_name, int *err_code)
325 EM_DEBUG_FUNC_BEGIN("name[%s], enc_name[%p], err_code[%p]", name, enc_name, err_code);
327 if (!name || !enc_name) {
328 if (err_code != NULL)
329 *err_code = EMAIL_ERROR_INVALID_PARAM;
334 /* encoding mailbox name (Charset->UTF8->UTF7) */
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;
345 strcpy(*enc_name, name);
347 if (err_code != NULL)
348 *err_code = EMAIL_ERROR_NONE;
354 int emcore_get_temp_file_name(char **filename, int *err_code)
356 EM_DEBUG_FUNC_BEGIN("filename[%p], err_code[%p]", filename, err_code);
359 int error = EMAIL_ERROR_NONE;
361 if (filename == NULL) {
362 EM_DEBUG_EXCEPTION("\t filename[%p]\n", filename);
363 error = EMAIL_ERROR_INVALID_PARAM;
367 char tempname[512] = {0x00, };
371 gettimeofday(&tv, NULL);
374 /* Create Directory If deleted by user*/
375 emstorage_create_dir_if_delete();
377 SNPRINTF(tempname, sizeof(tempname), "%s%c%s%c%d", MAILHOME, DIR_SEPERATOR_CH, MAILTEMP, DIR_SEPERATOR_CH, rand());
379 char *p = EM_SAFE_STRDUP(tempname);
381 EM_DEBUG_EXCEPTION("\t strdup failed...\n");
382 error = EMAIL_ERROR_OUT_OF_MEMORY;
391 if (err_code != NULL)
397 int emcore_get_file_name(char *path, char **filename, int *err_code)
399 EM_DEBUG_FUNC_BEGIN("path[%s], filename[%p], err_code[%p]", path, filename, err_code);
402 int error = EMAIL_ERROR_NONE;
404 if (!path || !filename) {
405 EM_DEBUG_EXCEPTION("path[%p], filename[%p]", path, filename);
407 error = EMAIL_ERROR_INVALID_PARAM;
411 int i = (int)strlen(path);
415 if (path[i] == DIR_SEPERATOR_CH)
418 *filename = path + i + 1;
423 if (err_code != NULL)
429 int emcore_get_file_size(char *path, int *size, int *err_code)
431 EM_DEBUG_FUNC_BEGIN("path[%s], size[%p], err_code[%p]", path, size, err_code);
434 int error = EMAIL_ERROR_NONE;
436 if ((path == NULL) || (size == NULL)) {
437 EM_DEBUG_EXCEPTION("\t path[%p], size[%p]\n", path, size);
439 error = EMAIL_ERROR_INVALID_PARAM;
445 if (stat(path, &st_buf) < 0) {
446 EM_DEBUG_EXCEPTION("\t stat failed - %s\n", path);
448 error = EMAIL_ERROR_SYSTEM_FAILURE;
452 *size = st_buf.st_size;
457 if (err_code != NULL)
465 static int _emcore_check_host(char *host)
469 return strncmp(host, ".SYNTAX-ERROR.", strlen(".SYNTAX-ERROR."));
474 int emcore_get_address_count(char *addr_str, int *count, int *err_code)
476 EM_DEBUG_FUNC_BEGIN("addr_str[%s], count[%p], err_code[%p]", addr_str, count, err_code);
479 int error = EMAIL_ERROR_NONE;
481 ADDRESS *addr = NULL;
482 ADDRESS *p_addr = NULL;
488 EM_DEBUG_EXCEPTION("addr_str[%s], count[%p]", addr_str, count);
489 error = EMAIL_ERROR_INVALID_PARAM;
493 if (addr_str != NULL) {
494 em_skip_whitespace(addr_str, &p);
495 EM_DEBUG_LOG("em_skip_whitespace[p][%s]", p);
498 for (i = 0, j = strlen(p); i < j; i++)
499 if (p[i] == ';') p[i] = ',';
500 rfc822_parse_adrlist(&addr, p, NULL);
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 ");
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);
514 error = EMAIL_ERROR_INVALID_ADDRESS;
515 /* goto FINISH_OFF; */
521 if (error != EMAIL_ERROR_INVALID_ADDRESS)
526 mail_free_address(&addr);
528 if (err_code != NULL)
534 INTERNAL_FUNC int emcore_set_network_error(int err_code)
536 email_session_t *session = NULL;
538 EM_DEBUG_FUNC_BEGIN();
540 emcore_get_current_session(&session);
545 session->network = err_code;
550 int emcore_get_empty_session(email_session_t **session)
552 EM_DEBUG_FUNC_BEGIN("session[%p]", session);
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;
570 *session = (i != SESSION_MAX) ? &g_session_list[i] : NULL;
572 return (i != SESSION_MAX) ? true : false;
575 int emcore_clear_session(email_session_t *session)
577 EM_DEBUG_FUNC_BEGIN();
580 memset(session, 0x00, sizeof(email_session_t));
585 int emcore_get_current_session(email_session_t **session)
587 EM_DEBUG_FUNC_BEGIN("session[%p]", session);
591 for (i = 0; i < SESSION_MAX; i++) {
592 if (g_session_list[i].tid == GPOINTER_TO_INT(THREAD_SELF())) {
594 *session = g_session_list + i;
601 *session = (i != SESSION_MAX) ? g_session_list + i : NULL;
603 return (i != SESSION_MAX) ? true : false;
606 int emcore_check_unread_mail()
608 EM_DEBUG_FUNC_BEGIN();
611 int err = EMAIL_ERROR_NONE;
612 int total_unread_count = 0;
613 int total_mail_count = 0;
614 email_mailbox_t mailbox;
616 memset(&mailbox, 0x00, sizeof(email_mailbox_t));
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;
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);
627 EM_DEBUG_LOG("total_unread_count [%d]", total_unread_count);
629 /* temporarily disable : set unread count to badge */
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;
644 static int emcore_add_notification_for_user_message(int account_id, int mail_id, char *title, char *content, time_t log_time)
646 EM_DEBUG_FUNC_BEGIN();
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;
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);
658 noti = notification_new(NOTIFICATION_TYPE_NOTI, account_id, mail_id);
661 EM_DEBUG_EXCEPTION("notification_new failed");
665 if( (noti_err = notification_set_time(noti, log_time)) != NOTIFICATION_ERROR_NONE) {
666 EM_DEBUG_EXCEPTION("notification_set_time failed [%d]", noti_err);
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);
675 if( (noti_err = notification_set_title(noti, title, NULL)) != NOTIFICATION_ERROR_NONE) {
676 EM_DEBUG_EXCEPTION("notification_set_title failed [%d]", noti_err);
680 if( (noti_err = notification_set_content(noti, content, NULL)) != NOTIFICATION_ERROR_NONE) {
681 EM_DEBUG_EXCEPTION("notification_set_content failed [%d]", noti_err);
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);
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);
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);
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);
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);
710 if( (noti_err = notification_insert(noti, NULL)) != NOTIFICATION_ERROR_NONE) {
711 EM_DEBUG_EXCEPTION("notification_insert failed [%d]", noti_err);
716 if( (noti_err = notification_free(noti)) != NOTIFICATION_ERROR_NONE) {
717 EM_DEBUG_EXCEPTION("notification_free failed [%d]", noti_err);
720 if (noti_err != NOTIFICATION_ERROR_NONE)
723 EM_DEBUG_FUNC_END("ret [%d]", ret);
727 INTERNAL_FUNC int emcore_show_user_message(int id, email_action_t action, int error)
729 EM_DEBUG_FUNC_BEGIN("id[%d], action[%d], error[%d]", id, action, error);
733 struct tm *log_time_tm;
736 log_time_tm = localtime(&log_time);
737 log_time = mktime(log_time_tm);
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);
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;
745 if (error == 0) /* error 0 means 'this is not error' */
749 EM_DEBUG_LOG("Invalid mail_id");
753 if (!emstorage_get_mail_by_id(id, &mail_table_data, true, NULL)) {
754 EM_DEBUG_LOG("Mail not found");
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");
763 if (!emstorage_free_mail(&mail_table_data, 1, NULL))
764 EM_DEBUG_EXCEPTION("emstorage_free_mail Failed");
768 EM_DEBUG_FUNC_END("ret [%d]", ret);
773 /* storage space handling - 210709 */
774 int emcore_get_storage_status(void)
776 EM_DEBUG_FUNC_BEGIN();
777 int storage_status = 0, nError = 0;
781 #ifdef STORAGE_STATUS
782 nError = vconf_get_int(PS_KEY_SYSTEM_STORAGE_MOVI_STATUS,
784 #endif /* STORAGE_STATUS */
787 EM_DEBUG_EXCEPTION("vconf_get_int Failed");
791 return storage_status;
794 int emcore_is_storage_full(int *err_code)
796 EM_DEBUG_FUNC_BEGIN();
799 int err = EMAIL_ERROR_NONE;
800 struct statfs buf = {0};
802 if (statfs(DATA_PATH, &buf) == -1) {
803 EM_DEBUG_EXCEPTION("statfs(\"%s\") failed - %d", DATA_PATH, errno);
804 err = EMAIL_ERROR_SYSTEM_FAILURE;
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;
815 if (err == EMAIL_ERROR_MAIL_MEMORY_FULL)
819 if (err_code != NULL)
821 EM_DEBUG_FUNC_END("ret[%d]", ret);
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)
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);
830 int mail_size = 0; /* size of the plain text body and attachments */
831 int err = EMAIL_ERROR_NONE;
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;
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;
847 mail_size += st_buf.st_size;
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;
858 mail_size += st_buf.st_size;
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;
867 mail_size += st_buf.st_size;
870 *output_size = mail_size;
874 EM_DEBUG_FUNC_END("mail_size [%d]", mail_size);
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)
882 EM_DEBUG_FUNC_BEGIN();
883 EM_IF_NULL_RETURN_VALUE(mailbox_path, NULL);
886 gchar **token_list = NULL;
887 gchar *mailbox = NULL, *name = NULL;
888 char *converted_name;
891 mailbox = g_strdup(mailbox_path);
892 token_list = g_strsplit_set(mailbox, "/", -1);
897 while (token_list[index] != NULL)
900 name = g_strdup(token_list[index - 1]);
901 g_strfreev(token_list);
903 converted_name = emcore_convert_mutf7_to_utf8(name);
909 return converted_name;
913 static int emcore_get_first_address(const char *full_address, char **alias, char **address)
915 EM_DEBUG_FUNC_BEGIN();
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);
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;
931 s = (char *)strchr((char *)full_address, ';');
933 first_address = strdup(full_address); /* only one address */
935 first_address = strndup(full_address, s - full_address); /* over two addresses */
939 if ((alias_start = (char *)strchr((char *)first_address, '\"'))){
941 alias_cursor = alias_start;
942 while ((alias_cursor = (char *)strchr((char *)(alias_cursor), '\"'))){
943 alias_end = alias_cursor;
945 if (*alias_cursor == 0)
948 if (alias_end) { /* there is "alias" */
949 *alias = strndup(alias_start, alias_end - alias_start);
950 EM_DEBUG_LOG("alias [%s]", *alias);
956 if (alias_end == NULL)
960 if ((address_start = (char *)strchr((char *)s, '<'))){
962 if ((address_end = (char *)strchr((char *)address_start, '>')))
963 *address = strndup(address_start, address_end - address_start); /* (alias) <(addr)> ... */
965 *address = strdup(s);
968 *address = strdup(s); /* (addr) ; ... : no alias */
971 EM_SAFE_FREE(first_address);
976 void emcore_fill_address_information_of_mail_tbl(emstorage_mail_tbl_t *mail_data)
978 EM_DEBUG_FUNC_BEGIN("mail_data [%p]", mail_data);
980 char *first_alias = NULL;
981 char *first_address = NULL;
982 char *recipient = NULL;
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);
990 mail_data->alias_sender = first_alias;
993 mail_data->email_address_sender = first_address;
994 first_address = NULL;
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;
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);
1009 mail_data->alias_recipient = first_alias;
1011 mail_data->email_address_recipient = first_address;
1013 EM_DEBUG_FUNC_END();
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)
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);
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;
1030 GError *glib_error = NULL;
1033 if (!output_preview_buffer) {
1034 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1035 err = EMAIL_ERROR_INVALID_PARAM;
1039 local_preview_buffer_length = input_preview_buffer_length * 2;
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);
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;
1054 if (!(fp = fopen(input_html_path, "r"))) {
1055 EM_DEBUG_EXCEPTION("fopen failed [%s]", input_html_path);
1056 err = EMAIL_ERROR_SYSTEM_FAILURE;
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;
1066 byte_read = fread(local_preview_text, sizeof(char), st_buf.st_size, fp);
1069 EM_DEBUG_EXCEPTION("fread failed [%s]", input_plain_path);
1070 err = EMAIL_ERROR_SYSTEM_FAILURE;
1074 if ( (err = emcore_strip_HTML(local_preview_text)) != EMAIL_ERROR_NONE) {
1075 EM_DEBUG_EXCEPTION("emcore_strip failed");
1079 result_strlen = EM_SAFE_STRLEN(local_preview_text);
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);
1089 if (!(fp = fopen(input_plain_path, "r"))) {
1090 EM_DEBUG_EXCEPTION("fopen failed [%s]", input_plain_path);
1091 err = EMAIL_ERROR_SYSTEM_FAILURE;
1095 if (!(local_preview_text = (char*)em_malloc(sizeof(char) * local_preview_buffer_length))) {
1096 EM_DEBUG_EXCEPTION("em_malloc failed");
1100 byte_read = fread(local_preview_text, sizeof(char), local_preview_buffer_length - 1, fp);
1103 EM_DEBUG_EXCEPTION("fread failed [%s]", input_plain_path);
1104 err = EMAIL_ERROR_SYSTEM_FAILURE;
1108 reg_replace(local_preview_text, CR_STRING, "");
1109 reg_replace(local_preview_text, LF_STRING, "");
1110 reg_replace(local_preview_text, TAB_STRING, "");
1112 result_strlen = EM_SAFE_STRLEN(local_preview_text);
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);
1121 if(utf8_encoded_string) {
1122 EM_SAFE_FREE(local_preview_text);
1123 local_preview_text = utf8_encoded_string;
1126 EM_DEBUG_EXCEPTION("g_convert failed");
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;
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); */
1141 EM_SAFE_FREE(local_preview_text);
1142 EM_SAFE_FREE(encoding_type);
1147 EM_DEBUG_FUNC_END("err [%d]", err);
1151 INTERNAL_FUNC int emcore_add_transaction_info(int mail_id, int handle , int *err_code)
1153 EM_DEBUG_FUNC_BEGIN("mail_id[%d], handle[%d]", mail_id, handle);
1156 int err = EMAIL_ERROR_NONE ;
1157 em_transaction_info_type_t *pTransinfo = NULL ;
1158 em_transaction_info_type_t *pTemp = NULL;
1160 EM_DEBUG_LOG("g_transaction_info_list[%p]", g_transaction_info_list);
1161 pTransinfo = g_transaction_info_list ;
1163 if (!(pTemp = em_malloc(sizeof(em_transaction_info_type_t)))) {
1164 EM_DEBUG_EXCEPTION("malloc failed...");
1165 err = EMAIL_ERROR_OUT_OF_MEMORY;
1168 pTemp->mail_id = mail_id ;
1169 pTemp->handle = handle;
1172 pTransinfo = pTemp ;
1173 g_transaction_info_list = pTransinfo ;
1176 while (pTransinfo->next)
1177 pTransinfo = pTransinfo->next;
1178 pTransinfo->next = pTemp;
1186 EM_DEBUG_FUNC_END("g_transaction_info_list[%p]", g_transaction_info_list);
1190 INTERNAL_FUNC int emcore_get_handle_by_mailId_from_transaction_info(int mail_id, int *pHandle)
1192 EM_DEBUG_FUNC_BEGIN("mail_id[%d], handle[%p]", mail_id, pHandle);
1195 em_transaction_info_type_t *pTransinfo = NULL ;
1197 if (g_transaction_info_list == NULL) {
1198 EM_DEBUG_EXCEPTION("g_transaction_info_list NULL");
1201 pTransinfo = g_transaction_info_list;
1204 EM_DEBUG_LOG("pTransinfo->mail_id[%d]", pTransinfo->mail_id);
1205 if (pTransinfo->mail_id == mail_id) {
1206 *pHandle = pTransinfo->handle;
1208 EM_DEBUG_LOG("*pHandle[%d]", *pHandle);
1212 pTransinfo = pTransinfo->next ;
1213 }while (pTransinfo);
1214 EM_DEBUG_FUNC_END();
1218 INTERNAL_FUNC int emcore_delete_transaction_info_by_mailId(int mail_id )
1220 EM_DEBUG_FUNC_BEGIN("mail_id[%d]", mail_id);
1222 em_transaction_info_type_t *pTransinfo ;
1223 em_transaction_info_type_t *pTemp = NULL;
1225 if (g_transaction_info_list == NULL) {
1226 EM_DEBUG_EXCEPTION("g_transaction_info_list NULL");
1229 pTransinfo = g_transaction_info_list;
1231 EM_DEBUG_LOG("pTransinfo[%p]", pTransinfo);
1234 EM_DEBUG_LOG("pTransinfo->mail_id[%d]", pTransinfo->mail_id);
1235 if (pTransinfo->mail_id == mail_id) {
1236 pTemp = pTransinfo->next ;
1238 EM_SAFE_FREE(pTransinfo) ;
1239 g_transaction_info_list = NULL;
1242 pTransinfo->mail_id = pTransinfo->next->mail_id;
1243 pTransinfo->handle = pTransinfo->next->handle ;
1244 pTransinfo->next = pTransinfo->next->next;
1246 EM_SAFE_FREE(pTemp);
1251 pTransinfo = pTransinfo->next ;
1254 }while (pTransinfo);
1255 EM_DEBUG_FUNC_END();
1262 int reg_replace (char *input_source_text, char *input_old_pattern_string, char *input_new_string)
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;
1267 int so, n, nmatch, source_text_length, n_count = 1;
1268 regmatch_t *pmatch = NULL;
1269 regex_t reg_pattern;
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;
1277 source_text_length = strlen(input_source_text);
1279 regcomp(®_pattern, input_old_pattern_string, REG_ICASE);
1281 nmatch = reg_pattern.re_nsub + 1;
1283 EM_DEBUG_LOG("nmatch [%d]", nmatch);
1286 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_DATA");
1287 error_code = EMAIL_ERROR_INVALID_DATA;
1291 pmatch = (regmatch_t*)em_malloc(sizeof(regmatch_t) * nmatch);
1293 if(pmatch == NULL) {
1294 EM_DEBUG_EXCEPTION("EMAIL_ERROR_OUT_OF_MEMORY");
1295 error_code = EMAIL_ERROR_OUT_OF_MEMORY;
1299 for (pos = input_new_string; *pos ; pos++) {
1300 if (*pos == '\\' && *(pos + 1) > '0' && *(pos + 1) <= '9') {
1302 so = pmatch[*(pos + 1) - 48].rm_so;
1303 n = pmatch[*(pos + 1) - 48].rm_eo - so;
1305 EM_DEBUG_LOG("so [%d], n [%d]", so, n);
1307 if (so < 0 || strlen (input_new_string) + n - 1 > source_text_length)
1310 memmove (pos + n, pos + 2, strlen (pos) - 1);
1311 memmove (pos, input_source_text + so, n);
1316 for (pos = input_source_text; !regexec (®_pattern, pos, 1, pmatch, 0);) {
1317 n = pmatch[0].rm_eo - pmatch[0].rm_so;
1318 pos += pmatch[0].rm_so;
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);
1328 EM_SAFE_FREE(pmatch);
1329 regfree (®_pattern);
1331 EM_DEBUG_FUNC_END("error_code [%d]", error_code);
1336 int emcore_strip_HTML(char *source_string)
1338 EM_DEBUG_FUNC_BEGIN("source_string [%p]", source_string);
1340 int result = EMAIL_ERROR_NONE;
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>)", "" */
1349 reg_replace(source_string, "<*/p>", " ");
1350 reg_replace(source_string, "<br>", " ");
1353 reg_replace(source_string, "<[^>]*>", "");
1357 /* reg_replace(source_string, "•", " * "); */
1359 /* "‹", "< */
1360 /* reg_replace(source_string, "‹", "<"); */
1362 /* "›", "> */
1363 /* reg_replace(source_string, "›", ">"); */
1365 /* "™", "(tm) */
1366 /* reg_replace(source_string, "™", "(tm)"); */
1369 /* reg_replace(source_string, "⁄", "/"); */
1372 reg_replace(source_string, "<", "<");
1375 reg_replace(source_string, ">", ">");
1377 /* "©", "(c) */
1378 /* reg_replace(source_string, "©", "(c)"); */
1381 reg_replace(source_string, """, "\'");
1384 reg_replace(source_string, " ", " ");
1386 reg_replace(source_string, " ", " ");
1388 EM_DEBUG_FUNC_END();
1393 #define MAX_NOTI_STRING_LENGTH 8096
1395 INTERNAL_FUNC int emcore_convert_structure_to_string(void *struct_var, char **encoded_string, email_convert_struct_type_e type)
1397 EM_DEBUG_FUNC_BEGIN("Struct type[%d]", type);
1400 char delimiter[] = {0x01, 0x00};
1401 int error_code = EMAIL_ERROR_NONE;
1403 buf = (char *) malloc(MAX_NOTI_STRING_LENGTH * sizeof(char));
1405 error_code = EMAIL_ERROR_OUT_OF_MEMORY;
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 ; */
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]
1460 *encoded_string = buf;
1461 EM_DEBUG_FUNC_END("Struct -> String:[%s]\n", buf);
1465 INTERNAL_FUNC int emcore_convert_string_to_structure(const char *encoded_string, void **struct_var, email_convert_struct_type_e type)
1467 EM_DEBUG_FUNC_BEGIN();
1470 void *temp_struct = NULL;
1472 char *current_pos = NULL;
1473 char *found_pos = NULL;
1474 char delimiter[] = {0x01, 0x00};
1475 int error_code = EMAIL_ERROR_NONE;
1477 EM_DEBUG_LOG("Struct Type[%d], String:[%s]", type, encoded_string);
1479 buff = (char *)EM_SAFE_STRDUP(encoded_string);
1481 error_code = EMAIL_ERROR_OUT_OF_MEMORY;
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));
1489 error_code = EMAIL_ERROR_OUT_OF_MEMORY;
1492 temp_struct = (void *)item;
1497 found_pos = strstr(current_pos, delimiter);
1498 if (NULL == found_pos) {
1499 error_code = EMAIL_ERROR_INVALID_DATA;
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;
1508 found_pos = strstr(current_pos, delimiter);
1509 if (NULL == found_pos) {
1510 error_code = EMAIL_ERROR_INVALID_DATA;
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;
1519 found_pos = strstr(current_pos, delimiter);
1520 if (NULL == found_pos) {
1521 error_code = EMAIL_ERROR_INVALID_DATA;
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;
1530 found_pos = strstr(current_pos, delimiter);
1531 if (NULL == found_pos) {
1532 error_code = EMAIL_ERROR_INVALID_DATA;
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;
1540 /* from_email_address */
1541 found_pos = strstr(current_pos, delimiter);
1542 if (NULL == found_pos) {
1543 error_code = EMAIL_ERROR_INVALID_DATA;
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;
1552 found_pos = strstr(current_pos, delimiter);
1553 if (NULL == found_pos) {
1554 error_code = EMAIL_ERROR_INVALID_DATA;
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;
1563 found_pos = strstr(current_pos, delimiter);
1564 if (NULL == found_pos) {
1565 error_code = EMAIL_ERROR_INVALID_DATA;
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;
1573 /* is_text_downloaded */
1574 found_pos = strstr(current_pos, delimiter);
1575 if (NULL == found_pos) {
1576 error_code = EMAIL_ERROR_INVALID_DATA;
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;
1585 found_pos = strstr(current_pos, delimiter);
1586 if (NULL == found_pos) {
1587 error_code = EMAIL_ERROR_INVALID_DATA;
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;
1595 /* flags_seen_field */
1596 found_pos = strstr(current_pos, delimiter);
1597 if (NULL == found_pos) {
1598 error_code = EMAIL_ERROR_INVALID_DATA;
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;
1607 found_pos = strstr(current_pos, delimiter);
1608 if (NULL == found_pos) {
1609 error_code = EMAIL_ERROR_INVALID_DATA;
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;
1618 found_pos = strstr(current_pos, delimiter);
1619 if (NULL == found_pos) {
1620 error_code = EMAIL_ERROR_INVALID_DATA;
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;
1629 found_pos = strstr(current_pos, delimiter);
1630 if (NULL == found_pos) {
1631 error_code = EMAIL_ERROR_INVALID_DATA;
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;
1639 /* is_report_mail */
1640 found_pos = strstr(current_pos, delimiter);
1641 if (NULL == found_pos) {
1642 error_code = EMAIL_ERROR_INVALID_DATA;
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;
1650 /* recipients_count */
1651 found_pos = strstr(current_pos, delimiter);
1652 if (NULL == found_pos) {
1653 error_code = EMAIL_ERROR_INVALID_DATA;
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;
1661 /* has_attachment */
1662 found_pos = strstr(current_pos, delimiter);
1663 if (NULL == found_pos) {
1664 error_code = EMAIL_ERROR_INVALID_DATA;
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;
1672 /* has_drm_attachment */
1673 found_pos = strstr(current_pos, delimiter);
1674 if (NULL == found_pos) {
1675 error_code = EMAIL_ERROR_INVALID_DATA;
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;
1683 /* previewBodyText */
1684 found_pos = strstr(current_pos, delimiter);
1685 if (NULL == found_pos) {
1686 error_code = EMAIL_ERROR_INVALID_DATA;
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;
1695 found_pos = strstr(current_pos, delimiter);
1696 if (NULL == found_pos) {
1697 error_code = EMAIL_ERROR_INVALID_DATA;
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;
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);
1713 EM_DEBUG_EXCEPTION("Unknown structure type");
1723 *struct_var = temp_struct;
1727 case EMAIL_CONVERT_STRUCT_TYPE_MAIL_LIST_ITEM:
1728 EM_SAFE_FREE(temp_struct);
1734 EM_DEBUG_FUNC_END();
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 */
1746 INTERNAL_FUNC int emcore_send_noti_for_new_mail(int account_id, char *mailbox_name, char *subject, char *from, char *uid, char *datetime)
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;
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);
1758 param_string = malloc(strlen(mailbox_name) + strlen(subject) + strlen(from) + strlen(uid) + strlen(datetime) + 5);
1760 if (param_string == NULL) {
1761 error_code = EMAIL_ERROR_OUT_OF_MEMORY;
1762 EM_DEBUG_EXCEPTION("Memory allocation for 'param_string' is failed");
1766 memset(param_string, 0x00, sizeof(param_string));
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);
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");
1779 EM_DEBUG_FUNC_END();
1785 #define MAX_TITLE_LENGTH 1024
1786 int emcore_update_notification_for_unread_mail(int account_id)
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;
1792 if((noti_err = notification_update(NULL)) != NOTIFICATION_ERROR_NONE) {
1793 EM_DEBUG_EXCEPTION("notification_update failed");
1799 EM_DEBUG_FUNC_END("return [%d]", error_code);
1803 INTERNAL_FUNC int emcore_finalize_sync(int account_id, int *error)
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;
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);
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);
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();
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);
1829 EM_DEBUG_FUNC_END();
1833 INTERNAL_FUNC int emcore_clear_all_notifications()
1835 int account_count = 0, i;
1836 emstorage_account_tbl_t *account_list;
1837 int error_code = EMAIL_ERROR_NONE;
1839 if(!emstorage_get_account_list(&account_count, &account_list, true, false, &error_code)) {
1840 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed");
1844 for(i = 0; i < account_count; i++) {
1845 emcore_delete_notification_by_account(account_list[i].account_id);
1850 emstorage_free_account(&account_list, account_count, NULL);
1853 EM_DEBUG_FUNC_END("return[%d]", error_code);
1857 INTERNAL_FUNC int emcore_add_notification_for_unread_mail(emstorage_mail_tbl_t *input_mail_tbl_data)
1859 EM_DEBUG_FUNC_BEGIN("input_mail_tbl_data[%p]", input_mail_tbl_data);
1861 int err = EMAIL_ERROR_NONE;
1863 if (input_mail_tbl_data == NULL) {
1864 EM_DEBUG_EXCEPTION("input_mail_tbl_data is NULL");
1865 return EMAIL_ERROR_INVALID_PARAM;
1868 EM_DEBUG_FUNC_END("err[%d]", err);
1873 INTERNAL_FUNC int emcore_delete_notification_for_read_mail(int mail_id)
1875 EM_DEBUG_FUNC_BEGIN();
1876 int error_code = EMAIL_ERROR_NONE;
1878 EM_DEBUG_FUNC_END();
1882 #define EAS_EXECUTABLE_PATH "/usr/bin/eas-engine"
1884 INTERNAL_FUNC int emcore_delete_notification_by_account(int account_id)
1886 EM_DEBUG_FUNC_BEGIN("account_id [%d]", account_id);
1887 int error_code = EMAIL_ERROR_NONE;
1888 EM_DEBUG_FUNC_END();
1892 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
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.
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.
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)
1911 EM_DEBUG_FUNC_BEGIN();
1914 int error = EMAIL_ERROR_NONE;
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;
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*/
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;
1936 EM_DEBUG_LOG("id set count[%d] range_len[%d]", id_set_count, range_len);
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);
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, ¤t_node, uid_range_set, range_len, first_uid, last_uid)) {
1949 EM_DEBUG_EXCEPTION("emcore_append_subset_string_to_uid_range failed");
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 */
1957 current_uid = id_set[i].server_mail_id;
1958 if (current_uid == (last_uid + 1)) {
1959 last_uid = current_uid;
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);
1969 if (false == emcore_append_subset_string_to_uid_range(subset_string, ¤t_node, uid_range_set, range_len, first_uid, last_uid)) {
1970 EM_DEBUG_EXCEPTION("emcore_append_subset_string_to_uid_range failed");
1973 /* To Start formation of new subset string break out of inner loop */
1977 } while (i < id_set_count);
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 */
1983 if (last_uid == current_uid) {
1986 memset(subset_string, 0x00, max_subset_string_size);
1987 SNPRINTF(subset_string, max_subset_string_size, "%lu:%lu", first_uid, last_uid);
1989 if (false == emcore_append_subset_string_to_uid_range(subset_string, ¤t_node, uid_range_set, range_len, first_uid, last_uid)) {
1990 EM_DEBUG_EXCEPTION("emcore_append_subset_string_to_uid_range failed");
1995 /* Case 2: Do Nothing */
1999 } while (i < id_set_count);
2004 if (NULL != err_code)
2006 EM_DEBUG_FUNC_END();
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
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.
2024 * @return This function returns true on success or false on failure.
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)
2029 EM_DEBUG_FUNC_BEGIN();
2030 email_uid_range_set *current_node = NULL;
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");
2040 current_node->uid_range = (char *)em_malloc(range_len);
2042 if (NULL == current_node->uid_range) {
2043 EM_DEBUG_EXCEPTION("em_malloc failed");
2044 EM_SAFE_FREE(current_node);
2048 SNPRINTF(current_node->uid_range, range_len, "%s,", subset_string);
2050 current_node->lowest_uid = luid;
2051 current_node->highest_uid = huid;
2052 (*uid_range_set) = current_node;
2054 (*current_node_adr) = current_node;
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);
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;
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;
2072 new_node = (email_uid_range_set *)em_malloc(sizeof(email_uid_range_set));
2074 if (NULL == new_node) {
2075 EM_DEBUG_EXCEPTION("em_malloc failed");
2079 /* Allocate uid_range of new node */
2081 new_node->uid_range = (char *)em_malloc(range_len);
2083 if (NULL == new_node->uid_range) {
2084 EM_DEBUG_EXCEPTION("em_malloc failed");
2085 EM_SAFE_FREE(new_node);
2089 SNPRINTF(new_node->uid_range, range_len, "%s, ", subset_string);
2091 new_node->lowest_uid = luid;
2092 new_node->highest_uid = huid;
2094 current_node->next = new_node;
2096 (*current_node_adr) = new_node;
2099 EM_DEBUG_FUNC_END();
2104 * void emcore_free_uid_range_set(email_uid_range_set **uid_range_head)
2105 * Frees the linked list of uid ranges
2107 * @author h.gahlaut@samsung.com
2108 * @param[in] uid_range_head Head pointer of linked list of uid ranges
2110 * @return This function does not return anything.
2114 void emcore_free_uid_range_set(email_uid_range_set **uid_range_set)
2116 EM_DEBUG_FUNC_BEGIN();
2118 email_uid_range_set *current_node = NULL;
2119 email_uid_range_set *uid_range_head = NULL;
2121 current_node = uid_range_head = (*uid_range_set); /* Make the current node and head ptr point to starting of uid_range_set */
2123 while (current_node) {
2124 uid_range_head = current_node->next; /* Move the head ptr to next node*/
2126 EM_SAFE_FREE(current_node->uid_range);
2127 EM_SAFE_FREE(current_node); /* Free the current node */
2129 current_node = uid_range_head; /* Make the current node point to head ptr */
2132 (*uid_range_set) = NULL;
2133 EM_DEBUG_FUNC_END();
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
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"
2152 * @return This function returns true on success or false on failure.
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)
2157 EM_DEBUG_FUNC_BEGIN();
2159 int error = EMAIL_ERROR_NONE;
2162 char **string_list = NULL;
2163 int num_of_strings = 0;
2166 char num[MAX_INTEGER_LENGTH + 1] = {0, };
2168 int space_in_buffer = 0;
2169 int len_of_string_formed = 0;
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;
2179 EM_DEBUG_LOG("num_count [%d] max_string_len [%d]", num_count, max_string_len);
2181 string_list = em_malloc(sizeof(char *));
2183 if (NULL == string_list) {
2184 EM_DEBUG_EXCEPTION("em_malloc failed ");
2188 string_list[num_of_strings] = em_malloc(max_string_len);
2190 if (NULL == string_list[num_of_strings]) {
2191 EM_DEBUG_EXCEPTION("em_malloc failed ");
2196 space_in_buffer = max_string_len;
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]);
2202 num_len = strlen(num);
2204 len_of_string_formed = strlen(string_list[num_of_strings - 1]);
2206 space_in_buffer = max_string_len - len_of_string_formed ;
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]);
2211 else { /* Removing comma at end of string */
2212 string_list[num_of_strings - 1][len_of_string_formed-1] = '\0';
2214 temp = (char **)realloc(string_list, sizeof(char *) * (num_of_strings + 1)); /* Allocate new buffer to store a pointer to a new string */
2217 EM_DEBUG_EXCEPTION("realloc failed");
2221 memset(temp + num_of_strings, 0X00, sizeof(char *));
2225 string_list[num_of_strings] = em_malloc(max_string_len);/* Allocate new buffer to store the string */
2227 if (NULL == string_list[num_of_strings]) {
2228 EM_DEBUG_EXCEPTION(" em_malloc failed ");
2232 SNPRINTF(string_list[num_of_strings - 1] , max_string_len, "%d,", numbers[j]);/* Start making new string */
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';
2244 emcore_free_comma_separated_strings(&string_list, &num_of_strings);
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;
2254 if (NULL != err_code)
2257 EM_DEBUG_FUNC_END("ret [%d]", ret);
2261 * @fn emcore_free_comma_separated_strings(char *** string_list, int *string_count)
2262 * Frees the double dimensional array of strings.
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.
2268 * @return This function does not return anything.
2270 INTERNAL_FUNC void emcore_free_comma_separated_strings(char *** string_list, int *string_count)
2272 EM_DEBUG_FUNC_BEGIN();
2274 char **str_list = NULL;
2277 if (NULL != string_list) {
2278 str_list = *string_list;
2280 if (0 != *string_count) {
2281 count = *string_count;
2282 for (i = 0; i < count; ++i)
2283 EM_SAFE_FREE(str_list[i]);
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 */
2290 EM_DEBUG_FUNC_END();
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)
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, };
2306 if (!source_file_name || !result_file_name) {
2307 EM_DEBUG_EXCEPTION("Invalid Parameter");
2308 err = EMAIL_ERROR_INVALID_PARAM;
2312 strncpy(attachment_file_name, source_file_name, MAX_PATH);
2313 extcheck = strchr(attachment_file_name, '.');
2316 EM_DEBUG_LOG("Extension Exist in the Attachment [%s] ", extcheck);
2317 else { /* No extension attached, So add the Extension based on the subtype */
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);
2324 EM_DEBUG_LOG("UnKnown Extesnsion");
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);
2335 EM_DEBUG_FUNC_END();
2339 #ifdef __FEATURE_LOCAL_ACTIVITY__
2340 INTERNAL_FUNC int emcore_add_activity(emstorage_activity_tbl_t *new_activity, int *err_code)
2342 EM_DEBUG_FUNC_BEGIN();
2344 EM_DEBUG_LOG("\t new_activity[%p], err_code[%p]", new_activity, err_code);
2346 /* default variable */
2348 int err = EMAIL_ERROR_NONE;
2350 if (!new_activity) {
2351 EM_DEBUG_LOG("\t new_activity[%p]\n", new_activity);
2352 err = EMAIL_ERROR_INVALID_PARAM;
2355 if (!emstorage_add_activity(new_activity, false, &err)) {
2356 EM_DEBUG_LOG("\t emstorage_add_activity falied - %d\n", err);
2369 INTERNAL_FUNC int emcore_delete_activity(emstorage_activity_tbl_t *activity, int *err_code)
2371 EM_DEBUG_FUNC_BEGIN();
2373 EM_DEBUG_LOG("\t new_activity[%p], err_code[%p]", activity, err_code);
2375 /* default variable */
2377 int err = EMAIL_ERROR_NONE;
2379 EM_DEBUG_LOG("\t new_activity[%p]\n", activity);
2381 err = EMAIL_ERROR_INVALID_PARAM;
2384 if (!emstorage_delete_local_activity(activity, true, &err)) {
2385 EM_DEBUG_LOG("\t emstorage_delete_local_activity falied - %d\n", err);
2398 INTERNAL_FUNC int emcore_get_next_activity_id(int *activity_id, int *err_code)
2400 EM_DEBUG_FUNC_BEGIN();
2403 int err = EMAIL_ERROR_NONE;
2405 if (NULL == activity_id)
2407 EM_DEBUG_EXCEPTION("\t activity_id[%p]", activity_id);
2409 err = EMAIL_ERROR_INVALID_PARAM;
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);
2421 if (NULL != err_code) {
2429 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
2432 INTERNAL_FUNC void emcore_free_rule(email_rule_t* rule)
2434 EM_DEBUG_FUNC_BEGIN();
2439 EM_SAFE_FREE(rule->value);
2441 EM_DEBUG_FUNC_END();