2.0 alpha
[apps/core/preloaded/calendar.git] / common / util-efl.c
index aed8e7f..0f649ba 100755 (executable)
   */
 
 
-
-
-
-
 #include <Ecore_X.h>
-#include <unicode/uloc.h>
+#include <unicode/ucal.h>
 #include <unicode/udat.h>
 #include <unicode/udatpg.h>
+#include <unicode/uloc.h>
+#include <unicode/ustring.h>
+#include <unicode/ustdio.h>
+#include <vconf.h>
 
 #include "cld.h"
 
-extern int u_strlen(UChar*);
-extern void u_austrncpy(char*, UChar*, int);
-extern void u_uastrncpy(UChar*, char*, int);
-
 static UDateTimePatternGenerator *pattern_generator = NULL;
 static pthread_mutex_t mutex_lock = PTHREAD_MUTEX_INITIALIZER;
 static UErrorCode status = U_ZERO_ERROR;
@@ -44,20 +40,8 @@ static const char *_icons[] = {
 };
 static char _partbuf[1024];
 static char _textbuf[1024];
-static char _titlebuf[1024];
 static char _sigbuf[1024];
-
-Eina_Bool cal_util_black_theme_check()
-{
-       const char *curr_theme = elm_theme_get(NULL);
-
-       if (curr_theme)
-       {
-               return (strstr(curr_theme,"black")?EINA_TRUE:EINA_FALSE);
-       }
-
-       return EINA_FALSE;
-}
+static char _timezone[32];
 
 Evas_Object* cal_util_add_bg(Evas_Object *obj, Eina_Bool is_window)
 {
@@ -85,8 +69,6 @@ Evas_Object* cal_util_add_bg(Evas_Object *obj, Eina_Bool is_window)
 
 Evas_Object* cal_util_add_layout(Evas_Object *win, const char *g)
 {
-       CAL_FN_START;
-
        c_retvm_if(!win, NULL, "win is null");
 
        Eina_Bool r;
@@ -110,8 +92,6 @@ Evas_Object* cal_util_add_layout(Evas_Object *win, const char *g)
        evas_object_size_hint_weight_set(eo, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
        evas_object_show(eo);
 
-       CAL_FN_END;
-
        return eo;
 }
 
@@ -126,7 +106,7 @@ Evas_Object* cal_util_add_rectangle(Evas_Object *p)
        return r;
 }
 
-static void cal_util_delete_window(void *data, Evas_Object *obj, void *event)
+static void __cal_util_delete_window(void *data, Evas_Object *obj, void *event)
 {
        elm_exit();
 }
@@ -141,11 +121,10 @@ Evas_Object* cal_util_add_window(const char *name, Evas_Coord* w, Evas_Coord* h)
        Evas_Coord width, height;
 
        elm_win_title_set(eo, name);
-       elm_win_borderless_set(eo, EINA_TRUE);
 
        /*For calendar ring, window delete callback is not needed.*/
        if (strcmp(name, CALENDAR_RING))
-               evas_object_smart_callback_add(eo, "delete,request", cal_util_delete_window, NULL);
+               evas_object_smart_callback_add(eo, "delete,request", __cal_util_delete_window, NULL);
 
        ecore_x_window_size_get(ecore_x_window_root_first_get(), &width, &height);
        evas_object_resize(eo, width, height);
@@ -272,6 +251,164 @@ int cal_util_disconnect_pattern_generator()
        return 0;
 }
 
