[misc] Sync from master branch.
[apps/core/preloaded/calendar.git] / common / util-efl.c
1 /*
2   *
3   *  Copyright 2012  Samsung Electronics Co., Ltd
4   *
5   *  Licensed under the Flora License, Version 1.0 (the "License");
6   *  you may not use this file except in compliance with the License.
7   *  You may obtain a copy of the License at
8   *
9   *       http://floralicense.org/license/
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17
18
19 #include <Ecore_X.h>
20 #include <unicode/ucal.h>
21 #include <unicode/udat.h>
22 #include <unicode/udatpg.h>
23 #include <unicode/uloc.h>
24 #include <unicode/ustring.h>
25 #include <unicode/ustdio.h>
26 #include <vconf.h>
27
28 #include "cld.h"
29 #include "cld-images.h"
30
31 static UDateTimePatternGenerator *pattern_generator = NULL;
32 static pthread_mutex_t mutex_lock = PTHREAD_MUTEX_INITIALIZER;
33 static UErrorCode status = U_ZERO_ERROR;
34 static int pattern_generator_reference_counter = 0;
35
36 static char _partbuf[1024];
37 static char _textbuf[1024];
38 static char _sigbuf[1024];
39 static char _timezone[32];
40
41 Evas_Object* cal_util_add_bg(Evas_Object *obj, Eina_Bool is_window)
42 {
43         CAL_FN_START;
44
45         Evas_Object *bg;
46         CAL_ASSERT(obj);
47         bg = elm_bg_add(obj);
48         if (!bg)
49                 return NULL;
50
51         evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
52
53         if (is_window)
54                 elm_win_resize_object_add(obj, bg);
55         else
56                 elm_object_style_set(bg, "group_list");
57
58         evas_object_show(bg);
59
60         CAL_FN_END;
61
62         return bg;
63 }
64
65 Evas_Object* cal_util_add_layout(Evas_Object *win, const char *g)
66 {
67         c_retvm_if(!win, NULL, "win is null");
68
69         Eina_Bool r;
70         Evas_Object *eo = elm_layout_add(win);
71         c_retvm_if(!eo, NULL, "elm_layout_add returned null");
72
73         if (g) {
74                 r = elm_layout_file_set(eo, EDJ_FILE, g);
75                 c_warn_if(r == EINA_FALSE, "elm_layout_file_set is failed");
76         }
77         else {
78                 r = elm_layout_theme_set(eo, "layout", "application", "default");
79                 c_warn_if(r == EINA_FALSE, "elm_layout_theme_set is failed");
80         }
81
82         if (r == EINA_FALSE) {
83                 evas_object_del(eo);
84                 return NULL;
85         }
86
87         evas_object_size_hint_weight_set(eo, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
88         evas_object_show(eo);
89
90         return eo;
91 }
92
93 Evas_Object* cal_util_add_layout_noindicator(Evas_Object *win)
94 {
95         c_retvm_if(!win, NULL, "win is null");
96
97         Eina_Bool r;
98         Evas_Object *eo = elm_layout_add(win);
99         c_retvm_if(!eo, NULL, "elm_layout_add returned null");
100
101         r = elm_layout_theme_set(eo, "layout", "application", "noindicator");
102         c_warn_if(r == EINA_FALSE, "elm_layout_theme_set is failed");
103
104         if (r == EINA_FALSE) {
105                 evas_object_del(eo);
106                 return NULL;
107         }
108
109         evas_object_size_hint_weight_set(eo, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
110         evas_object_show(eo);
111
112         return eo;
113 }
114
115 Evas_Object* cal_util_add_rectangle(Evas_Object *p)
116 {
117         Evas_Object *r;
118
119         r = evas_object_rectangle_add(evas_object_evas_get(p));
120         CAL_ASSERT(r);
121
122         evas_object_show(r);
123         return r;
124 }
125
126 static void __cal_util_delete_window(void *data, Evas_Object *obj, void *event)
127 {
128         elm_exit();
129 }
130
131 static void profile_changed_cb(void *data, Evas_Object * obj, void *event)
132 {
133         const char *profile = elm_config_profile_get();
134
135         if (strcmp(profile, "desktop") == 0)
136                 elm_win_indicator_mode_set (obj, ELM_WIN_INDICATOR_HIDE);
137         else
138                 elm_win_indicator_mode_set (obj, ELM_WIN_INDICATOR_SHOW);
139 }
140
141 Evas_Object* cal_util_add_window(const char *name, Evas_Coord* w, Evas_Coord* h)
142 {
143         CAL_FN_START;
144
145         Evas_Object *eo = elm_win_add(NULL, name, ELM_WIN_BASIC);
146         c_retvm_if(!eo, NULL, "elm_win_add returned null");
147
148         Evas_Coord width, height;
149
150         elm_win_title_set(eo, name);
151
152         /*For calendar ring, window delete callback is not needed.*/
153         if (strcmp(name, CALENDAR_RING))
154                 evas_object_smart_callback_add(eo, "delete,request", __cal_util_delete_window, NULL);
155
156         evas_object_smart_callback_add(eo, "profile,changed", profile_changed_cb, NULL);
157
158         ecore_x_window_size_get(ecore_x_window_root_first_get(), &width, &height);
159         evas_object_resize(eo, width, height);
160
161         elm_win_conformant_set(eo, EINA_TRUE);
162         elm_win_indicator_mode_set(eo, ELM_WIN_INDICATOR_SHOW);
163
164         if (w && h) {
165                 *w = width;
166                 *h = height;
167         }
168
169         if (elm_win_wm_rotation_supported_get(eo)) {
170                 int rots[4] = { CAL_WINDOW_ROTATION_0,
171                         CAL_WINDOW_ROTATION_90,
172                         CAL_WINDOW_ROTATION_180,
173                         CAL_WINDOW_ROTATION_270 };
174                 elm_win_wm_rotation_available_rotations_set(eo, &rots, 4);
175         }
176
177         CAL_FN_END;
178
179         return eo;
180 }
181
182 char* cal_util_get_part_text(const char *fmt, int pos)
183 {
184         snprintf(_partbuf, sizeof(_partbuf), fmt, pos);
185         return _partbuf;
186 }
187
188
189 void cal_util_set_text(Evas_Object *obj, const char *part, const char *fmt, ...)
190 {
191         va_list ap;
192
193         va_start(ap, fmt);
194         vsnprintf(_textbuf, sizeof(_textbuf), fmt, ap);
195         va_end(ap);
196
197         edje_object_part_text_set(obj, part, _textbuf);
198 }
199
200 int cal_util_connect_pattern_generator()
201 {
202         CAL_FN_START;
203
204         pthread_mutex_lock(&mutex_lock);
205         if(pattern_generator_reference_counter == 0)
206         {
207                 uloc_setDefault(getenv("LC_TIME"), &status);
208                 if(!pattern_generator)
209                         pattern_generator = udatpg_open(uloc_getDefault(), &status);
210                 if(!pattern_generator)
211                 {
212                         ERR("udatpg_open fail : %s", u_errorName(status));
213                         pthread_mutex_unlock(&mutex_lock);
214                         return -1;
215                 }
216         }
217
218         pattern_generator_reference_counter++;
219         pthread_mutex_unlock(&mutex_lock);
220
221         CAL_FN_END;
222
223         return 0;
224 }
225
226 int cal_util_disconnect_pattern_generator()
227 {
228         pthread_mutex_lock(&mutex_lock);
229
230         if(pattern_generator_reference_counter == 1)
231         {
232                 if(pattern_generator)
233                         udatpg_close(pattern_generator);
234                 pattern_generator = NULL;
235         }
236
237         pattern_generator_reference_counter--;
238
239         pthread_mutex_unlock(&mutex_lock);
240
241         return 0;
242 }
243
244 void cal_util_set_timezone(const char *timezone)
245 {
246         c_ret_if(!timezone);
247
248         snprintf(_timezone, sizeof(_timezone), "%s", timezone);
249 }
250
251 void cal_util_initialize_timezone()
252 {
253         CAL_FN_START;
254
255         int is_lock_timezone = 0;
256
257         int ret = vconf_get_int(CAL_VCONFKEY_LOCK_TIMEZONE_ON_OFF, &is_lock_timezone);
258         c_ret_if(ret);
259
260         char *text = NULL;
261
262         if (is_lock_timezone)
263                 text = vconf_get_str(CAL_VCONFKEY_LOCK_TIMEZONE_PATH);
264         else
265                 text = vconf_get_str(VCONFKEY_SETAPPL_TIMEZONE_ID);
266
267         if (CAL_STRLEN(text)) {
268
269                 cal_util_set_timezone(text);
270
271                 free(text);
272         } else
273                 ERR("vconf_get_str is failed.");
274
275         CAL_FN_END;
276 }
277
278 void cal_util_convert_lli_to_tm(const char *timezone, long long int lli, struct tm *tm)
279 {
280         c_ret_if(!tm);
281
282         UErrorCode status = U_ZERO_ERROR;
283         UChar utf16_timezone[64] = {0};
284
285         if (timezone)
286                 u_uastrncpy(utf16_timezone, timezone, 64);
287         else
288                 u_uastrncpy(utf16_timezone, _timezone, sizeof(_timezone));
289
290         UCalendar *cal = ucal_open(utf16_timezone, u_strlen(utf16_timezone), uloc_getDefault(), UCAL_TRADITIONAL, &status);
291         c_ret_if(!cal);
292
293         ucal_setMillis(cal, (double)(lli* 1000.0), &status);
294
295         tm->tm_year = ucal_get(cal, UCAL_YEAR, &status) - 1900;
296         tm->tm_mon = ucal_get(cal, UCAL_MONTH, &status);
297         tm->tm_mday = ucal_get(cal, UCAL_DATE, &status);
298
299         if (ucal_get(cal, UCAL_AM_PM, &status))
300                 tm->tm_hour = ucal_get(cal, UCAL_HOUR, &status) + 12;
301         else
302                 tm->tm_hour = ucal_get(cal, UCAL_HOUR, &status);
303
304         tm->tm_min = ucal_get(cal, UCAL_MINUTE, &status);
305         tm->tm_sec = ucal_get(cal, UCAL_SECOND, &status);
306         tm->tm_isdst = ucal_get(cal, UCAL_DST_OFFSET, &status);
307         tm->tm_wday = ucal_get(cal, UCAL_DAY_OF_WEEK, &status) - 1;
308         tm->tm_yday = ucal_get(cal, UCAL_DAY_OF_YEAR, &status) - 1;
309
310         ucal_close(cal);
311 }
312
313 void cal_util_convert_tm_to_lli(const char *timezone, const struct tm *tm, long long int *lli)
314 {
315         c_ret_if(!tm);
316         c_ret_if(!lli);
317
318         UErrorCode status = U_ZERO_ERROR;
319         UChar utf16_timezone[64] = {0};
320
321         if (timezone)
322                 u_uastrncpy(utf16_timezone, timezone, 64);
323         else
324                 u_uastrncpy(utf16_timezone, _timezone, sizeof(_timezone));
325
326         UCalendar *cal = ucal_open(utf16_timezone, u_strlen(utf16_timezone), uloc_getDefault(), UCAL_TRADITIONAL, &status);
327         c_ret_if(!cal);
328
329         ucal_setAttribute(cal, UCAL_LENIENT, 1);
330         ucal_setDateTime(cal, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, &status);
331
332         UDate millis = ucal_getMillis(cal, &status);
333
334         *lli = (long long int)(millis / 1000);
335
336         ucal_close(cal);
337 }
338
339 void cal_util_convert_lli_to_time_t(const char *timezone, long long int lli, time_t *time)
340 {
341         c_ret_if(!time);
342
343         UErrorCode status = U_ZERO_ERROR;
344         UChar utf16_timezone[64] = {0};
345
346         if (timezone)
347                 u_uastrncpy(utf16_timezone, timezone, 64);
348         else
349                 u_uastrncpy(utf16_timezone, _timezone, sizeof(_timezone));
350
351         UCalendar *cal = ucal_open(utf16_timezone, u_strlen(utf16_timezone), uloc_getDefault(), UCAL_TRADITIONAL, &status);
352         c_ret_if(!cal);
353
354         ucal_setMillis(cal, (double)(lli* 1000.0), &status);
355
356         struct tm tm;
357
358         tm.tm_year = ucal_get(cal, UCAL_YEAR, &status) - 1900;
359         tm.tm_mon = ucal_get(cal, UCAL_MONTH, &status);
360         tm.tm_mday = ucal_get(cal, UCAL_DATE, &status);
361
362         if (ucal_get(cal, UCAL_AM_PM, &status))
363                 tm.tm_hour = ucal_get(cal, UCAL_HOUR, &status) + 12;
364         else
365                 tm.tm_hour = ucal_get(cal, UCAL_HOUR, &status);
366
367         tm.tm_min = ucal_get(cal, UCAL_MINUTE, &status);
368         tm.tm_sec = ucal_get(cal, UCAL_SECOND, &status);
369         tm.tm_isdst = ucal_get(cal, UCAL_DST_OFFSET, &status);
370         tm.tm_wday = ucal_get(cal, UCAL_DAY_OF_WEEK, &status) - 1;
371         tm.tm_yday = ucal_get(cal, UCAL_DAY_OF_YEAR, &status) - 1;
372
373         *time = mktime(&tm);
374
375         ucal_close(cal);
376 }
377
378 void cal_util_convert_time_t_to_lli(const char *timezone, time_t time, long long int *lli)
379 {
380         c_ret_if(!lli);
381
382         UErrorCode status = U_ZERO_ERROR;
383         UChar utf16_timezone[64] = {0};
384
385         if (timezone)
386                 u_uastrncpy(utf16_timezone, timezone, 64);
387         else
388                 u_uastrncpy(utf16_timezone, _timezone, sizeof(_timezone));
389
390         UCalendar *cal = ucal_open(utf16_timezone, u_strlen(utf16_timezone), uloc_getDefault(), UCAL_TRADITIONAL, &status);
391         c_ret_if(!cal);
392
393         ucal_setAttribute(cal, UCAL_LENIENT, 1);
394
395         struct tm tm;
396
397         ucal_setDateTime(cal, tm.tm_year + 1900, tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, &status);
398
399         UDate millis = ucal_getMillis(cal, &status);
400
401         *lli = (long long int)(millis / 1000);
402
403         ucal_close(cal);
404 }
405
406
407 UDate cal_util_get_u_date_from_time_t(time_t time)
408 {
409         UDate date;
410         date = (UDate ) time*1000;  /* Equivalent to Date = ucal_getNow() in Milliseconds */
411         return date;
412 }
413
414 UDate cal_util_get_u_date_from_tm(const struct tm* tm)
415 {
416         UDate date;
417         time_t time;
418
419         time = timelocal((struct tm*)tm);
420
421         date = (UDate ) time*1000;  /* Equivalent to Date = ucal_getNow() in Milliseconds */
422         return date;
423 }
424
425 static void __cal_util_generate_best_pattern(UChar *custom_format, const struct tm* tm, char *buffer, int buffer_size)
426 {
427         UErrorCode status = U_ZERO_ERROR;
428         UDateFormat *formatter;
429         UDate date;
430         UChar bestPattern[64] = {0,};
431         UChar formatted[64] = {0,};
432         char bestPatternString[128]={0,};
433         char formattedString[128] = {0,};
434         int32_t bestPatternCapacity, formattedCapacity;
435         int32_t bestPatternLength, formattedLength;
436         uloc_setDefault(getenv("LC_TIME"), &status);
437         const char* locale = uloc_getDefault();
438
439         bestPatternCapacity = (int32_t)(sizeof(bestPattern)/sizeof((bestPattern)[0]));
440         bestPatternLength = udatpg_getBestPattern(pattern_generator, custom_format, u_strlen(custom_format), bestPattern,   bestPatternCapacity, &status);
441
442         u_austrncpy(bestPatternString, bestPattern, 128);
443
444         date = cal_util_get_u_date_from_tm(tm);
445         formatter = udat_open(UDAT_IGNORE, UDAT_IGNORE, locale, NULL, -1, bestPattern, -1, &status);
446
447         formattedCapacity = (int32_t)(sizeof(formatted)/sizeof((formatted)[0]));
448         formattedLength = udat_format(formatter, date, formatted, formattedCapacity, NULL, &status);
449
450         u_austrncpy(formattedString, formatted, 128);
451
452         udat_close(formatter);
453
454         snprintf(buffer, buffer_size, "%s", formattedString);
455 }
456
457 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)
458 {
459         char format[128]={0};
460         UChar custom_format[64]={0};
461         int date_format_length = 0;
462         int time_format_length = 0;
463         int format_length = 0;
464
465         if (date_format) {
466                 date_format_length = CAL_STRLEN(date_format);
467                 CAL_STRNCPY(format,date_format,date_format_length);
468         }
469
470         if (time_format) {
471                 time_format_length = CAL_STRLEN(time_format);
472                 CAL_STRNCAT(format,time_format,time_format_length);
473         }
474
475         format_length = CAL_STRLEN(format);
476
477         u_uastrncpy(custom_format, format, format_length);
478
479         __cal_util_generate_best_pattern(custom_format, tm, buffer, buffer_size);
480 }
481
482 void cal_util_set_time_text(Evas_Object *obj, const char *part, const char *date, const char* time, const struct tm *tm)
483 {
484         char time_text[128] = {0};
485         __cal_util_get_time_text_from_format(date, time, tm, time_text, sizeof(time_text) - 1);
486
487         edje_object_part_text_set(obj, part, time_text);
488 }
489
490 void cal_util_set_item_time_text(Elm_Object_Item *item, const char *part, const char *date, const char* time, const struct tm *tm)
491 {
492         char time_text[128] = {0};
493         __cal_util_get_time_text_from_format(date, time, tm, time_text, sizeof(time_text) - 1);
494
495         elm_object_item_part_text_set(item, part, time_text);
496 }
497
498
499 static void __cal_util_generate_best_pattern_toupper(UChar *custom_format, const struct tm* tm, char *buffer, int buffer_size)
500 {
501         UErrorCode status = U_ZERO_ERROR;
502         UDateFormat *formatter;
503         UDate date;
504         UChar bestPattern[64] = {0,};
505         UChar formatted[64] = {0,};
506         UChar formatted_upper[64] = {0,};
507         char bestPatternString[128]={0,};
508         char formattedString[128] = {0,};
509         int32_t bestPatternCapacity, formattedCapacity;
510         int32_t bestPatternLength, formattedLength;
511         uloc_setDefault(getenv("LC_TIME"), &status);
512         const char* locale = uloc_getDefault();
513
514         bestPatternCapacity = (int32_t)(sizeof(bestPattern)/sizeof((bestPattern)[0]));
515         bestPatternLength = udatpg_getBestPattern(pattern_generator, custom_format, u_strlen(custom_format), bestPattern,   bestPatternCapacity, &status);
516
517         u_austrncpy(bestPatternString, bestPattern, 128);
518
519         date = cal_util_get_u_date_from_tm(tm);
520         formatter = udat_open(UDAT_IGNORE, UDAT_IGNORE, locale, NULL, -1, bestPattern, -1, &status);
521
522         formattedCapacity = (int32_t)(sizeof(formatted)/sizeof((formatted)[0]));
523         formattedLength = udat_format(formatter, date, formatted, formattedCapacity, NULL, &status);
524
525         u_strToUpper(formatted_upper, 64, formatted, 64, "", &status);
526
527         u_austrncpy(formattedString, formatted_upper, 128);
528
529         udat_close(formatter);
530
531         snprintf(buffer, buffer_size, "%s", formattedString);
532 }
533
534 static void __cal_util_get_time_text_from_format_toupper(const char *date_format, const char *time_format, const struct tm* tm, char *buffer, int buffer_size)
535 {
536         char format[128]={0};
537         UChar custom_format[64]={0};
538         int date_format_length = 0;
539         int time_format_length = 0;
540         int format_length = 0;
541
542         if (date_format) {
543                 date_format_length = CAL_STRLEN(date_format);
544                 CAL_STRNCPY(format,date_format,date_format_length);
545         }
546
547         if (time_format) {
548                 time_format_length = CAL_STRLEN(time_format);
549                 CAL_STRNCAT(format,time_format,time_format_length);
550         }
551
552         format_length = CAL_STRLEN(format);
553
554         u_uastrncpy(custom_format, format, format_length);
555
556         __cal_util_generate_best_pattern_toupper(custom_format, tm, buffer, buffer_size);
557 }
558
559 void cal_util_set_time_text_toupper(Evas_Object *obj, const char *part, const char *date, const char* time, const struct tm *tm)
560 {
561         char time_text[128] = {0};
562         __cal_util_get_time_text_from_format_toupper(date, time, tm, time_text, sizeof(time_text) - 1);
563
564         edje_object_part_text_set(obj, part, time_text);
565 }
566
567 void cal_util_set_time_week_text(Evas_Object *obj, const char *part, const char *date, const char* time,
568                 const struct tm *t, int start)
569 {
570         c_retm_if(!obj, "obj is null.");
571         c_retm_if(!part, "part is null.");
572         c_retm_if(!t, "t is null.");
573
574         struct tm tm = *t;
575         char week[8] = {0};
576         char start_date[8] = {0};
577         char end_date[8] = {0};
578         char month_year[32] = {0};
579         char time_text[128] = {0};
580
581         __cal_util_get_time_text_from_format(CAL_UTIL_DATE_FORMAT_16, NULL, &tm, week, sizeof(week) - 1);
582
583         tm.tm_mday -= CAL_UTIL_GET_WDAY(tm.tm_wday - start);
584         __cal_util_get_time_text_from_format(CAL_UTIL_DATE_FORMAT_9, NULL, &tm, start_date, sizeof(start_date) - 1);
585
586         __cal_util_get_time_text_from_format(CAL_UTIL_DATE_FORMAT_6, NULL, &tm, month_year, sizeof(month_year) - 1);
587
588         tm.tm_mday += 6;
589         __cal_util_get_time_text_from_format(CAL_UTIL_DATE_FORMAT_9, NULL, &tm, end_date, sizeof(end_date) - 1);
590
591         snprintf(time_text, sizeof(time_text) - 1,"W%s(%s-%s) %s", week, start_date, end_date, month_year);
592
593         edje_object_part_text_set(obj, part, time_text);
594 }
595
596 void cal_util_set_item_time_week_text(Elm_Object_Item *item, const char *part, const char *date, const char* time,
597                 const struct tm *t, int start)
598 {
599         c_retm_if(!item, "item is null.");
600         c_retm_if(!part, "part is null.");
601         c_retm_if(!t, "t is null.");
602
603         struct tm tm = *t;
604         char week[8] = {0};
605         char start_date[8] = {0};
606         char end_date[8] = {0};
607         char month_year[32] = {0};
608         char time_text[128] = {0};
609
610         __cal_util_get_time_text_from_format(CAL_UTIL_DATE_FORMAT_16, NULL, &tm, week, sizeof(week) - 1);
611
612         tm.tm_mday -= CAL_UTIL_GET_WDAY(tm.tm_wday - start);
613         __cal_util_get_time_text_from_format(CAL_UTIL_DATE_FORMAT_9, NULL, &tm, start_date, sizeof(start_date) - 1);
614
615         __cal_util_get_time_text_from_format(CAL_UTIL_DATE_FORMAT_18, NULL, &tm, month_year, sizeof(month_year) - 1);
616
617         tm.tm_mday += 6;
618         __cal_util_get_time_text_from_format(CAL_UTIL_DATE_FORMAT_9, NULL, &tm, end_date, sizeof(end_date) - 1);
619
620         snprintf(time_text, sizeof(time_text) - 1,"W%s %s", week, month_year);
621
622         elm_object_item_part_text_set(item, part, time_text);
623 }
624
625 int cal_util_get_time_text(char* buf, int buf_size, const char *date, const char* time, const struct tm *tm)
626 {
627         char time_text[128] = {0};
628
629         __cal_util_get_time_text_from_format(date, time, tm, time_text, sizeof(time_text) - 1);
630
631         snprintf(buf, buf_size, "%s", time_text);
632
633         return CAL_STRLEN(time_text);
634 }
635
636 void cal_util_emit_signal(Evas_Object *obj, const char *fmt, ...)
637 {
638         va_list ap;
639
640         va_start(ap, fmt);
641         vsnprintf(_sigbuf, sizeof(_sigbuf), fmt, ap);
642         va_end(ap);
643
644         edje_object_signal_emit(obj, _sigbuf, "prog");
645 }
646
647 Evas_Object* cal_util_add_separator(Evas_Object *box, const char *style)
648 {
649         Evas_Object *sp;
650
651         if (!box || CAL_STRCMP(elm_object_widget_type_get(box), "box"))
652                 return NULL;
653
654         sp = elm_separator_add(box);
655         if (sp) {
656                 elm_separator_horizontal_set(sp, EINA_TRUE);
657                 elm_object_style_set(sp, style);
658                 evas_object_show(sp);
659                 elm_box_pack_end(box, sp);
660         }
661
662         return sp;
663 }
664
665 Evas_Object* cal_util_add_scroller(Evas_Object *p)
666 {
667         c_retv_if(!p, NULL);
668
669         Evas_Object *sc = NULL;
670
671         sc = elm_scroller_add(p);
672         c_retv_if(!sc, NULL);
673
674         elm_scroller_bounce_set(sc, EINA_FALSE, EINA_TRUE);
675         elm_scroller_policy_set(sc, ELM_SCROLLER_POLICY_OFF, ELM_SCROLLER_POLICY_AUTO);
676
677         evas_object_show(sc);
678
679         return sc;
680 }
681
682 Evas_Object* cal_util_add_box(Evas_Object *p)
683 {
684         Evas_Object *bx;
685
686         bx = elm_box_add(p);
687         if (!bx)
688                 return NULL;
689
690         evas_object_size_hint_weight_set(bx, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
691         evas_object_size_hint_align_set(bx, EVAS_HINT_FILL, 0.0);
692         evas_object_show(bx);
693
694         return bx;
695 }
696
697 void cal_util_set_controlbar_button(Evas_Object *btn, char *label, char *style, void (*func) (void *data, Evas_Object *obj, void *event_info), void* data)
698 {
699         CAL_ASSERT(btn);
700
701         elm_object_style_set(btn, style);
702         evas_object_size_hint_weight_set(btn, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
703         evas_object_size_hint_align_set(btn, EVAS_HINT_FILL, 0.5);
704         elm_object_text_set(btn, label);
705         evas_object_smart_callback_add(btn, "clicked", func, data);
706         evas_object_show(btn);
707 }
708
709 static void __cal_util_edit_field_changed_callback(void *data, Evas_Object *obj, void *event_info) // This callback is for showing(hiding) X marked button.
710 {
711         c_retm_if(!data, "data is null");
712         c_retm_if(!obj, "obj is null");
713
714         if (elm_object_focus_get(data)) {
715                 if (elm_entry_is_empty(obj))
716                         elm_object_signal_emit(data, "elm,state,rename,hide", "");
717                 else
718                         elm_object_signal_emit(data, "elm,state,eraser,show", "");
719         }
720 }
721
722 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.
723 {
724         c_retm_if(!data, "data is null");
725         c_retm_if(!obj, "obj is null");
726
727         elm_object_signal_emit(data, "elm,state,eraser,show", "");
728         elm_object_signal_emit(data, "elm,state,rename,hide", "");
729
730 }
731
732 static void __cal_util_edit_field_unfocused_callback(void *data, Evas_Object *obj, void *event_info) // Unfocused callback will show guidetext and hide X marked button.
733 {
734         c_retm_if(!data, "data is null");
735         c_retm_if(!obj, "obj is null");
736
737         elm_object_signal_emit(data, "elm,state,eraser,hide", "");
738         elm_object_signal_emit(data, "elm,state,rename,show", "");
739
740 }
741
742 static void __cal_util_edit_field_eraser_clicked_callback(void *data, Evas_Object *obj, const char *emission, const char *source) // When X marked button is clicked, empty entry's contents.
743 {
744         c_retm_if(!data, "data is null");
745
746         elm_entry_entry_set(data, "");
747 }
748
749 static void __cal_util_edit_field_clicked_callback(void *data, Evas_Object *obj, const char *emission, const char *source)
750 {
751         c_ret_if(!data);
752
753         elm_object_focus_set(data, EINA_TRUE);
754 }
755
756 Evas_Object * cal_util_add_edit_field(Evas_Object *parent, const char *title, const char *guide, Eina_Bool single_line, Eina_Bool is_editable, Eina_Bool is_no_bg)
757 {
758         c_retvm_if(!parent, NULL, "parent is null");
759
760         Evas_Object *layout = elm_layout_add(parent);
761         c_retvm_if(!layout, NULL, "layout is null");
762
763         if (CAL_STRLEN(title)) {
764                 elm_layout_theme_set(layout, "layout", "dialogue/editfield", "title");
765                 elm_object_part_text_set(layout, "elm.text", title);
766         } else  {
767                 if(is_no_bg)
768                         elm_layout_theme_set(layout, "layout", "editfield/no_bg", "default");
769                 else
770                         elm_layout_theme_set(layout, "layout", "dialogue/editfield", "default");
771         }
772
773         Evas_Object *entry = elm_entry_add(parent);
774         c_retvm_if(!entry, layout, "entry is null");
775
776         elm_object_part_content_set(layout, "elm.icon.entry", entry);
777         evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
778
779         if (CAL_STRLEN(guide))
780                 elm_object_part_text_set(entry, "elm.guide", guide);
781
782         elm_entry_single_line_set(entry, single_line);
783         elm_entry_scrollable_set(entry, single_line);
784
785         evas_object_smart_callback_add(entry, "changed", __cal_util_edit_field_changed_callback, layout);
786         evas_object_smart_callback_add(entry, "focused", __cal_util_edit_field_focused_callback, layout);
787         evas_object_smart_callback_add(entry, "unfocused", __cal_util_edit_field_unfocused_callback, layout);
788
789         elm_object_part_content_set(layout, "elm.icon.entry", entry);
790
791         Evas_Object *button = elm_button_add(parent);
792         elm_object_style_set(button, "editfield_clear");
793         elm_object_part_content_set(layout, "elm.icon.eraser", button);
794
795         if (is_editable)
796                 evas_object_smart_callback_add(button, "clicked", __cal_util_edit_field_eraser_clicked_callback, entry);
797         else {
798                 elm_entry_editable_set(entry, is_editable);
799                 elm_object_signal_emit(layout, "elm,state,rename,hide", "");
800                 elm_object_signal_emit(layout, "elm,state,eraser,hide", "");
801         }
802
803         evas_object_show(layout);
804
805         elm_object_signal_callback_add(layout, "mouse,clicked,1", "*", __cal_util_edit_field_clicked_callback, entry);
806
807         return layout;
808 }
809
810 Evas_Object * cal_util_add_nocontents(Evas_Object *parent, const char *label)
811 {
812         c_retvm_if(!parent, NULL, "parent is null");
813
814         Evas_Object *layout = elm_layout_add(parent);
815         c_retvm_if(!layout, NULL, "layout is null");
816
817         elm_layout_theme_set(layout, "layout", "nocontents", "full");
818
819         if (label && strlen(label))
820                 elm_object_part_text_set(layout, "elm.text", label);
821
822         return layout;
823 }
824
825 Evas_Object * cal_util_add_search_nocontents(Evas_Object *parent, const char *label)
826 {
827         c_retvm_if(!parent, NULL, "parent is null");
828
829         Evas_Object *layout = elm_layout_add(parent);
830         c_retvm_if(!layout, NULL, "layout is null");
831
832         elm_layout_theme_set(layout, "layout", "nocontents", "search");
833
834         if (CAL_STRLEN(label))
835                 elm_object_part_text_set(layout, "elm.text", label);
836
837         return layout;
838 }
839
840 static void __cal_util_popup_response_callback(void *data, Evas_Object *obj, void *event_info)
841 {
842         evas_object_del(data);
843 }
844
845 Evas_Object * cal_util_add_popup(Evas_Object *parent, const char *style, const char *title, const char *desc,
846         void (*callback_func)(void *data, Evas_Object *obj, void *ei), void *data, ...)
847 {
848         c_retvm_if(!parent, NULL, "parent is null");
849
850         Evas_Object *popup = elm_popup_add(parent);
851         c_retvm_if(!popup, NULL, "popup is null");
852
853         if (CAL_STRLEN(style))
854                 elm_object_style_set(popup, style);
855
856         if (data)
857                 evas_object_data_set(popup, "data", data);
858
859         evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
860
861         if (CAL_STRLEN(title))
862                 elm_object_part_text_set(popup, "title,text", title);
863
864         if (CAL_STRLEN(desc))
865                 elm_object_text_set(popup, desc);
866
867         va_list arg;
868         int i = 0;
869         Evas_Object *btn = NULL;
870         const char *label = NULL;
871         char part[16] = {0};
872
873         if (!callback_func)
874                 callback_func = __cal_util_popup_response_callback;
875
876         va_start(arg, data);
877
878         while ((label = va_arg(arg, char*))) {
879                 btn = elm_button_add(popup);
880                 if (!btn) {
881                         ERR("btn is null");
882                         continue;
883                 }
884
885                 elm_object_text_set(btn, label);
886                 snprintf(part, sizeof(part), "button%d", ++i);
887                 elm_object_part_content_set(popup, part, btn);
888                 if (!CAL_STRCMP(label, S_("IDS_COM_BODY_DELETE"))) {
889                         elm_object_style_set(btn, "sweep/delete");
890                         char buffer[128] = {0};
891                         snprintf(buffer, sizeof(buffer), "<font_size=36>%s</font_size>", label);
892                         elm_object_text_set(btn, buffer);
893                 } else {
894                         elm_object_style_set(btn, "popup_button/default");
895                 }
896                 evas_object_smart_callback_add(btn, "clicked", callback_func, popup);
897         }
898
899         va_end(arg);
900
901         if (!i)
902                 elm_popup_timeout_set(popup, 3.0);
903
904         evas_object_show(popup);
905         elm_object_focus_set(popup, EINA_TRUE);
906
907         return popup;
908 }
909
910 Evas_Object * cal_util_add_datetime(Evas_Object *parent, const char *title, const struct tm *tm)
911 {
912         c_retvm_if(!parent, NULL, "parent is null");
913
914         Evas_Object *datetime = NULL;
915
916         if (CAL_STRLEN(title)) {
917
918                 Evas_Object *layout = elm_layout_add(parent);
919                 c_retvm_if(!layout, NULL, "layout is null");
920
921                 Eina_Bool r = elm_layout_file_set(layout, EDJ_FILE, "dialoguegroup/datetime");
922                 c_retvm_if(!r, NULL, "r is null");
923
924                 evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
925
926                 datetime = elm_datetime_add(layout);
927                 c_retvm_if(!datetime, NULL, "datetime is null");
928
929                 elm_object_part_text_set(layout, "elm.text", title);
930                 elm_object_part_content_set(layout, "elm.icon", datetime);
931
932                 elm_datetime_field_limit_set(datetime, ELM_DATETIME_YEAR, 70, 136);
933                 if (tm)
934                         elm_datetime_value_set(datetime, tm);
935
936                 return layout;
937
938         }
939         else {
940                 datetime = elm_datetime_add(parent);
941                 c_retvm_if(!datetime, NULL, "datetime is null");
942
943                 evas_object_size_hint_weight_set(datetime, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
944
945                 elm_datetime_field_limit_set(datetime, ELM_DATETIME_YEAR, 70, 136);
946                 if (tm)
947                         elm_datetime_value_set(datetime, tm);
948
949                 return datetime;
950         }
951 }
952
953 void cal_util_get_week_number_text(const struct tm* tm, char *buffer, int buffer_size)
954 {
955         c_retm_if(!tm, "tm is null");
956         c_retm_if(!buffer, "buffer is null");
957
958         __cal_util_get_time_text_from_format(CAL_UTIL_DATE_FORMAT_16, NULL ,tm, buffer, buffer_size);
959 }
960
961 int cal_util_get_distance(Evas_Coord_Point *s, Evas_Coord_Point *e)
962 {
963         c_retvm_if(!s, -1, "s is null");
964         c_retvm_if(!e, -1, "e is null");
965
966         return (Evas_Coord)sqrt((s->x - e->x) * (s->x - e->x)
967                 + (s->y - e->y) * (s->y - e->y));
968 }
969
970 static Eina_Bool __cal_util_hide_small_information_by_timer(void *user_data)
971 {
972         c_retv_if(!user_data, ECORE_CALLBACK_CANCEL);
973
974         cal_util_hide_small_information((Evas_Object *)user_data);
975
976         return ECORE_CALLBACK_CANCEL;
977 }
978
979 void cal_util_show_small_information(Evas_Object *layout, const char *message, double timeout)
980 {
981         c_ret_if(!layout);
982         c_ret_if(!CAL_STRLEN(message));
983
984         Evas_Object *small_information = elm_object_part_content_get(layout, "sel.swallow.contents");
985
986         if (!small_information) {
987
988                 small_information = elm_layout_add(layout);
989                 c_ret_if(!small_information);
990
991                 elm_object_part_content_set(layout, "sel.swallow.contents", small_information);
992                 evas_object_size_hint_weight_set(small_information, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
993                 evas_object_size_hint_align_set(small_information, EVAS_HINT_FILL, EVAS_HINT_FILL);
994
995                 Eina_Bool ret = elm_layout_theme_set(small_information, "standard", "selectioninfo", "center_text");
996                 c_warn_if(!ret, "elm_layout_theme_set() is failed.");
997
998                 evas_object_show(small_information);
999         }
1000
1001         elm_object_part_text_set(small_information, "elm.text", message);
1002
1003         elm_object_signal_emit(layout, "show,selection,info", "elm");
1004
1005         if (0 < timeout) {
1006                 Ecore_Timer *timer = ecore_timer_add(timeout, __cal_util_hide_small_information_by_timer, layout);
1007                 c_warn_if(!timer, "ecore_timer_add() is failed.");
1008         }
1009 }
1010
1011 void cal_util_hide_small_information(Evas_Object *layout)
1012 {
1013         c_ret_if(!layout);
1014
1015         elm_object_signal_emit(layout, "hide,selection,info", "elm");
1016 }
1017
1018 Evas_Object * cal_util_add_toolbar_button(Evas_Object *naviframe, const char *part, const char *text, Evas_Smart_Cb func, void *data)
1019 {
1020         c_retv_if(!naviframe, NULL);
1021         c_retv_if(!CAL_STRLEN(part), NULL);
1022
1023         Evas_Object *button = elm_button_add(naviframe);
1024         c_retv_if(!button, NULL);
1025
1026         elm_object_style_set(button, "naviframe/toolbar/default");
1027
1028         if (CAL_STRLEN(text))
1029                 elm_object_text_set(button, text);
1030
1031         evas_object_smart_callback_add(button, "clicked", func, data);
1032
1033         Elm_Object_Item *navi_item = elm_naviframe_top_item_get(naviframe);
1034         c_retv_if(!navi_item, NULL);
1035
1036         elm_object_item_part_content_set(navi_item, part, button);
1037
1038         return button;
1039 }
1040
1041 Evas_Object * cal_util_add_toolbar_button2(Evas_Object* naviframe, const char* part, const char* filename, Evas_Smart_Cb func, void* data)
1042 {
1043         c_retv_if(!naviframe, NULL);
1044         c_retv_if(!CAL_STRLEN(part), NULL);
1045
1046         Evas_Object* button = elm_button_add(naviframe);
1047         c_retv_if(!button, NULL);
1048
1049         elm_object_style_set(button, "naviframe/title_icon");
1050
1051         Evas_Object* icon = elm_icon_add(naviframe);
1052         elm_icon_file_set(icon, CAL_IMAGES_EDJ, filename);
1053
1054         evas_object_size_hint_aspect_set(icon, EVAS_ASPECT_CONTROL_VERTICAL, 1, 1);
1055         elm_icon_resizable_set(icon, EINA_TRUE, EINA_TRUE);
1056
1057         elm_object_part_content_set(button, "icon", icon);
1058
1059         evas_object_smart_callback_add(button, "clicked", func, data);
1060
1061         Elm_Object_Item *navi_item = elm_naviframe_top_item_get(naviframe);
1062         c_retv_if(!navi_item, NULL);
1063
1064         elm_object_item_part_content_set(navi_item, part, button);
1065
1066         return button;
1067 }
1068
1069 int cal_util_get_default_first_day_of_week()
1070 {
1071         UErrorCode status = U_ZERO_ERROR;
1072         UChar utf16_timezone[64] = {0};
1073
1074         char *locale = vconf_get_str(VCONFKEY_REGIONFORMAT);
1075
1076         if (CAL_STRLEN(locale)) {
1077                 uloc_setDefault(locale, &status);
1078                 free(locale);
1079         }
1080
1081         u_uastrncpy(utf16_timezone, _timezone, sizeof(_timezone));
1082
1083         UCalendar *cal = ucal_open(utf16_timezone, u_strlen(utf16_timezone), uloc_getDefault(), UCAL_TRADITIONAL, &status);
1084         c_retvm_if(!cal, -1, "cal is null");
1085
1086         int first_day_of_week = 0;
1087
1088         first_day_of_week = ucal_getAttribute(cal, UCAL_FIRST_DAY_OF_WEEK);
1089
1090         ucal_close(cal);
1091
1092         return first_day_of_week;
1093 }