2.0 alpha
[apps/home/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://www.tizenopensource.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
30 static UDateTimePatternGenerator *pattern_generator = NULL;
31 static pthread_mutex_t mutex_lock = PTHREAD_MUTEX_INITIALIZER;
32 static UErrorCode status = U_ZERO_ERROR;
33 static int pattern_generator_reference_counter = 0;
34
35 static const char *_icons[] = {
36         [CI_EDIT] = "01_navi_icon_edit.png",
37         [CI_NEW] = "01_navi_icon_new.png",
38         [CI_DEL] = "01_navi_icon_delete.png",
39         [CI_PHOTO] = "00_list_photo_default.png",
40 };
41 static char _partbuf[1024];
42 static char _textbuf[1024];
43 static char _sigbuf[1024];
44 static char _timezone[32];
45
46 Evas_Object* cal_util_add_bg(Evas_Object *obj, Eina_Bool is_window)
47 {
48         CAL_FN_START;
49
50         Evas_Object *bg;
51         CAL_ASSERT(obj);
52         bg = elm_bg_add(obj);
53         if (!bg)
54                 return NULL;
55
56         evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
57
58         if (is_window)
59                 elm_win_resize_object_add(obj, bg);
60         else
61                 elm_object_style_set(bg, "group_list");
62
63         evas_object_show(bg);
64
65         CAL_FN_END;
66
67         return bg;
68 }
69
70 Evas_Object* cal_util_add_layout(Evas_Object *win, const char *g)
71 {
72         c_retvm_if(!win, NULL, "win is null");
73
74         Eina_Bool r;
75         Evas_Object *eo = elm_layout_add(win);
76         c_retvm_if(!eo, NULL, "elm_layout_add returned null");
77
78         if (g) {
79                 r = elm_layout_file_set(eo, EDJ_FILE, g);
80                 c_warn_if(r == EINA_FALSE, "elm_layout_file_set is failed");
81         }
82         else {
83                 r = elm_layout_theme_set(eo, "layout", "application", "default");
84                 c_warn_if(r == EINA_FALSE, "elm_layout_theme_set is failed");
85         }
86
87         if (r == EINA_FALSE) {
88                 evas_object_del(eo);
89                 return NULL;
90         }
91
92         evas_object_size_hint_weight_set(eo, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
93         evas_object_show(eo);
94
95         return eo;
96 }
97
98 Evas_Object* cal_util_add_rectangle(Evas_Object *p)
99 {
100         Evas_Object *r;
101
102         r = evas_object_rectangle_add(evas_object_evas_get(p));
103         CAL_ASSERT(r);
104
105         evas_object_show(r);
106         return r;
107 }
108
109 static void __cal_util_delete_window(void *data, Evas_Object *obj, void *event)
110 {
111         elm_exit();
112 }
113
114 Evas_Object* cal_util_add_window(const char *name, Evas_Coord* w, Evas_Coord* h)
115 {
116         CAL_FN_START;
117
118         Evas_Object *eo = elm_win_add(NULL, name, ELM_WIN_BASIC);
119         c_retvm_if(!eo, NULL, "elm_win_add returned null");
120
121         Evas_Coord width, height;
122
123         elm_win_title_set(eo, name);
124
125         /*For calendar ring, window delete callback is not needed.*/
126         if (strcmp(name, CALENDAR_RING))
127                 evas_object_smart_callback_add(eo, "delete,request", __cal_util_delete_window, NULL);
128
129         ecore_x_window_size_get(ecore_x_window_root_first_get(), &width, &height);
130         evas_object_resize(eo, width, height);
131
132         elm_win_conformant_set(eo, EINA_TRUE);
133         elm_win_indicator_mode_set(eo, ELM_WIN_INDICATOR_SHOW);
134
135         if (w && h) {
136                 *w = width;
137                 *h = height;
138         }
139
140         CAL_FN_END;
141
142         return eo;
143 }
144
145 Evas_Object* cal_util_add_icon(Evas_Object *p, enum cld_icon i)
146 {
147         Eina_Bool r;
148         Evas_Object *ic;
149
150         CAL_ASSERT(p);
151
152         if (i < 0 || i >= CI_MAX)
153                 return NULL;
154
155         ic = elm_icon_add(p);
156         if (!ic)
157                 return NULL;
158
159         r = elm_icon_file_set(ic, EDJ_FILE, _icons[i]);
160         if (!r) {
161                 evas_object_del(ic);
162                 return NULL;
163         }
164
165         evas_object_size_hint_align_set(ic, 0.5, 0.5);
166
167         return ic;
168 }
169
170 Evas_Object* cal_util_add_icon_button(Evas_Object *p, enum cld_icon i)
171 {
172         Evas_Object *ic;
173         Evas_Object *bt;
174
175         CAL_ASSERT(p);
176
177         ic = cal_util_add_icon(p, i);
178         if (!ic)
179                 return NULL;
180
181         evas_object_size_hint_min_set(ic, 34, 34);
182
183         bt = elm_button_add(p);
184         if (!bt) {
185                 evas_object_del(ic);
186                 return NULL;
187         }
188
189         evas_object_size_hint_min_set(bt, 50, 50);
190
191         elm_object_content_set(bt, ic);
192
193         return bt;
194 }
195
196 char* cal_util_get_part_text(const char *fmt, int pos)
197 {
198         snprintf(_partbuf, sizeof(_partbuf), fmt, pos);
199         return _partbuf;
200 }
201
202
203 void cal_util_set_text(Evas_Object *obj, const char *part, const char *fmt, ...)
204 {
205         va_list ap;
206
207         va_start(ap, fmt);
208         vsnprintf(_textbuf, sizeof(_textbuf), fmt, ap);
209         va_end(ap);
210
211         edje_object_part_text_set(obj, part, _textbuf);
212 }
213
214 int cal_util_connect_pattern_generator()
215 {
216         pthread_mutex_lock(&mutex_lock);
217         if(pattern_generator_reference_counter == 0)
218         {
219                 uloc_setDefault(getenv("LC_TIME"), &status);
220                 if(!pattern_generator)
221                         pattern_generator = udatpg_open(uloc_getDefault(), &status);
222                 if(!pattern_generator)
223                 {
224                         ERR("udatpg_open fail : %s", u_errorName(status));
225                         pthread_mutex_unlock(&mutex_lock);
226                         return -1;
227                 }
228         }
229
230         pattern_generator_reference_counter++;
231         pthread_mutex_unlock(&mutex_lock);
232
233         return 0;
234 }
235
236 int cal_util_disconnect_pattern_generator()
237 {
238         pthread_mutex_lock(&mutex_lock);
239
240         if(pattern_generator_reference_counter == 1)
241         {
242                 if(pattern_generator)
243                         udatpg_close(pattern_generator);
244                 pattern_generator = NULL;
245         }
246
247         pattern_generator_reference_counter--;
248
249         pthread_mutex_unlock(&mutex_lock);
250
251         return 0;
252 }
253
254 void cal_util_set_timezone(const char *timezone)
255 {
256         c_ret_if(!timezone);
257
258         snprintf(_timezone, sizeof(_timezone), "%s", timezone);
259 }
260
261 void cal_util_initialize_timezone()
262 {
263         int is_lock_timezone = 0;
264
265         int ret = vconf_get_int(CAL_VCONFKEY_LOCK_TIMEZONE_OFFSET, &is_lock_timezone);
266         c_ret_if(ret);
267
268         char *text = NULL;
269
270         if (is_lock_timezone)
271                 text = vconf_get_str(CAL_VCONFKEY_LOCK_TIMEZONE_PATH);
272         else
273                 text = vconf_get_str(VCONFKEY_SETAPPL_TIMEZONE_ID);
274
275         if (CAL_STRLEN(text)) {
276
277                 cal_util_set_timezone(text);
278
279                 free(text);
280         } else
281                 ERR("vconf_get_str is failed.");
282 }
283
284 void cal_util_convert_lli_to_tm(const char *timezone, long long int lli, struct tm *tm)
285 {
286         c_ret_if(!tm);
287
288         UErrorCode status = U_ZERO_ERROR;
289         UChar utf16_timezone[64] = {0};
290
291         if (timezone)
292                 u_uastrncpy(utf16_timezone, timezone, 64);
293         else
294                 u_uastrncpy(utf16_timezone, _timezone, sizeof(_timezone));
295
296         UCalendar *cal = ucal_open(utf16_timezone, u_strlen(utf16_timezone), uloc_getDefault(), UCAL_TRADITIONAL, &status);
297         c_ret_if(!cal);
298
299         ucal_setMillis(cal, (double)(lli* 1000.0), &status);
300
301         tm->tm_year = ucal_get(cal, UCAL_YEAR, &status) - 1900;
302         tm->tm_mon = ucal_get(cal, UCAL_MONTH, &status);
303         tm->tm_mday = ucal_get(cal, UCAL_DATE, &status);
304
305         if (ucal_get(cal, UCAL_AM_PM, &status))
306                 tm->tm_hour = ucal_get(cal, UCAL_HOUR, &status) + 12;
307         else
308                 tm->tm_hour = ucal_get(cal, UCAL_HOUR, &status);
309
310         tm->tm_min = ucal_get(cal, UCAL_MINUTE, &status);
311         tm->tm_sec = ucal_get(cal, UCAL_SECOND, &status);
312         tm->tm_isdst = ucal_get(cal, UCAL_DST_OFFSET, &status);
313         tm->tm_wday = ucal_get(cal, UCAL_DAY_OF_WEEK, &status) - 1;
314         tm->tm_yday = ucal_get(cal, UCAL_DAY_OF_YEAR, &status) - 1;
315
316         ucal_close(cal);
317 }
318
319 void cal_util_convert_tm_to_lli(const char *timezone, struct tm *tm, long long int *lli)
320 {
321         c_ret_if(!tm);
322         c_ret_if(!lli);
323
324         UErrorCode status = U_ZERO_ERROR;
325         UChar utf16_timezone[64] = {0};
326
327         if (timezone)
328                 u_uastrncpy(utf16_timezone, timezone, 64);
329         else
330                 u_uastrncpy(utf16_timezone, _timezone, sizeof(_timezone));
331
332         UCalendar *cal = ucal_open(utf16_timezone, u_strlen(utf16_timezone), uloc_getDefault(), UCAL_TRADITIONAL, &status);
333         c_ret_if(!cal);
334
335         ucal_setAttribute(cal, UCAL_LENIENT, 1);
336         ucal_setDateTime(cal, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, &status);
337
338         UDate millis = ucal_getMillis(cal, &status);
339
340         *lli = (long long int)(millis / 1000);
341
342         ucal_close(cal);
343 }
344
345 void cal_util_convert_lli_to_time_t(const char *timezone, long long int lli, time_t *time)
346 {
347         c_ret_if(!time);
348
349         UErrorCode status = U_ZERO_ERROR;
350         UChar utf16_timezone[64] = {0};
351
352         if (timezone)
353                 u_uastrncpy(utf16_timezone, timezone, 64);
354         else
355                 u_uastrncpy(utf16_timezone, _timezone, sizeof(_timezone));
356
357         UCalendar *cal = ucal_open(utf16_timezone, u_strlen(utf16_timezone), uloc_getDefault(), UCAL_TRADITIONAL, &status);
358         c_ret_if(!cal);
359
360         ucal_setMillis(cal, (double)(lli* 1000.0), &status);
361
362         struct tm tm;
363
364         tm.tm_year = ucal_get(cal, UCAL_YEAR, &status) - 1900;
365         tm.tm_mon = ucal_get(cal, UCAL_MONTH, &status);
366         tm.tm_mday = ucal_get(cal, UCAL_DATE, &status);
367
368         if (ucal_get(cal, UCAL_AM_PM, &status))
369                 tm.tm_hour = ucal_get(cal, UCAL_HOUR, &status) + 12;
370         else
371                 tm.tm_hour = ucal_get(cal, UCAL_HOUR, &status);
372
373         tm.tm_min = ucal_get(cal, UCAL_MINUTE, &status);
374         tm.tm_sec = ucal_get(cal, UCAL_SECOND, &status);
375         tm.tm_isdst = ucal_get(cal, UCAL_DST_OFFSET, &status);
376         tm.tm_wday = ucal_get(cal, UCAL_DAY_OF_WEEK, &status) - 1;
377         tm.tm_yday = ucal_get(cal, UCAL_DAY_OF_YEAR, &status) - 1;
378
379         *time = mktime(&tm);
380
381         ucal_close(cal);
382 }
383
384 void cal_util_convert_time_t_to_lli(const char *timezone, time_t time, long long int *lli)
385 {
386         c_ret_if(!lli);
387
388         UErrorCode status = U_ZERO_ERROR;
389         UChar utf16_timezone[64] = {0};
390
391         if (timezone)
392                 u_uastrncpy(utf16_timezone, timezone, 64);
393         else
394                 u_uastrncpy(utf16_timezone, _timezone, sizeof(_timezone));
395
396         UCalendar *cal = ucal_open(utf16_timezone, u_strlen(utf16_timezone), uloc_getDefault(), UCAL_TRADITIONAL, &status);
397         c_ret_if(!cal);
398
399         ucal_setAttribute(cal, UCAL_LENIENT, 1);
400
401         struct tm tm;
402
403         ucal_setDateTime(cal, tm.tm_year + 1900, tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, &status);
404
405         UDate millis = ucal_getMillis(cal, &status);
406
407         *lli = (long long int)(millis / 1000);
408
409         ucal_close(cal);
410 }
411
412
413 UDate cal_util_get_u_date_from_time_t(time_t time)
414 {
415         UDate date;
416         date = (UDate ) time*1000;  /* Equivalent to Date = ucal_getNow() in Milliseconds */
417         return date;
418 }
419
420 UDate cal_util_get_u_date_from_tm(const struct tm* tm)
421 {
422         UDate date;
423         time_t time;
424
425         time = timelocal((struct tm*)tm);
426
427         date = (UDate ) time*1000;  /* Equivalent to Date = ucal_getNow() in Milliseconds */
428         return date;
429 }
430
431 static void __cal_util_generate_best_pattern(UChar *custom_format, const struct tm* tm, char *buffer, int buffer_size)
432 {
433         UErrorCode status = U_ZERO_ERROR;
434         UDateFormat *formatter;
435         UDate date;
436         UChar bestPattern[64] = {0,};
437         UChar formatted[64] = {0,};
438         char bestPatternString[128]={0,};
439         char formattedString[128] = {0,};
440         int32_t bestPatternCapacity, formattedCapacity;
441         int32_t bestPatternLength, formattedLength;
442         uloc_setDefault(getenv("LC_TIME"), &status);
443         const char* locale = uloc_getDefault();
444
445         bestPatternCapacity = (int32_t)(sizeof(bestPattern)/sizeof((bestPattern)[0]));
446         bestPatternLength = udatpg_getBestPattern(pattern_generator, custom_format, u_strlen(custom_format), bestPattern,   bestPatternCapacity, &status);
447
448         u_austrncpy(bestPatternString, bestPattern, 128);
449
450         date = cal_util_get_u_date_from_tm(tm);
451         formatter = udat_open(UDAT_IGNORE, UDAT_IGNORE, locale, NULL, -1, bestPattern, -1, &status);
452
453         formattedCapacity = (int32_t)(sizeof(formatted)/sizeof((formatted)[0]));
454         formattedLength = udat_format(formatter, date, formatted, formattedCapacity, NULL, &status);
455
456         u_austrncpy(formattedString, formatted, 128);
457
458         udat_close(formatter);
459
460         snprintf(buffer, buffer_size, "%s", formattedString);
461 }
462
463 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)
464 {
465         char format[128]={0};
466         UChar custom_format[64]={0};
467         int date_format_length = 0;
468         int time_format_length = 0;
469         int format_length = 0;
470
471         if (date_format) {
472                 date_format_length = CAL_STRLEN(date_format);
473                 CAL_STRNCPY(format,date_format,date_format_length);
474         }
475
476         if (time_format) {
477                 time_format_length = CAL_STRLEN(time_format);
478                 CAL_STRNCAT(format,time_format,time_format_length);
479         }
480
481         format_length = CAL_STRLEN(format);
482
483         u_uastrncpy(custom_format, format, format_length);
484
485         __cal_util_generate_best_pattern(custom_format, tm, buffer, buffer_size);
486 }
487
488 void cal_util_set_time_text(Evas_Object *obj, const char *part, const char *date, const char* time, const struct tm *tm)
489 {
490         char time_text[128] = {0};
491         __cal_util_get_time_text_from_format(date, time, tm, time_text, sizeof(time_text) - 1);
492
493         edje_object_part_text_set(obj, part, time_text);
494 }
495
496 void cal_util_set_time_week_text(Evas_Object *obj, const char *part, const char *date, const char* time,
497                 const struct tm *t, int start)
498 {
499         c_retm_if(!obj, "obj is null.");
500         c_retm_if(!part, "part is null.");
501         c_retm_if(!t, "t is null.");
502
503         struct tm tm = *t;
504         char week[8] = {0};
505         char start_date[8] = {0};
506         char end_date[8] = {0};
507         char month_year[32] = {0};
508         char time_text[128] = {0};
509
510         __cal_util_get_time_text_from_format(CAL_UTIL_DATE_FORMAT_16, NULL, &tm, week, sizeof(week) - 1);
511
512         tm.tm_mday -= CAL_UTIL_GET_WDAY(tm.tm_wday - start);
513         __cal_util_get_time_text_from_format(CAL_UTIL_DATE_FORMAT_9, NULL, &tm, start_date, sizeof(start_date) - 1);
514
515         __cal_util_get_time_text_from_format(CAL_UTIL_DATE_FORMAT_6, NULL, &tm, month_year, sizeof(month_year) - 1);
516
517         tm.tm_mday += 6;
518         __cal_util_get_time_text_from_format(CAL_UTIL_DATE_FORMAT_9, NULL, &tm, end_date, sizeof(end_date) - 1);
519
520         snprintf(time_text, sizeof(time_text) - 1,"W%s ( %s - %s ) %s", week, start_date, end_date, month_year);
521
522         edje_object_part_text_set(obj, part, time_text);
523 }
524
525 int cal_util_get_time_text(char* buf, int buf_size, const char *date, const char* time, const struct tm *tm)
526 {
527         char time_text[128] = {0};
528
529         __cal_util_get_time_text_from_format(date, time, tm, time_text, sizeof(time_text) - 1);
530
531         snprintf(buf, buf_size, "%s", time_text);
532
533         return CAL_STRLEN(time_text);
534 }
535
536 void cal_util_emit_signal(Evas_Object *obj, const char *fmt, ...)
537 {
538         va_list ap;
539
540         va_start(ap, fmt);
541         vsnprintf(_sigbuf, sizeof(_sigbuf), fmt, ap);
542         va_end(ap);
543
544         edje_object_signal_emit(obj, _sigbuf, "prog");
545 }
546
547 Evas_Object* cal_util_add_separator(Evas_Object *box, const char *style)
548 {
549         Evas_Object *sp;
550
551         if (!box || CAL_STRCMP(elm_object_widget_type_get(box), "box"))
552                 return NULL;
553
554         sp = elm_separator_add(box);
555         if (sp) {
556                 elm_separator_horizontal_set(sp, EINA_TRUE);
557                 elm_object_style_set(sp, style);
558                 evas_object_show(sp);
559                 elm_box_pack_end(box, sp);
560         }
561
562         return sp;
563 }
564
565 Evas_Object* cal_util_add_scroller(Evas_Object *p)
566 {
567         Evas_Object *sc;
568
569         sc = elm_scroller_add(p);
570         if (!sc)
571                 return NULL;
572
573         elm_scroller_bounce_set(sc, EINA_FALSE, EINA_TRUE);
574         elm_scroller_policy_set(sc, ELM_SCROLLER_POLICY_OFF, ELM_SCROLLER_POLICY_AUTO);
575
576         evas_object_show(sc);
577         return sc;
578 }
579
580 Evas_Object* cal_util_add_box(Evas_Object *p)
581 {
582         Evas_Object *bx;
583
584         bx = elm_box_add(p);
585         if (!bx)
586                 return NULL;
587
588         evas_object_size_hint_weight_set(bx, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
589         evas_object_size_hint_align_set(bx, EVAS_HINT_FILL, 0.0);
590         evas_object_show(bx);
591
592         return bx;
593 }
594
595 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)
596 {
597         CAL_ASSERT(btn);
598
599         elm_object_style_set(btn, style);
600         evas_object_size_hint_weight_set(btn, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
601         evas_object_size_hint_align_set(btn, EVAS_HINT_FILL, 0.5);
602         elm_object_text_set(btn, label);
603         evas_object_smart_callback_add(btn, "clicked", func, data);
604         evas_object_show(btn);
605 }
606
607 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.
608 {
609         c_retm_if(!data, "data is null");
610         c_retm_if(!obj, "obj is null");
611
612         if (elm_object_focus_get(data)) {
613                 elm_object_signal_emit(data, "elm,state,eraser,show", "elm");
614         }
615         else {
616                 if (elm_entry_is_empty(obj))
617                         elm_object_signal_emit(data, "elm,state,guidetext,show", "elm");
618                 else
619                         elm_object_signal_emit(data, "elm,state,guidetext,hide", "elm");
620         }
621 }
622
623 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.
624 {
625         c_retm_if(!data, "data is null");
626         c_retm_if(!obj, "obj is null");
627
628         elm_object_signal_emit(data, "elm,state,eraser,show", "elm");
629
630         elm_object_signal_emit(data, "elm,state,guidetext,hide", "elm");
631 }
632
633 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.
634 {
635         c_retm_if(!data, "data is null");
636         c_retm_if(!obj, "obj is null");
637
638         if (elm_entry_is_empty(obj))
639                 elm_object_signal_emit(data, "elm,state,guidetext,show", "elm");
640
641         elm_object_signal_emit(data, "elm,state,eraser,hide", "elm");
642 }
643
644 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.
645 {
646         c_retm_if(!data, "data is null");
647
648         elm_entry_entry_set(data, "");
649 }
650
651 Evas_Object * cal_util_add_edit_field(Evas_Object *parent, const char *title, const char *guide, Eina_Bool single_line, Eina_Bool is_editable)
652 {
653         c_retvm_if(!parent, NULL, "parent is null");
654
655         Evas_Object *layout = elm_layout_add(parent);
656         c_retvm_if(!layout, NULL, "layout is null");
657
658         if (title && title[0] != '\0') {
659                 elm_layout_theme_set(layout, "layout", "editfield", "title");
660                 elm_object_part_text_set(layout, "elm.text", title);
661         }
662         else
663                 elm_layout_theme_set(layout, "layout", "editfield", "default");
664
665         Evas_Object *entry = elm_entry_add(parent);
666         c_retvm_if(!entry, layout, "entry is null");
667
668         elm_object_part_content_set(layout, "elm.swallow.content", entry);
669         evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
670
671         if (guide && guide[0] != '\0')
672                 elm_object_part_text_set(layout, "elm.guidetext", guide);
673
674         elm_entry_single_line_set(entry, single_line);
675         elm_entry_scrollable_set(entry, single_line);
676
677         if (is_editable)
678                 elm_object_signal_callback_add(layout, "elm,eraser,clicked", "elm", __cal_util_edit_field_eraser_clicked_callback, entry);
679         else {
680                 elm_entry_editable_set(entry, is_editable);
681                 elm_object_signal_emit(layout, "elm,state,eraser,hide", "elm");
682         }
683
684         evas_object_smart_callback_add(entry, "changed", __cal_util_edit_field_changed_callback, layout);
685         evas_object_smart_callback_add(entry, "focused", __cal_util_edit_field_focused_callback, layout);
686         evas_object_smart_callback_add(entry, "unfocused", __cal_util_edit_field_unfocused_callback, layout);
687
688         evas_object_show(layout);
689
690         return layout;
691 }
692
693 static void __cal_util_searchbar_changed_callback(void *data, Evas_Object *obj, void *event_info)
694 {
695         c_retm_if(!data, "data is null");
696         c_retm_if(!obj, "obj is null");
697
698         if (elm_object_focus_get(data)) {
699                 elm_object_signal_emit(data, "elm,state,eraser,show", "elm");
700         }
701         else {
702                 if (elm_entry_is_empty(obj))
703                         elm_object_signal_emit(data, "elm,state,guidetext,show", "elm");
704                 else
705                         elm_object_signal_emit(data, "elm,state,guidetext,hide", "elm");
706         }
707 }
708
709 static void __cal_util_searchbar_with_cancel_btn_focused_callback(void *data, Evas_Object *obj, void *event_info)
710 {
711         c_retm_if(!data, "data is null");
712         c_retm_if(!obj, "obj is null");
713
714         if (!elm_entry_is_empty(obj))
715                 elm_object_signal_emit(data, "elm,state,eraser,show", "elm");
716
717         elm_object_signal_emit(data, "elm,state,guidetext,hide", "elm");
718
719         elm_object_signal_emit(data, "cancel,in", "");
720 }
721
722 static void __cal_util_searchbar_focused_callback(void *data, Evas_Object *obj, void *event_info)
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", "elm");
728
729         elm_object_signal_emit(data, "elm,state,guidetext,hide", "elm");
730 }
731
732 static void __cal_util_searchbar_eraser_clicked_callback(void *data, Evas_Object *obj, const char *emission, const char *source) // When X marked button is clicked, empty entry's contents.
733 {
734         c_retm_if(!data, "data is null");
735
736         elm_entry_entry_set(data, "");
737 }
738
739 static void __cal_util_searchbar_cancel_btn_clicked_callback(void *data, Evas_Object *obj, void *event_info)
740 {
741         c_retm_if(!data, "data is null");
742         c_retm_if(!obj, "obj is null");
743
744         Evas_Object *searchbar_layout = data;
745         Evas_Object *entry = elm_object_part_content_get(searchbar_layout, "elm.swallow.content");
746         c_retm_if(!entry, "entry is null");
747
748         evas_object_hide(obj);
749
750         elm_entry_input_panel_hide(entry);
751
752         elm_entry_entry_set(entry, NULL);
753
754         elm_object_focus_set(entry, EINA_FALSE);
755 }
756
757 static void __cal_util_searchbar_unfocused_callback(void *data, Evas_Object *obj, void *event_info)
758 {
759         c_retm_if(!data, "data is null");
760         c_retm_if(!obj, "obj is null");
761
762         if (elm_entry_is_empty(obj))
763                 elm_object_signal_emit(data, "elm,state,guidetext,show", "elm");
764
765         elm_object_signal_emit(data, "elm,state,eraser,hide", "elm");
766
767         elm_object_signal_emit(data, "cancel,out", "");
768 }
769
770 static void __cal_util_searchbar_symbol_clicked_callback(void *data, Evas_Object *obj, const char *emission, const char *source)
771 {
772         c_retm_if(!data, "data is null");
773
774         elm_object_focus_set(data, EINA_TRUE);
775 }
776
777 static void __cal_util_searchbar_bg_clicked_callback(void *data, Evas_Object *obj, const char *emission, const char *source)
778 {
779         c_retm_if(!data, "data is null");
780
781         elm_object_focus_set(data, EINA_TRUE);
782 }
783
784 Evas_Object * cal_util_add_searchbar(Evas_Object *parent, const char *text,  const char *guide, Eina_Bool cancel_button)
785 {
786         c_retvm_if(!parent, NULL, "parent is null");
787
788         Evas_Object *searchbar_layout = elm_layout_add(parent);
789         c_retvm_if(!searchbar_layout, NULL, "searchbar_layout is null");
790
791         if (cancel_button)
792                 elm_layout_theme_set(searchbar_layout, "layout", "searchbar", "cancel_button");
793         else
794                 elm_layout_theme_set(searchbar_layout, "layout", "searchbar", "default");
795
796         Evas_Object *entry = elm_entry_add(searchbar_layout);
797         c_retvm_if(!entry, searchbar_layout, "entry is null");
798
799         evas_object_show(entry);
800
801         elm_entry_scrollable_set(entry, EINA_TRUE);
802         elm_entry_single_line_set(entry, EINA_TRUE);
803         elm_object_part_content_set(searchbar_layout, "elm.swallow.content", entry);
804
805         if (guide && strlen(guide))
806                 elm_object_part_text_set(searchbar_layout, "elm.guidetext", guide);
807         else
808                 elm_object_part_text_set(searchbar_layout, "elm.guidetext", S_("IDS_COM_BODY_SEARCH"));
809
810         if (text && strlen(text))
811                 elm_object_text_set(entry, text);
812
813         if (cancel_button) {
814                 Evas_Object *cancel_btn = elm_button_add(searchbar_layout);
815                 c_retvm_if(!cancel_btn, searchbar_layout, "cancel_btn is null");
816
817                 elm_object_part_content_set(searchbar_layout, "button_cancel", cancel_btn);
818                 elm_object_style_set(cancel_btn, "searchbar/default");
819                 elm_object_text_set(cancel_btn, S_("IDS_COM_SK_CANCEL"));
820
821                 evas_object_smart_callback_add(entry, "focused", __cal_util_searchbar_with_cancel_btn_focused_callback, searchbar_layout);
822                 evas_object_smart_callback_add(cancel_btn, "clicked", __cal_util_searchbar_cancel_btn_clicked_callback, searchbar_layout);
823
824         }
825         else {
826                 evas_object_smart_callback_add(entry, "focused", __cal_util_searchbar_focused_callback, searchbar_layout);
827         }
828
829         evas_object_smart_callback_add(entry, "changed", __cal_util_searchbar_changed_callback, searchbar_layout);
830         evas_object_smart_callback_add(entry, "unfocused", __cal_util_searchbar_unfocused_callback, searchbar_layout);
831         elm_object_signal_callback_add(searchbar_layout, "elm,eraser,clicked", "elm", __cal_util_searchbar_eraser_clicked_callback, entry);
832         elm_object_signal_callback_add(searchbar_layout, "elm,bg,clicked", "elm", __cal_util_searchbar_bg_clicked_callback, entry);
833         elm_object_signal_callback_add(searchbar_layout, "elm,action,click", "", __cal_util_searchbar_symbol_clicked_callback, entry);
834
835         return searchbar_layout;
836 }
837
838 Evas_Object * cal_util_add_nocontents(Evas_Object *parent, const char *label)
839 {
840         c_retvm_if(!parent, NULL, "parent is null");
841
842         Evas_Object *layout = elm_layout_add(parent);
843         c_retvm_if(!layout, NULL, "layout is null");
844
845         elm_layout_theme_set(layout, "layout", "nocontents", "full");
846
847         if (label && strlen(label))
848                 elm_object_part_text_set(layout, "elm.text", label);
849
850         return layout;
851 }
852
853 Evas_Object * cal_util_add_controlbar(Evas_Object * parent)
854 {
855         c_retvm_if(!parent, NULL, "parent is null");
856
857         Evas_Object *toolbar = elm_toolbar_add(parent);
858         c_retvm_if(!toolbar, NULL, "toolbar is null");
859
860         elm_toolbar_shrink_mode_set(toolbar, ELM_TOOLBAR_SHRINK_EXPAND);
861
862         return toolbar;
863 }
864
865 static void __cal_util_popup_response_callback(void *data, Evas_Object *obj, void *event_info)
866 {
867         evas_object_del(data);
868 }
869
870 Evas_Object * cal_util_add_popup(Evas_Object *parent, const char *style, const char *title, const char *desc,
871         void (*callback_func)(void *data, Evas_Object *obj, void *ei), void *data, ...)
872 {
873         c_retvm_if(!parent, NULL, "parent is null");
874
875         Evas_Object *popup = elm_popup_add(parent);
876         c_retvm_if(!popup, NULL, "popup is null");
877
878         if (CAL_STRLEN(style))
879                 elm_object_style_set(popup, style);
880
881         if (data)
882                 evas_object_data_set(popup, "data", data);
883
884         evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
885
886         if (CAL_STRLEN(title))
887                 elm_object_part_text_set(popup, "title,text", title);
888
889         if (CAL_STRLEN(desc))
890                 elm_object_text_set(popup, desc);
891
892         va_list arg;
893         int i = 0;
894         Evas_Object *btn = NULL;
895         const char *label = NULL;
896         char part[16] = {0};
897
898         if (!callback_func)
899                 callback_func = __cal_util_popup_response_callback;
900
901         va_start(arg, data);
902
903         while ((label = va_arg(arg, char*))) {
904                 btn = elm_button_add(popup);
905                 if (!btn) {
906                         ERR("btn is null");
907                         continue;
908                 }
909
910                 elm_object_text_set(btn, label);
911                 snprintf(part, sizeof(part), "button%d", ++i);
912                 elm_object_part_content_set(popup, part, btn);
913                 evas_object_smart_callback_add(btn, "clicked", callback_func, popup);
914         }
915
916         va_end(arg);
917
918         if (!i)
919                 elm_popup_timeout_set(popup, 3.0);
920
921         evas_object_show(popup);
922
923         return popup;
924 }
925
926 Evas_Object * cal_util_add_datetime(Evas_Object *parent, const char *title, const struct tm *tm)
927 {
928         c_retvm_if(!parent, NULL, "parent is null");
929
930         Evas_Object *datetime = NULL;
931
932         if (CAL_STRLEN(title)) {
933
934                 Evas_Object *layout = elm_layout_add(parent);
935                 c_retvm_if(!layout, NULL, "layout is null");
936
937                 Eina_Bool r = elm_layout_file_set(layout, EDJ_FILE, "dialoguegroup/datetime");
938                 c_retvm_if(!r, NULL, "r is null");
939
940                 evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
941
942                 datetime = elm_datetime_add(layout);
943                 c_retvm_if(!datetime, NULL, "datetime is null");
944
945                 elm_object_part_text_set(layout, "elm.text", title);
946
947                 if (tm)
948                         elm_datetime_value_set(datetime, tm);
949
950                 elm_object_part_content_set(layout, "elm.icon", datetime);
951
952                 return layout;
953
954         }
955         else {
956                 datetime = elm_datetime_add(parent);
957                 c_retvm_if(!datetime, NULL, "datetime is null");
958
959                 evas_object_size_hint_weight_set(datetime, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
960
961                 if (tm)
962                         elm_datetime_value_set(datetime, tm);
963
964                 return datetime;
965         }
966 }
967
968 void cal_util_get_week_number_text(const struct tm* tm, char *buffer, int buffer_size)
969 {
970         c_retm_if(!tm, "tm is null");
971         c_retm_if(!buffer, "buffer is null");
972
973         __cal_util_get_time_text_from_format(CAL_UTIL_DATE_FORMAT_16, NULL ,tm, buffer, buffer_size);
974 }
975
976 int cal_util_get_distance(Evas_Coord_Point *s, Evas_Coord_Point *e)
977 {
978         c_retvm_if(!s, -1, "s is null");
979         c_retvm_if(!e, -1, "e is null");
980
981         return (Evas_Coord)sqrt((s->x - e->x) * (s->x - e->x)
982                 + (s->y - e->y) * (s->y - e->y));
983 }
984