+void cal_util_set_timezone(const char *timezone)
+{
+       c_ret_if(!timezone);
+
+       snprintf(_timezone, sizeof(_timezone), "%s", timezone);
+}
+
+void cal_util_initialize_timezone()
+{
+       int is_lock_timezone = 0;
+
+       int ret = vconf_get_int(CAL_VCONFKEY_LOCK_TIMEZONE_OFFSET, &is_lock_timezone);
+       c_ret_if(ret);
+
+       char *text = NULL;
+
+       if (is_lock_timezone)
+               text = vconf_get_str(CAL_VCONFKEY_LOCK_TIMEZONE_PATH);
+       else
+               text = vconf_get_str(VCONFKEY_SETAPPL_TIMEZONE_ID);
+
+       if (CAL_STRLEN(text)) {
+
+               cal_util_set_timezone(text);
+
+               free(text);
+       } else
+               ERR("vconf_get_str is failed.");
+}
+
+void cal_util_convert_lli_to_tm(const char *timezone, long long int lli, struct tm *tm)
+{
+       c_ret_if(!tm);
+
+       UErrorCode status = U_ZERO_ERROR;
+       UChar utf16_timezone[64] = {0};
+
+       if (timezone)
+               u_uastrncpy(utf16_timezone, timezone, 64);
+       else
+               u_uastrncpy(utf16_timezone, _timezone, sizeof(_timezone));
+
+       UCalendar *cal = ucal_open(utf16_timezone, u_strlen(utf16_timezone), uloc_getDefault(), UCAL_TRADITIONAL, &status);
+       c_ret_if(!cal);
+
+       ucal_setMillis(cal, (double)(lli* 1000.0), &status);
+
+       tm->tm_year = ucal_get(cal, UCAL_YEAR, &status) - 1900;
+       tm->tm_mon = ucal_get(cal, UCAL_MONTH, &status);
+       tm->tm_mday = ucal_get(cal, UCAL_DATE, &status);
+
+       if (ucal_get(cal, UCAL_AM_PM, &status))
+               tm->tm_hour = ucal_get(cal, UCAL_HOUR, &status) + 12;
+       else
+               tm->tm_hour = ucal_get(cal, UCAL_HOUR, &status);
+
+       tm->tm_min = ucal_get(cal, UCAL_MINUTE, &status);
+       tm->tm_sec = ucal_get(cal, UCAL_SECOND, &status);
+       tm->tm_isdst = ucal_get(cal, UCAL_DST_OFFSET, &status);
+       tm->tm_wday = ucal_get(cal, UCAL_DAY_OF_WEEK, &status) - 1;
+       tm->tm_yday = ucal_get(cal, UCAL_DAY_OF_YEAR, &status) - 1;
+
+       ucal_close(cal);
+}
+
+void cal_util_convert_tm_to_lli(const char *timezone, struct tm *tm, long long int *lli)
+{
+       c_ret_if(!tm);
+       c_ret_if(!lli);
+
+       UErrorCode status = U_ZERO_ERROR;
+       UChar utf16_timezone[64] = {0};
+
+       if (timezone)
+               u_uastrncpy(utf16_timezone, timezone, 64);
+       else
+               u_uastrncpy(utf16_timezone, _timezone, sizeof(_timezone));
+
+       UCalendar *cal = ucal_open(utf16_timezone, u_strlen(utf16_timezone), uloc_getDefault(), UCAL_TRADITIONAL, &status);
+       c_ret_if(!cal);
+
+       ucal_setAttribute(cal, UCAL_LENIENT, 1);
+       ucal_setDateTime(cal, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, &status);
+
+       UDate millis = ucal_getMillis(cal, &status);
+
+       *lli = (long long int)(millis / 1000);
+
+       ucal_close(cal);
+}
+
+void cal_util_convert_lli_to_time_t(const char *timezone, long long int lli, time_t *time)
+{
+       c_ret_if(!time);
+
+       UErrorCode status = U_ZERO_ERROR;
+       UChar utf16_timezone[64] = {0};
+
+       if (timezone)
+               u_uastrncpy(utf16_timezone, timezone, 64);
+       else
+               u_uastrncpy(utf16_timezone, _timezone, sizeof(_timezone));
+
+       UCalendar *cal = ucal_open(utf16_timezone, u_strlen(utf16_timezone), uloc_getDefault(), UCAL_TRADITIONAL, &status);
+       c_ret_if(!cal);
+
+       ucal_setMillis(cal, (double)(lli* 1000.0), &status);
+
+       struct tm tm;
+
+       tm.tm_year = ucal_get(cal, UCAL_YEAR, &status) - 1900;
+       tm.tm_mon = ucal_get(cal, UCAL_MONTH, &status);
+       tm.tm_mday = ucal_get(cal, UCAL_DATE, &status);
+
+       if (ucal_get(cal, UCAL_AM_PM, &status))
+               tm.tm_hour = ucal_get(cal, UCAL_HOUR, &status) + 12;
+       else
+               tm.tm_hour = ucal_get(cal, UCAL_HOUR, &status);
+
+       tm.tm_min = ucal_get(cal, UCAL_MINUTE, &status);
+       tm.tm_sec = ucal_get(cal, UCAL_SECOND, &status);
+       tm.tm_isdst = ucal_get(cal, UCAL_DST_OFFSET, &status);
+       tm.tm_wday = ucal_get(cal, UCAL_DAY_OF_WEEK, &status) - 1;
+       tm.tm_yday = ucal_get(cal, UCAL_DAY_OF_YEAR, &status) - 1;
+
+       *time = mktime(&tm);
+
+       ucal_close(cal);
+}
+
+void cal_util_convert_time_t_to_lli(const char *timezone, time_t time, long long int *lli)
+{
+       c_ret_if(!lli);
+
+       UErrorCode status = U_ZERO_ERROR;
+       UChar utf16_timezone[64] = {0};
+
+       if (timezone)
+               u_uastrncpy(utf16_timezone, timezone, 64);
+       else
+               u_uastrncpy(utf16_timezone, _timezone, sizeof(_timezone));
+
+       UCalendar *cal = ucal_open(utf16_timezone, u_strlen(utf16_timezone), uloc_getDefault(), UCAL_TRADITIONAL, &status);
+       c_ret_if(!cal);
+
+       ucal_setAttribute(cal, UCAL_LENIENT, 1);
+
+       struct tm tm;
+
+       ucal_setDateTime(cal, tm.tm_year + 1900, tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, &status);
+
+       UDate millis = ucal_getMillis(cal, &status);
+
+       *lli = (long long int)(millis / 1000);
+
+       ucal_close(cal);
+}
+
 
 UDate cal_util_get_u_date_from_time_t(time_t time)
 {
@@ -291,7 +428,7 @@ UDate cal_util_get_u_date_from_tm(const struct tm* tm)
        return date;
 }
 
