2 * Copyright 2012 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.tizenopensource.org/license
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <glib/gprintf.h>
20 #include <appcore-common.h>
25 #include <drm_client.h>
26 #include <drm_client_types.h>
28 #include <media_content.h>
30 #include "email-utils.h"
32 static pthread_mutex_t icu_mutex = PTHREAD_MUTEX_INITIALIZER;
33 char *icu_locale = NULL;
34 static UDateTimePatternGenerator *icu_pattern_generator = NULL;
35 static UDateFormat *icu_formatter_hmm = NULL;
36 static UDateFormat *icu_formatter_Hmm = NULL;
37 static UDateFormat *icu_formatter_MMMyyyy = NULL;
38 static UDateFormat *icu_formatter_MMMd = NULL;
39 static UDateFormat *icu_formatter_EEEMMMd = NULL;
40 enum appcore_time_format icu_timeformat;
44 EMAIL_GROUP_UNKNOWN = -1,
46 EMAIL_GROUP_YESTERDAY,
53 EMAIL_GROUP_TWOWEEKS_AGO,
54 EMAIL_GROUP_THREEWEEKS_AGO,
55 EMAIL_GROUP_EARLIER_THISMONTH,
56 EMAIL_GROUP_LASTMONTH,
60 EMAIL_GROUP_FAVORITES,
61 EMAIL_GROUP_ATTACHMENTS,
63 EMAIL_GROUP_PRIORITY_HIGH,
64 EMAIL_GROUP_PRIORITY_NORMAL,
65 EMAIL_GROUP_PRIORITY_LOW,
70 static gboolean _copy_actual_file(const char *src_full_path, const char *dest_full_path, gboolean(*copy_file_cb) (float percentage));
71 static void _generate_best_pattern(const char *locale, UChar * customSkeleton, char *formattedString, void *time);
72 static int _open_icu_pattern_n_formatter(const char *locale, char *skeleton, UDateFormat **formatter);
73 static int _close_icu_pattern_n_formatter(UDateFormat *formatter);
76 * Function implementation
78 char *email_get_current_theme_name(void)
80 char *theme_name = NULL;
81 char *save_ptr = NULL;
83 /* Get current theme path. Return value is a full path of theme file. ex) blue-hd:default */
84 const char *current_theme_path = elm_theme_get(NULL);
85 debug_log("current_theme_path [%s]", current_theme_path);
87 if (current_theme_path == NULL) {
88 debug_log("current_theme_path is NULL !!");
92 theme_name = strdup(current_theme_path);
93 if (theme_name == NULL) {
94 debug_log("theme_name is NULL !!");
98 theme_name = strtok_r(theme_name, ":", &save_ptr);
99 if (theme_name == NULL) {
100 debug_log("theme_name is NULL !!");
104 debug_log("theme_name [%s]", theme_name);
109 gboolean email_check_file_exist(const gchar *path)
112 RETURN_VAL_IF_FAIL(STR_VALID(path), FALSE);
114 if (!g_file_test(path, G_FILE_TEST_EXISTS)) {
120 gboolean email_check_dir_exist(const gchar *path)
123 RETURN_VAL_IF_FAIL(STR_VALID(path), FALSE);
125 if (!g_file_test(path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
131 gchar *email_parse_get_filename_from_path(const gchar *path)
134 RETURN_VAL_IF_FAIL(STR_VALID(path), NULL);
138 gchar *file_path = g_strdup(path);
139 file_path = g_strstrip(file_path);
140 gchar **token_list = g_strsplit_set(file_path, "/", -1);
141 g_free(file_path); /* MUST BE. */
143 RETURN_VAL_IF_FAIL(token_list != NULL, NULL);
145 while (token_list[index] != NULL) {
149 gchar *file_name = g_strdup(token_list[index - 1]);
151 g_strfreev(token_list); /* MUST BE. */
155 len = strlen(file_name);
157 gchar *ext = g_strrstr(file_name, ".");
159 len = len - strlen(ext);
161 gchar *file_name_without_ext = g_strndup(file_name, len);
163 g_free(file_name); /* MUST BE. */
165 debug_log("file name (%s)", file_name_without_ext);
167 return file_name_without_ext;
170 void email_parse_get_filename_n_ext_from_path(const gchar *path, gchar **ret_file_name, gchar **ret_ext)
173 RETURN_IF_FAIL(STR_VALID(path));
177 gchar *file_path = g_strdup(path);
178 file_path = g_strstrip(file_path);
179 gchar **token_list = g_strsplit_set(file_path, "/", -1);
180 g_free(file_path); /* MUST BE. */
182 RETURN_IF_FAIL(token_list != NULL);
184 while (token_list[index] != NULL) {
188 gchar *file_name = g_strdup(token_list[index - 1]);
190 g_strfreev(token_list); /* MUST BE. */
194 len = strlen(file_name);
196 gchar *ext = g_strrstr(file_name, ".");
198 len = len - strlen(ext);
200 *ret_file_name = g_strndup(file_name, len);
202 *ret_ext = g_strdup(ext);
207 gchar *email_parse_get_filepath_from_path(const gchar *path)
210 RETURN_VAL_IF_FAIL(STR_VALID(path), NULL);
212 gchar *file_path = NULL;
214 gint size = STR_LEN((gchar *)path);
216 for (i = (size - 1); i >= 0; --i) {
217 if (path[i] == '/') {
218 file_path = g_strndup(path, i + 1);
223 debug_log("file path (%s)", file_path);
228 void email_save_file(const gchar *path, const gchar *buf, gsize len)
231 if (STR_LEN((gchar *)buf) > 0 && len > 0) {
232 int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
234 int size = write(fd, (const void *)buf, (size_t) len);
235 debug_log("size:%d", size);
241 gchar *email_get_buff_from_file(const gchar *path, guint max_kbyte)
244 RETURN_VAL_IF_FAIL(STR_VALID(path), NULL);
246 debug_log("path (%s)", path);
248 FILE *fp = fopen(path, "r");
252 char body[MAX_STR_LEN + 1] = { 0, };
260 memset(body, 0, sizeof(body));
262 size = fread(body, sizeof(unsigned char), MAX_STR_LEN, fp);
263 body[MAX_STR_LEN] = '\0';
265 if (size <= 0 || STR_LEN(body) == 0)
268 if (max_kbyte > 0 && kbyte > max_kbyte)
271 if (STR_VALID(buff)) {
272 temp = g_strconcat(buff, body, NULL);
277 buff = g_strdup(body);
286 debug_log("buff [%s]", buff);
295 gchar *email_get_file_size_string(guint64 size)
298 gchar str_buf[16] = "\0";
301 g_sprintf(str_buf, "%d %s", (gint) size, dgettext("sys_string", "IDS_COM_BODY_B"));
303 gdouble tmpsize = (gdouble) (size / 1024.);
305 if (tmpsize < 1024) {
306 g_sprintf(str_buf, "%.2f %s", tmpsize, dgettext("sys_string", "IDS_COM_BODY_KB"));
310 if (tmpsize < 1024) {
311 g_sprintf(str_buf, "%.2f %s", tmpsize, dgettext("sys_string", "IDS_COM_BODY_MB"));
314 g_sprintf(str_buf, "%.2f %s", tmpsize, dgettext("sys_string", "IDS_COM_BODY_GB"));
318 return STR_LEN(str_buf) > 0 ? g_strdup(str_buf) : NULL;
321 guint64 email_get_file_size(const gchar *path)
326 memset(&st, 0, sizeof(struct stat));
328 if (stat(path, &st) == 0) {
329 if (S_ISREG(st.st_mode)) {
330 size = (guint64) st.st_size;
331 debug_log("size (%llu)", size);
339 #define EMAIL_ACCOUNT_RGEX "([a-z0-9!#$%&'*+/=?^_`{|}~-]+[.])*[a-z0-9!#$%&'*+/=?^_`{|}~-]+"
340 #define EMAIL_DOMAIN_RGEX "([a-z0-9!#$%&'*+/=?^_`{|}~-]+[.])+[a-z0-9!#$%&'*+/=?^_`{|}~-]+"
341 #define EMAIL_ADDR_RGEX EMAIL_ACCOUNT_RGEX"@"EMAIL_DOMAIN_RGEX
343 gboolean email_get_address_validation(const char *address)
346 /* this following code verfies the email alias string using reg. exp. */
347 regex_t alias_list_regex;
350 if (regcomp(&alias_list_regex, EMAIL_ADDR_RGEX, REG_ICASE | REG_EXTENDED)) {
351 debug_log("email alias regex unrecognized");
355 int nsub = alias_list_regex.re_nsub + 1; /* should be the number of parenthesized subexpressions (+1) */
356 regmatch_t pmatch[nsub];
357 memset(pmatch, 0, sizeof(regmatch_t) * nsub);
359 if (regexec(&alias_list_regex, address, nsub, pmatch, 0) == REG_NOMATCH)
360 debug_log("failed : [%s]", address);
362 debug_log("success : [%s]", address);
365 * remove sub-string match
367 if ((int)(pmatch[0].rm_eo - pmatch[0].rm_so) == strlen(address))
371 regfree(&alias_list_regex);
376 gchar *email_cut_text_by_byte_len(const gchar *text, gint len)
379 RETURN_VAL_IF_FAIL(STR_VALID(text), NULL);
380 RETURN_VAL_IF_FAIL(len > 0, NULL);
382 gint char_len2 = g_utf8_strlen(text, len);
383 gchar *offset = g_utf8_offset_to_pointer(text, char_len2);
385 gchar *ret_text = (gchar *)g_malloc0(offset - text + 1);
387 STR_NCPY(ret_text, (gchar *)text, offset - text);
392 static int is_yesterday(struct tm *req_tm, struct tm *now_tm)
394 if (now_tm->tm_yday == 0) { /* It is the first day of year */
395 if (req_tm->tm_year == now_tm->tm_year-1 && req_tm->tm_mon == 12 && req_tm->tm_mday == 31)
400 if (req_tm->tm_year == now_tm->tm_year && req_tm->tm_yday == now_tm->tm_yday - 1)
407 static int is_today(struct tm *req_tm, struct tm *now_tm)
409 if (req_tm->tm_year == now_tm->tm_year && req_tm->tm_yday == now_tm->tm_yday)
415 static int is_nth_weeks_ago(struct tm *req_tm, struct tm *now_tm, int num)
417 time_t req = mktime(req_tm);
418 time_t now = mktime(now_tm);
419 time_t nth_begin = 0, nth_end = 0;
420 nth_end = now - (now_tm->tm_wday * 3600 * 24 + now_tm->tm_hour * 3600 + now_tm->tm_min * 60 + now_tm->tm_sec) - ((num - 1) * 7 * 3600 * 24);
421 nth_begin = now - (now_tm->tm_wday * 3600 * 24 + now_tm->tm_hour * 3600 + now_tm->tm_min * 60 + now_tm->tm_sec) - (num * 7 * 3600 * 24);
423 if(req > nth_begin && req < nth_end)
429 static int is_lastmonth(struct tm *req_tm, struct tm *now_tm)
431 if((req_tm->tm_year == now_tm->tm_year - 1) && (req_tm->tm_mon == 11) && (now_tm->tm_mon == 0))
433 else if((req_tm->tm_year == now_tm->tm_year) && (req_tm->tm_mon == now_tm->tm_mon - 1))
439 int email_get_group_title_str(const time_t req_time, gchar **group_title,
443 RETURN_VAL_IF_FAIL(group_title != NULL, 0);
444 RETURN_VAL_IF_FAIL(group_date != NULL, 0);
446 int groupIndex = EMAIL_GROUP_UNKNOWN;
447 tzset(); /* MUST BE. */
449 time_t now_time = time(NULL);
451 struct tm *dummy = localtime(&now_time);
453 memcpy(&now_tm, dummy, sizeof(struct tm));
455 dummy = localtime(&req_time);
457 memcpy(&req_tm, dummy, sizeof(struct tm));
459 email_icu_mutex_lock();
461 if (is_today(&req_tm, &now_tm) || req_time > now_time /*future mail :)*/) {
462 *group_title = g_strdup(dgettext("sys_string", "IDS_COM_BODY_TODAY"));
463 *group_date = email_get_date_text_with_formatter(icu_formatter_EEEMMMd, (void *)&req_time);
464 groupIndex = EMAIL_GROUP_TODAY;
466 else if (is_yesterday(&req_tm, &now_tm)) {
467 *group_title = g_strdup(dgettext("sys_string", "IDS_COM_BODY_YESTERDAY"));
468 *group_date = email_get_date_text_with_formatter(icu_formatter_EEEMMMd, (void *)&req_time);
469 groupIndex = EMAIL_GROUP_YESTERDAY;
471 else if (is_nth_weeks_ago(&req_tm, &now_tm, 0)) {
472 switch(req_tm.tm_wday)
475 *group_title = g_strdup(_("IDS_EMAIL_BODY_SUNDAY"));
476 groupIndex = EMAIL_GROUP_SUN;
479 *group_title = g_strdup(_("IDS_EMAIL_BODY_MONDAY"));
480 groupIndex = EMAIL_GROUP_MON;
483 *group_title = g_strdup(_("IDS_EMAIL_BODY_TUESDAY"));
484 groupIndex = EMAIL_GROUP_TUE;
487 *group_title = g_strdup(_("IDS_EMAIL_BODY_WEDNESDAY"));
488 groupIndex = EMAIL_GROUP_WED;
491 *group_title = g_strdup(_("IDS_EMAIL_BODY_THURSDAY"));
492 groupIndex = EMAIL_GROUP_THU;
495 *group_title = g_strdup(_("IDS_EMAIL_BODY_FRIDAY"));
496 groupIndex = EMAIL_GROUP_THU;
499 *group_title = g_strdup(_("IDS_EMAIL_BODY_SATURDAY"));
500 groupIndex = EMAIL_GROUP_THU;
503 debug_log("invalid date");
506 *group_date = email_get_date_text_with_formatter(icu_formatter_EEEMMMd, (void *)&req_time);
508 else if(is_nth_weeks_ago(&req_tm, &now_tm, 1)) {
509 *group_title = g_strdup(_("IDS_EMAIL_BODY_LAST_WEEK"));
510 groupIndex = EMAIL_GROUP_LASTWEEK;
513 else if(is_nth_weeks_ago(&req_tm, &now_tm, 2)) {
514 char str[MAX_STR_LEN] = { 0, };
515 snprintf(str, sizeof(str), _("IDS_EMAIL_BODY_PD_WEEKS_AGO"), 2);
516 *group_title = g_strdup(str);
517 groupIndex = EMAIL_GROUP_TWOWEEKS_AGO;
520 else if(is_nth_weeks_ago(&req_tm, &now_tm, 3)) {
521 char str[MAX_STR_LEN] = { 0, };
522 snprintf(str, sizeof(str), _("IDS_EMAIL_BODY_PD_WEEKS_AGO"), 3);
523 *group_title = g_strdup(str);
524 groupIndex = EMAIL_GROUP_THREEWEEKS_AGO;
527 else if((req_tm.tm_year == now_tm.tm_year) && (req_tm.tm_mon == now_tm.tm_mon)) {
528 *group_title = g_strdup(N_("Earlier this Month"));
529 groupIndex = EMAIL_GROUP_EARLIER_THISMONTH;
532 else if(is_lastmonth(&req_tm, &now_tm)) {
533 *group_title = g_strdup(_("IDS_EMAIL_BODY_LAST_MONTH"));
534 groupIndex = EMAIL_GROUP_LASTMONTH;
538 *group_title = g_strdup(_("IDS_EMAIL_BODY_OLDER_M_HISTORY"));
539 groupIndex = EMAIL_GROUP_OLDER;
542 email_icu_mutex_unlock();
543 /*debug_log("-now : %d.%d (%d)", now_tm.tm_year + 1900, now_tm.tm_yday, now_time);
544 debug_log("-req : %d.%d (%d)", req_tm.tm_year + 1900, req_tm.tm_yday, req_time);
545 debug_log("(%s) (%s)", *group_title, *group_date);*/
550 char *email_get_str_datetime(const time_t req_time)
554 tzset(); /* MUST BE. */
556 time_t now_time = time(NULL);
558 struct tm *dummy = localtime(&now_time);
560 memcpy(&now_tm, dummy, sizeof(struct tm));
562 dummy = localtime(&req_time);
564 memcpy(&req_tm, dummy, sizeof(struct tm));
566 /*debug_log("*now : %d.%d.%d", now_tm.tm_year + 1900, now_tm.tm_mon, now_tm.tm_mday);
567 debug_log("*req : %d.%d.%d", req_tm.tm_year + 1900, req_tm.tm_mon, req_tm.tm_mday);*/
568 email_icu_mutex_lock();
571 char *_ret_str = NULL;
572 if (is_today(&req_tm, &now_tm) || is_yesterday(&req_tm, &now_tm) ||
573 req_time > now_time) /* sometimes, future mail arrives :) */ {
574 /* today or yesterday */
575 _ret_str = (icu_timeformat == APPCORE_TIME_FORMAT_12)?
576 email_get_date_text_with_formatter(icu_formatter_hmm, (void *)&req_time):
577 email_get_date_text_with_formatter(icu_formatter_Hmm, (void *)&req_time);
578 } else if (req_tm.tm_year < now_tm.tm_year) /* previous year */
579 _ret_str = email_get_date_text_with_formatter(icu_formatter_MMMyyyy, (void *)&req_time);
580 else /* days before yesterday in this year */
581 _ret_str = email_get_date_text_with_formatter(icu_formatter_MMMd, (void *)&req_time);
584 email_icu_mutex_unlock();
589 char *email_get_date_text(const char *locale, char *skeleton, void *time)
592 char formattedString[128] = { 0, };
593 UChar customSkeleton[64] = { 0, };
594 int skeletonLength = strlen(skeleton);
596 email_icu_mutex_lock();
597 u_uastrncpy(customSkeleton, skeleton, skeletonLength);
598 _generate_best_pattern(locale, customSkeleton, formattedString, time);
599 email_icu_mutex_unlock();
600 return g_strdup(formattedString);
603 char *email_get_date_text_with_formatter(UDateFormat *formatter, void *time)
606 char formattedString[128] = { 0, };
608 UErrorCode status = U_ZERO_ERROR;
610 UChar formatted[64] = { 0, };
611 int32_t formattedCapacity;
612 int32_t formattedLength;
614 formattedCapacity = (int32_t) (sizeof(formatted) / sizeof(formatted[0]));
616 time_t msg_time = *(time_t *)time;
617 date = (UDate)msg_time * 1000; /* Equivalent to Date = ucal_getNow() in Milliseconds */
619 formattedLength = udat_format(formatter, date, formatted, formattedCapacity, NULL, &status);
620 u_austrncpy(formattedString, formatted, 128);
621 return g_strdup(formattedString);
623 void email_icu_mutex_init(void)
626 pthread_mutex_init(&icu_mutex, NULL);
629 void email_icu_mutex_lock(void)
632 pthread_mutex_lock(&icu_mutex);
635 void email_icu_mutex_unlock(void)
638 pthread_mutex_unlock(&icu_mutex);
641 void email_icu_mutex_destroy(void)
644 pthread_mutex_destroy(&icu_mutex);
647 int email_open_icu_pattern_generator(void)
650 UErrorCode status = U_ZERO_ERROR;
652 uloc_setDefault(getenv("LC_TIME"), &status);
653 if (U_FAILURE(status)) {
654 debug_critical("uloc_setDefault() failed: %s\n", u_errorName(status));
658 status = U_ZERO_ERROR;
659 icu_locale = (char *)uloc_getDefault();
660 debug_log("uloc_getDefault: %s", icu_locale);
661 appcore_get_timeformat(&icu_timeformat);
662 icu_pattern_generator = udatpg_open(icu_locale, &status);
664 if (!icu_pattern_generator) {
665 debug_log("udatpg_open() failed: %s", u_errorName(status));
669 _open_icu_pattern_n_formatter(icu_locale, "hmm", &icu_formatter_hmm);
670 _open_icu_pattern_n_formatter(icu_locale, "Hmm", &icu_formatter_Hmm);
671 _open_icu_pattern_n_formatter(icu_locale, "MMMyyyy", &icu_formatter_MMMyyyy);
672 _open_icu_pattern_n_formatter(icu_locale, "MMMd", &icu_formatter_MMMd);
673 _open_icu_pattern_n_formatter(icu_locale, "EEEMMMd", &icu_formatter_EEEMMMd);
678 int email_close_icu_pattern_generator(void)
682 if (icu_pattern_generator) {
683 udatpg_close(icu_pattern_generator);
684 icu_pattern_generator = NULL;
687 _close_icu_pattern_n_formatter(icu_formatter_hmm);
688 _close_icu_pattern_n_formatter(icu_formatter_Hmm);
689 _close_icu_pattern_n_formatter(icu_formatter_MMMyyyy);
690 _close_icu_pattern_n_formatter(icu_formatter_MMMd);
691 _close_icu_pattern_n_formatter(icu_formatter_EEEMMMd);
696 static int _open_icu_pattern_n_formatter(const char *locale, char *skeleton, UDateFormat **formatter)
699 UErrorCode status = U_ZERO_ERROR;
700 UChar bestPattern[64] = { 0, };
701 UChar customSkeleton[64] = { 0, };
702 int32_t bestPatternCapacity;
703 int32_t bestPatternLength;
704 int skeletonLength = strlen(skeleton);
706 u_uastrncpy(customSkeleton, skeleton, skeletonLength);
708 bestPatternCapacity = (int32_t) (sizeof(bestPattern) / sizeof(bestPattern[0]));
709 bestPatternLength = udatpg_getBestPattern(icu_pattern_generator, customSkeleton, u_strlen(customSkeleton), bestPattern, bestPatternCapacity, &status);
710 *formatter = udat_open(UDAT_IGNORE, UDAT_IGNORE, locale, NULL, -1, bestPattern, -1, &status);
715 static int _close_icu_pattern_n_formatter(UDateFormat *formatter)
719 udat_close(formatter);
724 static void _generate_best_pattern(const char *locale, UChar * customSkeleton, char *formattedString, void *time)
727 UErrorCode status = U_ZERO_ERROR;
728 UDateFormat *formatter;
730 UChar bestPattern[64] = { 0, };
731 UChar formatted[64] = { 0, };
732 int32_t bestPatternCapacity, formattedCapacity;
733 int32_t bestPatternLength, formattedLength;
735 bestPatternCapacity = (int32_t) (sizeof(bestPattern) / sizeof(bestPattern[0]));
736 bestPatternLength = udatpg_getBestPattern(icu_pattern_generator, customSkeleton, u_strlen(customSkeleton), bestPattern, bestPatternCapacity, &status);
738 formatter = udat_open(UDAT_IGNORE, UDAT_IGNORE, locale, NULL, -1, bestPattern, -1, &status);
739 formattedCapacity = (int32_t) (sizeof(formatted) / sizeof(formatted[0]));
741 time_t msg_time = *(time_t *)time;
742 date = (UDate)msg_time * 1000; /* Equivalent to Date = ucal_getNow() in Milliseconds */
744 formattedLength = udat_format(formatter, date, formatted, formattedCapacity, NULL, &status);
745 u_austrncpy(formattedString, formatted, 128);
746 udat_close(formatter);
749 void _create_download_folder()
753 if (!email_check_dir_exist(DIR_DEFAULT_MEDIA_PHONE"/Downloads")) {
755 nErr = mkdir(DIR_DEFAULT_MEDIA_PHONE"/Downloads", 0755);
756 debug_log("errno: %d", nErr);
758 debug_log("Downloads folder creation failed");
760 debug_log("Downloads folder already exists.");
763 int email_attachments_save_file(const gchar *path, gchar *newpath, gboolean(*copy_file_cb) (float percentage))
766 RETURN_VAL_IF_FAIL(STR_VALID(path), EMAIL_EXT_SAVE_ERR_UNKNOWN);
768 gchar new_path[MAX_PATH_LEN] = { 0, };
769 gchar tmp_path[MAX_PATH_LEN] = { 0, };
770 gchar new_filename[MAX_PATH_LEN] = { 0, };
771 gchar prefix[MAX_PATH_LEN] = { 0, };
772 gboolean saved = FALSE;
774 gint max_length = MAX_PATH_LEN;
776 snprintf(prefix, sizeof(prefix), "%s", DIR_DEFAULT_MEDIA_PHONE"/Downloads");
778 debug_log("prefix:%s", prefix);
780 memset(new_path, 0, sizeof(MAX_PATH_LEN));
781 memset(tmp_path, 0, sizeof(MAX_PATH_LEN));
783 if (STR_LEN(tmp_path) == 0) {
784 g_sprintf(tmp_path, "%s", path);
787 gchar *file_name = NULL;
788 gchar *file_ext = NULL;
789 gchar *file_path = email_parse_get_filepath_from_path(tmp_path);
790 email_parse_get_filename_n_ext_from_path(tmp_path, &file_name, &file_ext);
792 debug_log("file_name:%s", file_name);
793 debug_log("file_ext:%s", file_ext);
795 if (file_ext == NULL)
798 if (file_name != NULL) {
800 if (strlen(file_name) + strlen(file_ext) > max_length - STR_LEN(prefix)) {
801 gint available_len = max_length - STR_LEN(prefix);
803 if (strlen(file_ext) > 0) {
804 available_len -= strlen(file_ext);
807 gchar *new_name = email_cut_text_by_byte_len(file_name, available_len);
809 if (STR_VALID(new_name)) {
810 g_sprintf(new_path, "%s/%s%s", prefix, new_name, file_ext);
811 g_sprintf(new_filename, "%s%s", new_name, file_ext);
812 g_free(new_name); /* MUST BE. */
815 g_sprintf(new_path, "%s/%s%s", prefix, file_name, file_ext);
819 if (STR_VALID(file_path)) {
820 g_free(file_path); /* MUST BE. */
823 if (STR_VALID(file_name)) {
824 g_free(file_name); /* MUST BE. */
827 if (STR_VALID(file_ext)) {
828 g_free(file_ext); /* MUST BE. */
831 debug_log("new_path:%s", new_path);
833 /* Move for DRM file */
835 ret = media_content_connect();
837 _create_download_folder();
839 /* Copy for non-DRM */
840 if (access(new_path, F_OK) != -1) {
841 debug_log("file existed");
842 err = EMAIL_EXT_SAVE_ERR_ALREADY_EXIST;
844 saved = _copy_actual_file(path, new_path, copy_file_cb);
845 debug_log("saved(%d), err(%d), errno(%d)", saved, err, errno);
848 err = EMAIL_EXT_SAVE_ERR_NONE;
850 err = EMAIL_EXT_SAVE_ERR_UNKNOWN;
851 ret = media_content_scan_file(new_path);
852 debug_log("media_content_scan_file: %d", ret);
855 snprintf(newpath, MAX_PATH_LEN, "%s", new_path);
856 ret = media_content_disconnect();
858 debug_log("media_content_disconnect() is failed!");
861 debug_log("media_content_connect() is failed!");
866 static gboolean _copy_actual_file(const char *src_full_path, const char *dest_full_path, gboolean(*copy_file_cb) (float percentage))
871 char buff[4096] = { 0, };
873 gboolean result = FALSE;
874 gboolean stop_copying = FALSE;
877 struct stat statbuf = { 0 };
881 gboolean remove_dest = FALSE;
883 clock_t finish; /* consumed time to copy whole file */
885 float percentage = 0.0;
887 fs = fopen(src_full_path, "rb");
890 debug_log("fopen error(%d)", err);
894 ret = fstat(fileno(fs), &statbuf);
896 debug_log("fstat error");
901 total_size = (int)statbuf.st_size;
903 fseek(fs, 0, SEEK_SET);
905 fd = fopen(dest_full_path, "wb");
911 debug_log("fopen error(%d)", err);
916 fseek(fd, 0, SEEK_SET);
925 n = fread(buff, sizeof(char), sizeof(buff), fs);
927 m = fwrite(buff, sizeof(char), n, fd);
929 debug_log("fwrite = %d", m);
938 percentage = ((float)(total_size - copied_size) / (float)total_size) * 100.0;
940 stop_copying = copy_file_cb(percentage);
966 if (remove_dest && result == FALSE) {
967 if (-1 == remove(dest_full_path)) {
968 debug_log("Failed to remove dest_full_path");
975 totaltime = (double)(finish - begin) / CLOCKS_PER_SEC;
976 debug_log("takes %f s to copy %s", totaltime, src_full_path);
982 gboolean email_drm_file_is_right(const gchar *path)
984 RETURN_VAL_IF_FAIL(STR_VALID(path), FALSE);
988 drm_bool_type_e is_drm_file = FALSE;
990 if (drm_is_drm_file(path, &is_drm_file) != DRM_RETURN_SUCCESS) {
991 debug_log("drm_is_drm_file is failed");
998 gboolean email_drm_file_forward_lock_check(const gchar *path)
1000 RETURN_VAL_IF_FAIL(STR_VALID(path), FALSE);
1001 RETURN_VAL_IF_FAIL(email_drm_file_is_right(path), FALSE);
1005 drm_file_info_s drm_info;
1007 if (drm_get_file_info(path, &drm_info)) {
1008 debug_log("drm_info.oma_info.method (%d)", drm_info.oma_info.method);
1009 if (drm_info.oma_info.method == DRM_METHOD_TYPE_FORWARD_LOCK || drm_info.oma_info.method == DRM_METHOD_TYPE_COMBINED_DELIVERY) {
1010 debug_log("Forward Lock");
1019 static int termination_flag = 0;
1020 static int pause_flag = 0;
1022 void set_app_terminated()
1024 termination_flag = 1;
1027 int get_app_terminated()
1029 return termination_flag;
1032 void set_app_paused()
1037 void reset_app_paused()
1042 int get_app_paused()
1047 char* email_util_strrtrim(char* s)
1051 end = s + strlen(s) - 1;
1052 while (end != s && isspace(*end))
1058 debug_log("result [%s]", s);
1062 char* email_util_strltrim(char* s)
1067 while (*begin != '\0') {
1068 if (isspace(*begin))
1076 debug_log("result [%s]", s);
1080 char* email_util_get_parent_folder(char *s)
1082 gchar **vector = NULL;
1083 vector = g_strsplit_set(s, "/", -1);
1084 int vlen = g_strv_length(vector);
1085 gchar *foler_name = g_strdup(vector[vlen-1]);
1086 gchar* retval = g_strdup(s);
1088 ptr = g_strstr_len(retval, -1, foler_name);
1090 debug_log("input [%s], vlen(%d)", s, vlen);
1092 if (ptr != NULL && vlen > 1){
1093 gchar* temp = g_strndup(retval, ptr-retval-1);
1095 retval = g_strdup(temp);
1098 debug_log("foler_name [%s], retval[%s]", foler_name, retval);
1105 debug_log("foler_name [%s]: parent == root. ", foler_name);
1111 int email_register_timezone_changed_callback(void *func, void *data)
1114 if (vconf_notify_key_changed(VCONFKEY_SYSTEM_TIME_CHANGED, func, data) < 0) {
1115 debug_log("vconf callback registration(VCONFKEY_SYSTEM_TIME_CHANGED) is failed");
1122 int email_deregister_timezone_changed_callback(void *func)
1125 if (vconf_ignore_key_changed(VCONFKEY_SYSTEM_TIME_CHANGED, func) < 0) {
1126 debug_log("vconf callback removal(VCONFKEY_SYSTEM_TIME_CHANGED) is failed");
1132 /* Contact service2 */
1133 int email_get_contacts_list(contacts_match_str_flag_e match, contacts_list_h *list, const char *search_word)
1137 int ct_ret = CONTACTS_ERROR_NONE;
1139 contacts_query_h query = NULL;
1140 contacts_filter_h filter = NULL;
1141 debug_log("search_word = %s", search_word);
1143 /* get number list first */
1144 ct_ret = contacts_query_create(_contacts_contact_email._uri, &query);
1145 ct_ret = contacts_filter_create(_contacts_contact_email._uri, &filter);
1146 ct_ret = contacts_filter_add_str(filter, _contacts_contact_email.email, match, search_word);
1147 ct_ret = contacts_query_set_filter(query, filter);
1149 ct_ret = contacts_db_get_records_with_query(query, 0, 0, list);
1150 ct_ret = contacts_filter_destroy(filter);
1151 ct_ret = contacts_query_destroy(query);
1153 if (ct_ret != CONTACTS_ERROR_NONE) {
1154 debug_log("contacts_db_get_records_with_query is failed error_code = %d", ct_ret);
1159 int email_get_contacts_index(contacts_record_h record, int *index)
1162 int ct_ret = CONTACTS_ERROR_NONE;
1163 ct_ret = contacts_record_get_int(record, _contacts_contact_email.contact_id, index);
1167 int email_get_contacts_display_name(contacts_record_h record, char *display_name)
1170 int ct_ret = CONTACTS_ERROR_NONE;
1171 ct_ret = contacts_record_get_str_p(record, _contacts_contact_email.display_name, &display_name);
1172 // contacts_list_item->display = g_strdup(display_name);
1176 int email_get_contacts_email_address(contacts_record_h record, char *email_addr)
1179 int ct_ret = CONTACTS_ERROR_NONE;
1180 ct_ret = contacts_record_get_str_p(record, _contacts_contact_email.email, &email_addr);
1184 int email_get_contacts_image_thumbnail_path(contacts_record_h record, char *image_thumbnail_path)
1187 int ct_ret = CONTACTS_ERROR_NONE;
1188 ct_ret = contacts_record_get_str_p(record, _contacts_contact_email.image_thumbnail_path, &image_thumbnail_path);
1192 int email_get_contacts_first_name(contacts_record_h record, char *first_name)
1195 int ct_ret = CONTACTS_ERROR_NONE;
1196 ct_ret = contacts_record_get_str_p(record, _contacts_name.first, &first_name);
1200 int email_get_contacts_last_name(contacts_record_h record, char *last_name)
1203 int ct_ret = CONTACTS_ERROR_NONE;
1204 ct_ret = contacts_record_get_str_p(record, _contacts_name.last, &last_name);
1208 int email_get_contacts_list_info(contacts_list_h list, EMAIL_CONTACT_LIST_INFO_S *ct_list_info)
1212 int ct_ret = CONTACTS_ERROR_NONE;
1214 while (CONTACTS_ERROR_NONE == ct_ret) {
1215 contacts_record_h ct_value = NULL;
1216 contacts_list_get_current_record_p(list, &ct_value);
1217 debug_log("ct_ret = %d", ct_ret);
1218 debug_log("ct_value = %d", (ct_value == NULL));
1220 ct_ret = contacts_record_get_int(ct_value, _contacts_contact_email.contact_id, &index);
1222 char *display_name = NULL;
1223 char *image_path = NULL;
1224 char *email_addr = NULL;
1226 ct_list_info->index = index;
1227 if ((ct_ret = contacts_record_get_str_p(ct_value, _contacts_contact_email.display_name, &display_name)) == CONTACTS_ERROR_NONE) {
1228 if (display_name != NULL) {
1229 ct_list_info->display = g_strdup(display_name);
1232 debug_log("email_get_contacts_display_name is failed error_code = %d", ct_ret);
1235 if ((ct_ret = contacts_record_get_str_p(ct_value, _contacts_contact_email.image_thumbnail_path, &image_path)) == CONTACTS_ERROR_NONE) {
1236 if (image_path != NULL) {
1237 ct_list_info->image_path = g_strdup(image_path);
1240 debug_log("email_get_contacts_image_thumbnail_path is failed error_code = %d", ct_ret);
1243 if ((ct_ret = contacts_record_get_str_p(ct_value, _contacts_contact_email.email, &email_addr)) == CONTACTS_ERROR_NONE){
1244 if (email_addr != NULL) {
1245 ct_list_info->email_address = g_strdup(email_addr);
1248 debug_log("email_get_contacts_email_address is failed error_code = %d", ct_ret);
1251 if (!ct_list_info->display && email_addr) {
1252 strncpy(ct_list_info->display_name, email_addr, sizeof(ct_list_info->display_name) - 1);
1254 if (strlen(ct_list_info->display) > 0) {
1255 snprintf(ct_list_info->display_name, sizeof(ct_list_info->display_name), "%s", ct_list_info->display);
1259 debug_log("index(%d), display(%s), image_path(%s), email(%s), display_name(%s)",
1260 index, ct_list_info->display, ct_list_info->image_path, ct_list_info->email_address, ct_list_info->display_name);
1265 ct_ret = contacts_list_next(list);
1270 int email_num_id_get_contacts_record(int num_id, contacts_record_h* out_record)
1273 int ct_ret = CONTACTS_ERROR_NONE;
1275 /* get record email */
1276 ct_ret = contacts_db_get_record(_contacts_contact_email._uri, num_id, out_record);
1277 if (ct_ret != CONTACTS_ERROR_NONE) {
1278 debug_log("_contacts_number, db_get_record is failed : ct_ret = [%d]", ct_ret);