-static void __cal_util_generate_best_pattern(UChar *customSkeleton, const struct tm* tm)
+static void __cal_util_generate_best_pattern(UChar *custom_format, const struct tm* tm, char *buffer, int buffer_size)
 {
        UErrorCode status = U_ZERO_ERROR;
        UDateFormat *formatter;
@@ -306,7 +443,7 @@ static void __cal_util_generate_best_pattern(UChar *customSkeleton, const struct
        const char* locale = uloc_getDefault();
 
        bestPatternCapacity = (int32_t)(sizeof(bestPattern)/sizeof((bestPattern)[0]));
-       bestPatternLength = udatpg_getBestPattern(pattern_generator, customSkeleton, u_strlen(customSkeleton), bestPattern,   bestPatternCapacity, &status);
+       bestPatternLength = udatpg_getBestPattern(pattern_generator, custom_format, u_strlen(custom_format), bestPattern,   bestPatternCapacity, &status);
 
        u_austrncpy(bestPatternString, bestPattern, 128);
 
@@ -320,46 +457,40 @@ static void __cal_util_generate_best_pattern(UChar *customSkeleton, const struct
 
        udat_close(formatter);
 
-       snprintf(_textbuf,1023,"%s",formattedString);
+       snprintf(buffer, buffer_size, "%s", formattedString);
 }
 
-void cal_util_get_time_format_from_skeleton(const char* dateSkeleton, const char* timeSkeleton, const struct tm* tm)
+static void __cal_util_get_time_text_from_format(const char *date_format, const char *time_format, const struct tm* tm, char *buffer, int buffer_size)
 {
-       char skeleton[128]={'\0'};
-       UChar customSkeleton[64]={'\0'};
-       int dateSkeletonLength=0,timeSkeletonLength=0,skeletonLength=0;
+       char format[128]={0};
+       UChar custom_format[64]={0};
+       int date_format_length = 0;
+       int time_format_length = 0;
+       int format_length = 0;
 
-       if(dateSkeleton)
-       {
-               dateSkeletonLength = CAL_STRLEN(dateSkeleton);
-               CAL_STRNCPY(skeleton,dateSkeleton,dateSkeletonLength);
+       if (date_format) {
+               date_format_length = CAL_STRLEN(date_format);
+               CAL_STRNCPY(format,date_format,date_format_length);
        }
 
-       if(timeSkeleton)
-       {
-               timeSkeletonLength = CAL_STRLEN(timeSkeleton);
-               CAL_STRNCAT(skeleton,timeSkeleton,timeSkeletonLength);
+       if (time_format) {
+               time_format_length = CAL_STRLEN(time_format);
+               CAL_STRNCAT(format,time_format,time_format_length);
        }
 
-       skeletonLength = CAL_STRLEN(skeleton);
+       format_length = CAL_STRLEN(format);
 
-       u_uastrncpy(customSkeleton, skeleton,skeletonLength);
+       u_uastrncpy(custom_format, format, format_length);
 
-       __cal_util_generate_best_pattern(customSkeleton, tm);
+       __cal_util_generate_best_pattern(custom_format, tm, buffer, buffer_size);
 }
 
-void cal_util_set_time_text(Evas_Object *obj, const char *part, const char *date, const char* time,
-               const struct tm *tm)
+void cal_util_set_time_text(Evas_Object *obj, const char *part, const char *date, const char* time, const struct tm *tm)
 {
-       cal_util_get_time_format_from_skeleton(date, time, tm);
+       char time_text[128] = {0};
+       __cal_util_get_time_text_from_format(date, time, tm, time_text, sizeof(time_text) - 1);
 
-       if (!CAL_STRCMP(part,"title/text"))
-       {
-               snprintf(_titlebuf, 1023,"%s",_textbuf);
-               edje_object_part_text_set(obj, part, _titlebuf);
-       }
-       else
-               edje_object_part_text_set(obj, part, _textbuf);
+       edje_object_part_text_set(obj, part, time_text);
 }
 
 void cal_util_set_time_week_text(Evas_Object *obj, const char *part, const char *date, const char* time,
@@ -370,28 +501,36 @@ void cal_util_set_time_week_text(Evas_Object *obj, const char *part, const char
        c_retm_if(!t, "t is null.");
 
        struct tm tm = *t;
-       char week_start[1024] = {0};
-       if (!CAL_STRCMP(part,"title/text")) {
+       char week[8] = {0};
+       char start_date[8] = {0};
+       char end_date[8] = {0};
+       char month_year[32] = {0};
+       char time_text[128] = {0};
 
-               tm.tm_mday -= CAL_UTIL_GET_WDAY(tm.tm_wday - start);
-               cal_util_get_time_format_from_skeleton(date, time, &tm);
-               snprintf(week_start, 1023, "%s", _textbuf);
+       __cal_util_get_time_text_from_format(CAL_UTIL_DATE_FORMAT_16, NULL, &tm, week, sizeof(week) - 1);
 
-               tm.tm_mday += 7;
-               cal_util_get_time_format_from_skeleton(date, time, &tm);
-               snprintf(_titlebuf, 1023,"%s ~ %s", week_start, _textbuf);
+       tm.tm_mday -= CAL_UTIL_GET_WDAY(tm.tm_wday - start);
+       __cal_util_get_time_text_from_format(CAL_UTIL_DATE_FORMAT_9, NULL, &tm, start_date, sizeof(start_date) - 1);
 
-               edje_object_part_text_set(obj, part, _titlebuf);
-       }
-       else
-               edje_object_part_text_set(obj, part, _textbuf);
+       __cal_util_get_time_text_from_format(CAL_UTIL_DATE_FORMAT_6, NULL, &tm, month_year, sizeof(month_year) - 1);
+
+       tm.tm_mday += 6;
+       __cal_util_get_time_text_from_format(CAL_UTIL_DATE_FORMAT_9, NULL, &tm, end_date, sizeof(end_date) - 1);
+
+       snprintf(time_text, sizeof(time_text) - 1,"W%s ( %s - %s ) %s", week, start_date, end_date, month_year);
+
+       edje_object_part_text_set(obj, part, time_text);
 }
 
 int cal_util_get_time_text(char* buf, int buf_size, const char *date, const char* time, const struct tm *tm)
 {
-       cal_util_get_time_format_from_skeleton(date, time, tm);
-       snprintf(buf, buf_size, "%s", _textbuf);
-       return CAL_STRLEN(_textbuf);
+       char time_text[128] = {0};
+
+       __cal_util_get_time_text_from_format(date, time, tm, time_text, sizeof(time_text) - 1);
+
+       snprintf(buf, buf_size, "%s", time_text);
+
+       return CAL_STRLEN(time_text);
 }
 
 void cal_util_emit_signal(Evas_Object *obj, const char *fmt, ...)
@@ -471,14 +610,14 @@ static void __cal_util_edit_field_changed_callback(void *data, Evas_Object *obj,
        c_retm_if(!obj, "obj is null");
 
        if (elm_object_focus_get(data)) {
+               elm_object_signal_emit(data, "elm,state,eraser,show", "elm");
+       }
+       else {
                if (elm_entry_is_empty(obj))
-                       elm_object_signal_emit(data, "elm,state,eraser,hide", "elm");
+                       elm_object_signal_emit(data, "elm,state,guidetext,show", "elm");
                else
-                       elm_object_signal_emit(data, "elm,state,eraser,show", "elm");
+                       elm_object_signal_emit(data, "elm,state,guidetext,hide", "elm");
        }
-
-       if (!elm_entry_is_empty(obj))
-               elm_object_signal_emit(data, "elm,state,guidetext,hide", "elm");
 }
 
 static void __cal_util_edit_field_focused_callback(void *data, Evas_Object *obj, void *event_info) // Focused callback will show X marked button and hide guidetext.
@@ -486,8 +625,7 @@ static void __cal_util_edit_field_focused_callback(void *data, Evas_Object *obj,
        c_retm_if(!data, "data is null");
        c_retm_if(!obj, "obj is null");
 
-       if (!elm_entry_is_empty(obj))
-               elm_object_signal_emit(data, "elm,state,eraser,show", "elm");
+       elm_object_signal_emit(data, "elm,state,eraser,show", "elm");
 
        elm_object_signal_emit(data, "elm,state,guidetext,hide", "elm");
 }
@@ -558,14 +696,14 @@ static void __cal_util_searchbar_changed_callback(void *data, Evas_Object *obj,
        c_retm_if(!obj, "obj is null");
 
        if (elm_object_focus_get(data)) {
+               elm_object_signal_emit(data, "elm,state,eraser,show", "elm");
+       }
+       else {
                if (elm_entry_is_empty(obj))
-                       elm_object_signal_emit(data, "elm,state,eraser,hide", "elm");
+                       elm_object_signal_emit(data, "elm,state,guidetext,show", "elm");
                else
-                       elm_object_signal_emit(data, "elm,state,eraser,show", "elm");
+                       elm_object_signal_emit(data, "elm,state,guidetext,hide", "elm");
        }
-
-       if (!elm_entry_is_empty(obj))
-               elm_object_signal_emit(data, "elm,state,guidetext,hide", "elm");
 }
 
 static void __cal_util_searchbar_with_cancel_btn_focused_callback(void *data, Evas_Object *obj, void *event_info)
@@ -586,8 +724,7 @@ static void __cal_util_searchbar_focused_callback(void *data, Evas_Object *obj,
        c_retm_if(!data, "data is null");
        c_retm_if(!obj, "obj is null");
 
-       if (!elm_entry_is_empty(obj))
-               elm_object_signal_emit(data, "elm,state,eraser,show", "elm");
+       elm_object_signal_emit(data, "elm,state,eraser,show", "elm");
 
        elm_object_signal_emit(data, "elm,state,guidetext,hide", "elm");
 }
@@ -786,7 +923,7 @@ Evas_Object * cal_util_add_popup(Evas_Object *parent, const char *style, const c
        return popup;
 }
 
-Evas_Object * cal_util_add_datetime(Evas_Object *parent, const char *title, struct tm *tm)
+Evas_Object * cal_util_add_datetime(Evas_Object *parent, const char *title, const struct tm *tm)
 {
        c_retvm_if(!parent, NULL, "parent is null");
 
@@ -808,7 +945,7 @@ Evas_Object * cal_util_add_datetime(Evas_Object *parent, const char *title, stru
                elm_object_part_text_set(layout, "elm.text", title);
 
                if (tm)
-                       elm_datetime_value_set(datetime, (const struct tm *)tm);
+                       elm_datetime_value_set(datetime, tm);
 
                elm_object_part_content_set(layout, "elm.icon", datetime);
 
@@ -822,8 +959,26 @@ Evas_Object * cal_util_add_datetime(Evas_Object *parent, const char *title, stru
                evas_object_size_hint_weight_set(datetime, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
 
                if (tm)
-                       elm_datetime_value_set(datetime, (const struct tm *)tm);
+                       elm_datetime_value_set(datetime, tm);
 
                return datetime;
        }
 }
+
+void cal_util_get_week_number_text(const struct tm* tm, char *buffer, int buffer_size)
+{
+       c_retm_if(!tm, "tm is null");
+       c_retm_if(!buffer, "buffer is null");
+
+       __cal_util_get_time_text_from_format(CAL_UTIL_DATE_FORMAT_16, NULL ,tm, buffer, buffer_size);
+}
+
+int cal_util_get_distance(Evas_Coord_Point *s, Evas_Coord_Point *e)
+{
+       c_retvm_if(!s, -1, "s is null");
+       c_retvm_if(!e, -1, "e is null");
+
+       return (Evas_Coord)sqrt((s->x - e->x) * (s->x - e->x)
+               + (s->y - e->y) * (s->y - e->y));
+}
+