[misc] Add tag & release pkg
[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         CAL_FN_END;
170
171         return eo;
172 }
173
174 char* cal_util_get_part_text(const char *fmt, int pos)
175 {
176         snprintf(_partbuf, sizeof(_partbuf), fmt, pos);
177         return _partbuf;
178 }
179
180
181 void cal_util_set_text(Evas_Object *obj, const char *part, const char *fmt, ...)
182 {
183         va_list ap;
184
185         va_start(ap, fmt);
186         vsnprintf(_textbuf, sizeof(_textbuf), fmt, ap);
187         va_end(ap);
188
189         edje_object_part_text_set(obj, part, _textbuf);
190 }
191
192 int cal_util_connect_pattern_generator()
193 {
194         CAL_FN_START;
195
196         pthread_mutex_lock(&mutex_lock);
197         if(pattern_generator_reference_counter == 0)
198         {
199                 uloc_setDefault(getenv("LC_TIME"), &status);
200                 if(!pattern_generator)
201                         pattern_generator = udatpg_open(uloc_getDefault(), &status);
202                 if(!pattern_generator)
203                 {
204                         ERR("udatpg_open fail : %s", u_errorName(status));
205                         pthread_mutex_unlock(&mutex_lock);
206                         return -1;
207                 }
208         }
209
210         pattern_generator_reference_counter++;
211         pthread_mutex_unlock(&mutex_lock);
212
213         CAL_FN_END;
214
215         return 0;
216 }
217
218 int cal_util_disconnect_pattern_generator()
219 {
220         pthread_mutex_lock(&mutex_lock);
221
222         if(pattern_generator_reference_counter == 1)
223         {
224                 if(pattern_generator)
225                         udatpg_close(pattern_generator);
226                 pattern_generator = NULL;
227         }
228
229         pattern_generator_reference_counter--;
230
231         pthread_mutex_unlock(&mutex_lock);
232
233         return 0;
234 }
235
236 void cal_util_set_timezone(const char *timezone)
237 {
238         c_ret_if(!timezone);
239
240         snprintf(_timezone, sizeof(_timezone), "%s", timezone);
241 }
242
243 void cal_util_initialize_timezone()
244 {
245         CAL_FN_START;
246
247         int is_lock_timezone = 0;
248
249         int ret = vconf_get_int(CAL_VCONFKEY_LOCK_TIMEZONE_ON_OFF, &is_lock_timezone);
250         c_ret_if(ret);
251
252         char *text = NULL;
253
254         if (is_lock_timezone)
255                 text = vconf_get_str(CAL_VCONFKEY_LOCK_TIMEZONE_PATH);
256         else
257                 text = vconf_get_str(VCONFKEY_SETAPPL_TIMEZONE_ID);
258
259         if (CAL_STRLEN(text)) {
260
261                 cal_util_set_timezone(text);
262
263                 free(text);
264         } else
265                 ERR("vconf_get_str is failed.");
266
267         CAL_FN_END;
268 }
269
270 void cal_util_convert_lli_to_tm(const char *timezone, long long int lli, struct tm *tm)
271 {
272         c_ret_if(!tm);
273
274         UErrorCode status = U_ZERO_ERROR;
275         UChar utf16_timezone[64] = {0};
276
277         if (timezone)
278                 u_uastrncpy(utf16_timezone, timezone, 64);
279         else
280                 u_uastrncpy(utf16_timezone, _timezone, sizeof(_timezone));
281
282         UCalendar *cal = ucal_open(utf16_timezone, u_strlen(utf16_timezone), uloc_getDefault(), UCAL_TRADITIONAL, &status);
283         c_ret_if(!cal);
284
285         ucal_setMillis(cal, (double)(lli* 1000.0), &status);
286
287         tm->tm_year = ucal_get(cal, UCAL_YEAR, &status) - 1900;
288         tm->tm_mon = ucal_get(cal, UCAL_MONTH, &status);
289         tm->tm_mday = ucal_get(cal, UCAL_DATE, &status);
290
291         if (ucal_get(cal, UCAL_AM_PM, &status))
292                 tm->tm_hour = ucal_get(cal, UCAL_HOUR, &status) + 12;
293         else
294                 tm->tm_hour = ucal_get(cal, UCAL_HOUR, &status);
295
296         tm->tm_min = ucal_get(cal, UCAL_MINUTE, &status);
297         tm->tm_sec = ucal_get(cal, UCAL_SECOND, &status);
298         tm->tm_isdst = ucal_get(cal, UCAL_DST_OFFSET, &status);
299         tm->tm_wday = ucal_get(cal, UCAL_DAY_OF_WEEK, &status) - 1;
300         tm->tm_yday = ucal_get(cal, UCAL_DAY_OF_YEAR, &status) - 1;
301
302         ucal_close(cal);
303 }
304
305 void cal_util_convert_tm_to_lli(const char *timezone, const struct tm *tm, long long int *lli)
306 {
307         c_ret_if(!tm);
308         c_ret_if(!lli);
309
310         UErrorCode status = U_ZERO_ERROR;
311         UChar utf16_timezone[64] = {0};
312
313         if (timezone)
314                 u_uastrncpy(utf16_timezone, timezone, 64);
315         else
316                 u_uastrncpy(utf16_timezone, _timezone, sizeof(_timezone));
317
318         UCalendar *cal = ucal_open(utf16_timezone, u_strlen(utf16_timezone), uloc_getDefault(), UCAL_TRADITIONAL, &status);
319         c_ret_if(!cal);
320
321         ucal_setAttribute(cal, UCAL_LENIENT, 1);
322         ucal_setDateTime(cal, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, &status);
323
324         UDate millis = ucal_getMillis(cal, &status);
325
326         *lli = (long long int)(millis / 1000);
327
328         ucal_close(cal);
329 }
330
331 void cal_util_convert_lli_to_time_t(const char *timezone, long long int lli, time_t *time)
332 {
333         c_ret_if(!time);
334
335         UErrorCode status = U_ZERO_ERROR;
336         UChar utf16_timezone[64] = {0};
337
338         if (timezone)
339                 u_uastrncpy(utf16_timezone, timezone, 64);
340         else
341                 u_uastrncpy(utf16_timezone, _timezone, sizeof(_timezone));
342
343         UCalendar *cal = ucal_open(utf16_timezone, u_strlen(utf16_timezone), uloc_getDefault(), UCAL_TRADITIONAL, &status);
344         c_ret_if(!cal);
345
346         ucal_setMillis(cal, (double)(lli* 1000.0), &status);
347
348         struct tm tm;
349
350         tm.tm_year = ucal_get(cal, UCAL_YEAR, &status) - 1900;
351         tm.tm_mon = ucal_get(cal, UCAL_MONTH, &status);
352         tm.tm_mday = ucal_get(cal, UCAL_DATE, &status);
353
354         if (ucal_get(cal, UCAL_AM_PM, &status))
355                 tm.tm_hour = ucal_get(cal, UCAL_HOUR, &status) + 12;
356         else
357                 tm.tm_hour = ucal_get(cal, UCAL_HOUR, &status);
358
359         tm.tm_min = ucal_get(cal, UCAL_MINUTE, &status);
360         tm.tm_sec = ucal_get(cal, UCAL_SECOND, &status);
361         tm.tm_isdst = ucal_get(cal, UCAL_DST_OFFSET, &status);
362         tm.tm_wday = ucal_get(cal, UCAL_DAY_OF_WEEK, &status) - 1;
363         tm.tm_yday = ucal_get(cal, UCAL_DAY_OF_YEAR, &status) - 1;
364
365         *time = mktime(&tm);
366
367         ucal_close(cal);
368 }
369
370 void cal_util_convert_time_t_to_lli(const char *timezone, time_t time, long long int *lli)
371 {
372         c_ret_if(!lli);
373
374         UErrorCode status = U_ZERO_ERROR;
375         UChar utf16_timezone[64] = {0};
376
377         if (timezone)
378                 u_uastrncpy(utf16_timezone, timezone, 64);
379         else
380                 u_uastrncpy(utf16_timezone, _timezone, sizeof(_timezone));
381
382         UCalendar *cal = ucal_open(utf16_timezone, u_strlen(utf16_timezone), uloc_getDefault(), UCAL_TRADITIONAL, &status);
383         c_ret_if(!cal);
384
385         ucal_setAttribute(cal, UCAL_LENIENT, 1);
386
387         struct tm tm;
388
389         ucal_setDateTime(cal, tm.tm_year + 1900, tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, &status);
390
391         UDate millis = ucal_getMillis(cal, &status);
392
393         *lli = (long long int)(millis / 1000);
394
395         ucal_close(cal);
396 }
397
398
399 UDate cal_util_get_u_date_from_time_t(time_t time)
400 {
401         UDate date;
402         date = (UDate ) time*1000;  /* Equivalent to Date = ucal_getNow() in Milliseconds */
403         return date;
404 }
405
406 UDate cal_util_get_u_date_from_tm(const struct tm* tm)
407 {
408         UDate date;
409         time_t time;
410
411         time = timelocal((struct tm*)tm);
412
413         date = (UDate ) time*1000;  /* Equivalent to Date = ucal_getNow() in Milliseconds */
414         return date;
415 }
416
417 static void __cal_util_generate_best_pattern(UChar *custom_format, const struct tm* tm, char *buffer, int buffer_size)
418 {
419         UErrorCode status = U_ZERO_ERROR;
420         UDateFormat *formatter;
421         UDate date;
422         UChar bestPattern[64] = {0,};
423         UChar formatted[64] = {0,};
424         char bestPatternString[128]={0,};
425         char formattedString[128] = {0,};
426         int32_t bestPatternCapacity, formattedCapacity;
427         int32_t bestPatternLength, formattedLength;
428         uloc_setDefault(getenv("LC_TIME"), &status);
429         const char* locale = uloc_getDefault();
430
431         bestPatternCapacity = (int32_t)(sizeof(bestPattern)/sizeof((bestPattern)[0]));
432         bestPatternLength = udatpg_getBestPattern(pattern_generator, custom_format, u_strlen(custom_format), bestPattern,   bestPatternCapacity, &status);
433
434         u_austrncpy(bestPatternString, bestPattern, 128);
435
436         date = cal_util_get_u_date_from_tm(tm);
437         formatter = udat_open(UDAT_IGNORE, UDAT_IGNORE, locale, NULL, -1, bestPattern, -1, &status);
438
439         formattedCapacity = (int32_t)(sizeof(formatted)/sizeof((formatted)[0]));
440         formattedLength = udat_format(formatter, date, formatted, formattedCapacity, NULL, &status);
441
442         u_austrncpy(formattedString, formatted, 128);
443
444         udat_close(formatter);
445
446         snprintf(buffer, buffer_size, "%s", formattedString);
447 }
448
449 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)
450 {
451         char format[128]={0};
452         UChar custom_format[64]={0};
453         int date_format_length = 0;
454         int time_format_length = 0;
455         int format_length = 0;
456
457         if (date_format) {
458                 date_format_length = CAL_STRLEN(date_format);
459                 CAL_STRNCPY(format,date_format,date_format_length);
460         }
461
462         if (time_format) {
463                 time_format_length = CAL_STRLEN(time_format);
464                 CAL_STRNCAT(format,time_format,time_format_length);
465         }
466
467         format_length = CAL_STRLEN(format);
468
469         u_uastrncpy(custom_format, format, format_length);
470
471         __cal_util_generate_best_pattern(custom_format, tm, buffer, buffer_size);
472 }
473
474 void cal_util_set_time_text(Evas_Object *obj, const char *part, const char *date, const char* time, const struct tm *tm)
475 {
476         char time_text[128] = {0};
477         __cal_util_get_time_text_from_format(date, time, tm, time_text, sizeof(time_text) - 1);
478
479         edje_object_part_text_set(obj, part, time_text);
480 }
481
482 void cal_util_set_item_time_text(Elm_Object_Item *item, 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         elm_object_item_part_text_set(item, part, time_text);
488 }
489
490
491 static void __cal_util_generate_best_pattern_toupper(UChar *custom_format, const struct tm* tm, char *buffer, int buffer_size)
492 {
493         UErrorCode status = U_ZERO_ERROR;
494         UDateFormat *formatter;
495         UDate date;
496         UChar bestPattern[64] = {0,};
497         UChar formatted[64] = {0,};
498         UChar formatted_upper[64] = {0,};
499         char bestPatternString[128]={0,};
500         char formattedString[128] = {0,};
501         int32_t bestPatternCapacity, formattedCapacity;
502         int32_t bestPatternLength, formattedLength;
503         uloc_setDefault(getenv("LC_TIME"), &status);
504         const char* locale = uloc_getDefault();
505
506         bestPatternCapacity = (int32_t)(sizeof(bestPattern)/sizeof((bestPattern)[0]));
507         bestPatternLength = udatpg_getBestPattern(pattern_generator, custom_format, u_strlen(custom_format), bestPattern,   bestPatternCapacity, &status);
508
509         u_austrncpy(bestPatternString, bestPattern, 128);
510
511         date = cal_util_get_u_date_from_tm(tm);
512         formatter = udat_open(UDAT_IGNORE, UDAT_IGNORE, locale, NULL, -1, bestPattern, -1, &status);
513
514         formattedCapacity = (int32_t)(sizeof(formatted)/sizeof((formatted)[0]));
515         formattedLength = udat_format(formatter, date, formatted, formattedCapacity, NULL, &status);
516
517         u_strToUpper(formatted_upper, 64, formatted, 64, "", &status);
518
519         u_austrncpy(formattedString, formatted_upper, 128);
520
521         udat_close(formatter);
522
523         snprintf(buffer, buffer_size, "%s", formattedString);
524 }
525
526 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)
527 {
528         char format[128]={0};
529         UChar custom_format[64]={0};
530         int date_format_length = 0;
531         int time_format_length = 0;
532         int format_length = 0;
533
534         if (date_format) {
535                 date_format_length = CAL_STRLEN(date_format);
536                 CAL_STRNCPY(format,date_format,date_format_length);
537         }
538
539         if (time_format) {
540                 time_format_length = CAL_STRLEN(time_format);
541                 CAL_STRNCAT(format,time_format,time_format_length);
542         }
543
544         format_length = CAL_STRLEN(format);
545
546         u_uastrncpy(custom_format, format, format_length);
547
548         __cal_util_generate_best_pattern_toupper(custom_format, tm, buffer, buffer_size);
549 }
550
551 void cal_util_set_time_text_toupper(Evas_Object *obj, const char *part, const char *date, const char* time, const struct tm *tm)
552 {
553         char time_text[128] = {0};
554         __cal_util_get_time_text_from_format_toupper(date, time, tm, time_text, sizeof(time_text) - 1);
555
556         edje_object_part_text_set(obj, part, time_text);
557 }
558
559 void cal_util_set_time_week_text(Evas_Object *obj, const char *part, const char *date, const char* time,
560                 const struct tm *t, int start)
561 {
562         c_retm_if(!obj, "obj is null.");
563         c_retm_if(!part, "part is null.");
564         c_retm_if(!t, "t is null.");
565
566         struct tm tm = *t;
567         char week[8] = {0};
568         char start_date[8] = {0};
569         char end_date[8] = {0};
570         char month_year[32] = {0};
571         char time_text[128] = {0};
572
573         __cal_util_get_time_text_from_format(CAL_UTIL_DATE_FORMAT_16, NULL, &tm, week, sizeof(week) - 1);
574
575         tm.tm_mday -= CAL_UTIL_GET_WDAY(tm.tm_wday - start);
576         __cal_util_get_time_text_from_format(CAL_UTIL_DATE_FORMAT_9, NULL, &tm, start_date, sizeof(start_date) - 1);
577
578         __cal_util_get_time_text_from_format(CAL_UTIL_DATE_FORMAT_6, NULL, &tm, month_year, sizeof(month_year) - 1);
579
580         tm.tm_mday += 6;
581         __cal_util_get_time_text_from_format(CAL_UTIL_DATE_FORMAT_9, NULL, &tm, end_date, sizeof(end_date) - 1);
582
583         snprintf(time_text, sizeof(time_text) - 1,"W%s(%s-%s) %s", week, start_date, end_date, month_year);
584
585         edje_object_part_text_set(obj, part, time_text);
586 }
587
588 void cal_util_set_item_time_week_text(Elm_Object_Item *item, const char *part, const char *date, const char* time,
589                 const struct tm *t, int start)
590 {
591         c_retm_if(!item, "item is null.");
592         c_retm_if(!part, "part is null.");
593         c_retm_if(!t, "t is null.");
594
595         struct tm tm = *t;
596         char week[8] = {0};
597         char start_date[8] = {0};
598         char end_date[8] = {0};
599         char month_year[32] = {0};
600         char time_text[128] = {0};
601
602         __cal_util_get_time_text_from_format(CAL_UTIL_DATE_FORMAT_16, NULL, &tm, week, sizeof(week) - 1);
603
604         tm.tm_mday -= CAL_UTIL_GET_WDAY(tm.tm_wday - start);
605         __cal_util_get_time_text_from_format(CAL_UTIL_DATE_FORMAT_9, NULL, &tm, start_date, sizeof(start_date) - 1);
606
607         __cal_util_get_time_text_from_format(CAL_UTIL_DATE_FORMAT_18, NULL, &tm, month_year, sizeof(month_year) - 1);
608
609         tm.tm_mday += 6;
610         __cal_util_get_time_text_from_format(CAL_UTIL_DATE_FORMAT_9, NULL, &tm, end_date, sizeof(end_date) - 1);
611
612         snprintf(time_text, sizeof(time_text) - 1,"W%s %s", week, month_year);
613
614         elm_object_item_part_text_set(item, part, time_text);
615 }
616
617 int cal_util_get_time_text(char* buf, int buf_size, const char *date, const char* time, const struct tm *tm)
618 {
619         char time_text[128] = {0};
620
621         __cal_util_get_time_text_from_format(date, time, tm, time_text, sizeof(time_text) - 1);
622
623         snprintf(buf, buf_size, "%s", time_text);
624
625         return CAL_STRLEN(time_text);
626 }
627
628 void cal_util_emit_signal(Evas_Object *obj, const char *fmt, ...)
629 {
630         va_list ap;
631
632         va_start(ap, fmt);
633         vsnprintf(_sigbuf, sizeof(_sigbuf), fmt, ap);
634         va_end(ap);
635
636         edje_object_signal_emit(obj, _sigbuf, "prog");
637 }
638
639 Evas_Object* cal_util_add_separator(Evas_Object *box, const char *style)
640 {
641         Evas_Object *sp;
642
643         if (!box || CAL_STRCMP(elm_object_widget_type_get(box), "box"))
644                 return NULL;
645
646         sp = elm_separator_add(box);
647         if (sp) {
648                 elm_separator_horizontal_set(sp, EINA_TRUE);
649                 elm_object_style_set(sp, style);
650                 evas_object_show(sp);
651                 elm_box_pack_end(box, sp);
652         }
653
654         return sp;
655 }
656
657 Evas_Object* cal_util_add_scroller(Evas_Object *p)
658 {
659         c_retv_if(!p, NULL);
660
661         Evas_Object *sc = NULL;
662
663         sc = elm_scroller_add(p);
664         c_retv_if(!sc, NULL);
665
666         elm_scroller_bounce_set(sc, EINA_FALSE, EINA_TRUE);
667         elm_scroller_policy_set(sc, ELM_SCROLLER_POLICY_OFF, ELM_SCROLLER_POLICY_AUTO);
668
669         evas_object_show(sc);
670
671         return sc;
672 }
673
674 Evas_Object* cal_util_add_box(Evas_Object *p)
675 {
676         Evas_Object *bx;
677
678         bx = elm_box_add(p);
679         if (!bx)
680                 return NULL;
681
682         evas_object_size_hint_weight_set(bx, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
683         evas_object_size_hint_align_set(bx, EVAS_HINT_FILL, 0.0);
684         evas_object_show(bx);
685
686         return bx;
687 }
688
689 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)
690 {
691         CAL_ASSERT(btn);
692
693         elm_object_style_set(btn, style);
694         evas_object_size_hint_weight_set(btn, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
695         evas_object_size_hint_align_set(btn, EVAS_HINT_FILL, 0.5);
696         elm_object_text_set(btn, label);
697         evas_object_smart_callback_add(btn, "clicked", func, data);
698         evas_object_show(btn);
699 }
700
701 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.
702 {
703         c_retm_if(!data, "data is null");
704         c_retm_if(!obj, "obj is null");
705
706         if (elm_object_focus_get(data)) {
707                 elm_object_signal_emit(data, "elm,state,eraser,show", "elm");
708         }
709         else {
710                 if (elm_entry_is_empty(obj))
711                         elm_object_signal_emit(data, "elm,state,guidetext,show", "elm");
712                 else
713                         elm_object_signal_emit(data, "elm,state,guidetext,hide", "elm");
714         }
715 }
716
717 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.
718 {
719         c_retm_if(!data, "data is null");
720         c_retm_if(!obj, "obj is null");
721
722         elm_object_signal_emit(data, "elm,state,eraser,show", "elm");
723
724         elm_object_signal_emit(data, "elm,state,guidetext,hide", "elm");
725 }
726
727 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.
728 {
729         c_retm_if(!data, "data is null");
730         c_retm_if(!obj, "obj is null");
731
732         if (elm_entry_is_empty(obj))
733                 elm_object_signal_emit(data, "elm,state,guidetext,show", "elm");
734
735         elm_object_signal_emit(data, "elm,state,eraser,hide", "elm");
736 }
737
738 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.
739 {
740         c_retm_if(!data, "data is null");
741
742         elm_entry_entry_set(data, "");
743 }
744
745 static void __cal_util_edit_field_clicked_callback(void *data, Evas_Object *obj, const char *emission, const char *source)
746 {
747         c_ret_if(!data);
748
749         elm_object_focus_set(data, EINA_TRUE);
750 }
751
752 Evas_Object * cal_util_add_edit_field(Evas_Object *parent, const char *guide, Eina_Bool single_line, Eina_Bool is_editable)
753 {
754         c_retvm_if(!parent, NULL, "parent is null");
755
756         Evas_Object *layout = elm_layout_add(parent);
757         c_retvm_if(!layout, NULL, "layout is null");
758
759         elm_layout_theme_set(layout, "layout", "editfield", "default");
760
761         Evas_Object *entry = elm_entry_add(parent);
762         c_retvm_if(!entry, layout, "entry is null");
763
764         elm_object_part_content_set(layout, "elm.swallow.content", entry);
765         evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
766
767         if (guide && guide[0] != '\0')
768                 elm_object_part_text_set(layout, "elm.guidetext", guide);
769
770         elm_entry_single_line_set(entry, single_line);
771         elm_entry_scrollable_set(entry, single_line);
772
773         if (is_editable)
774                 elm_object_signal_callback_add(layout, "elm,eraser,clicked", "elm", __cal_util_edit_field_eraser_clicked_callback, entry);
775         else {
776                 elm_entry_editable_set(entry, is_editable);
777                 elm_object_signal_emit(layout, "elm,state,eraser,hide", "elm");
778         }
779
780         evas_object_smart_callback_add(entry, "changed", __cal_util_edit_field_changed_callback, layout);
781         evas_object_smart_callback_add(entry, "focused", __cal_util_edit_field_focused_callback, layout);
782         evas_object_smart_callback_add(entry, "unfocused", __cal_util_edit_field_unfocused_callback, layout);
783
784         evas_object_show(layout);
785
786         elm_object_signal_callback_add(layout, "mouse,clicked,1", "*", __cal_util_edit_field_clicked_callback, entry);
787
788         return layout;
789 }
790
791 Evas_Object * cal_util_add_nocontents(Evas_Object *parent, const char *label)
792 {
793         c_retvm_if(!parent, NULL, "parent is null");
794
795         Evas_Object *layout = elm_layout_add(parent);
796         c_retvm_if(!layout, NULL, "layout is null");
797
798         elm_layout_theme_set(layout, "layout", "nocontents", "full");
799
800         if (label && strlen(label))
801                 elm_object_part_text_set(layout, "elm.text", label);
802
803         return layout;
804 }
805
806 Evas_Object * cal_util_add_search_nocontents(Evas_Object *parent, const char *label)
807 {
808         c_retvm_if(!parent, NULL, "parent is null");
809
810         Evas_Object *layout = elm_layout_add(parent);
811         c_retvm_if(!layout, NULL, "layout is null");
812
813         elm_layout_theme_set(layout, "layout", "nocontents", "search");
814
815         if (CAL_STRLEN(label))
816                 elm_object_part_text_set(layout, "elm.text", label);
817
818         return layout;
819 }
820
821 static void __cal_util_popup_response_callback(void *data, Evas_Object *obj, void *event_info)
822 {
823         evas_object_del(data);
824 }
825
826 Evas_Object * cal_util_add_popup(Evas_Object *parent, const char *style, const char *title, const char *desc,
827         void (*callback_func)(void *data, Evas_Object *obj, void *ei), void *data, ...)
828 {
829         c_retvm_if(!parent, NULL, "parent is null");
830
831         Evas_Object *popup = elm_popup_add(parent);
832         c_retvm_if(!popup, NULL, "popup is null");
833
834         if (CAL_STRLEN(style))
835                 elm_object_style_set(popup, style);
836
837         if (data)
838                 evas_object_data_set(popup, "data", data);
839
840         evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
841
842         if (CAL_STRLEN(title))
843                 elm_object_part_text_set(popup, "title,text", title);
844
845         if (CAL_STRLEN(desc))
846                 elm_object_text_set(popup, desc);
847
848         va_list arg;
849         int i = 0;
850         Evas_Object *btn = NULL;
851         const char *label = NULL;
852         char part[16] = {0};
853
854         if (!callback_func)
855                 callback_func = __cal_util_popup_response_callback;
856
857         va_start(arg, data);
858
859         while ((label = va_arg(arg, char*))) {
860                 btn = elm_button_add(popup);
861                 if (!btn) {
862                         ERR("btn is null");
863                         continue;
864                 }
865
866                 elm_object_text_set(btn, label);
867                 snprintf(part, sizeof(part), "button%d", ++i);
868                 elm_object_part_content_set(popup, part, btn);
869                 if (!CAL_STRCMP(label, S_("IDS_COM_BODY_DELETE"))) {
870                         elm_object_style_set(btn, "sweep/delete");
871                         char buffer[128] = {0};
872                         snprintf(buffer, sizeof(buffer), "<font_size=36>%s</font_size>", label);
873                         elm_object_text_set(btn, buffer);
874                 } else {
875                         elm_object_style_set(btn, "popup_button/default");
876                 }
877                 evas_object_smart_callback_add(btn, "clicked", callback_func, popup);
878         }
879
880         va_end(arg);
881
882         if (!i)
883                 elm_popup_timeout_set(popup, 3.0);
884
885         evas_object_show(popup);
886         elm_object_focus_set(popup, EINA_TRUE);
887
888         return popup;
889 }
890
891 Evas_Object * cal_util_add_datetime(Evas_Object *parent, const char *title, const struct tm *tm)
892 {
893         c_retvm_if(!parent, NULL, "parent is null");
894
895         Evas_Object *datetime = NULL;
896
897         if (CAL_STRLEN(title)) {
898
899                 Evas_Object *layout = elm_layout_add(parent);
900                 c_retvm_if(!layout, NULL, "layout is null");
901
902                 Eina_Bool r = elm_layout_file_set(layout, EDJ_FILE, "dialoguegroup/datetime");
903                 c_retvm_if(!r, NULL, "r is null");
904
905                 evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
906
907                 datetime = elm_datetime_add(layout);
908                 c_retvm_if(!datetime, NULL, "datetime is null");
909
910                 elm_object_part_text_set(layout, "elm.text", title);
911                 elm_object_part_content_set(layout, "elm.icon", datetime);
912
913                 elm_datetime_field_limit_set(datetime, ELM_DATETIME_YEAR, 70, 136);
914                 if (tm)
915                         elm_datetime_value_set(datetime, tm);
916
917                 return layout;
918
919         }
920         else {
921                 datetime = elm_datetime_add(parent);
922                 c_retvm_if(!datetime, NULL, "datetime is null");
923
924                 evas_object_size_hint_weight_set(datetime, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
925
926                 elm_datetime_field_limit_set(datetime, ELM_DATETIME_YEAR, 70, 136);
927                 if (tm)
928                         elm_datetime_value_set(datetime, tm);
929
930                 return datetime;
931         }
932 }
933
934 void cal_util_get_week_number_text(const struct tm* tm, char *buffer, int buffer_size)
935 {
936         c_retm_if(!tm, "tm is null");
937         c_retm_if(!buffer, "buffer is null");
938
939         __cal_util_get_time_text_from_format(CAL_UTIL_DATE_FORMAT_16, NULL ,tm, buffer, buffer_size);
940 }
941
942 int cal_util_get_distance(Evas_Coord_Point *s, Evas_Coord_Point *e)
943 {
944         c_retvm_if(!s, -1, "s is null");
945         c_retvm_if(!e, -1, "e is null");
946
947         return (Evas_Coord)sqrt((s->x - e->x) * (s->x - e->x)
948                 + (s->y - e->y) * (s->y - e->y));
949 }
950
951 static Eina_Bool __cal_util_hide_small_information_by_timer(void *user_data)
952 {
953         c_retv_if(!user_data, ECORE_CALLBACK_CANCEL);
954
955         cal_util_hide_small_information((Evas_Object *)user_data);
956
957         return ECORE_CALLBACK_CANCEL;
958 }
959
960 void cal_util_show_small_information(Evas_Object *layout, const char *message, double timeout)
961 {
962         c_ret_if(!layout);
963         c_ret_if(!CAL_STRLEN(message));
964
965         Evas_Object *small_information = elm_object_part_content_get(layout, "sel.swallow.contents");
966
967         if (!small_information) {
968
969                 small_information = elm_layout_add(layout);
970                 c_ret_if(!small_information);
971
972                 elm_object_part_content_set(layout, "sel.swallow.contents", small_information);
973                 evas_object_size_hint_weight_set(small_information, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
974                 evas_object_size_hint_align_set(small_information, EVAS_HINT_FILL, EVAS_HINT_FILL);
975
976                 Eina_Bool ret = elm_layout_theme_set(small_information, "standard", "selectioninfo", "center_text");
977                 c_warn_if(!ret, "elm_layout_theme_set() is failed.");
978
979                 evas_object_show(small_information);
980         }
981
982         elm_object_part_text_set(small_information, "elm.text", message);
983
984         elm_object_signal_emit(layout, "show,selection,info", "elm");
985
986         if (0 < timeout) {
987                 Ecore_Timer *timer = ecore_timer_add(timeout, __cal_util_hide_small_information_by_timer, layout);
988                 c_warn_if(!timer, "ecore_timer_add() is failed.");
989         }
990 }
991
992 void cal_util_hide_small_information(Evas_Object *layout)
993 {
994         c_ret_if(!layout);
995
996         elm_object_signal_emit(layout, "hide,selection,info", "elm");
997 }
998
999 Evas_Object * cal_util_add_toolbar_button(Evas_Object *naviframe, const char *part, const char *text, Evas_Smart_Cb func, void *data)
1000 {
1001         c_retv_if(!naviframe, NULL);
1002         c_retv_if(!CAL_STRLEN(part), NULL);
1003
1004         Evas_Object *button = elm_button_add(naviframe);
1005         c_retv_if(!button, NULL);
1006
1007         elm_object_style_set(button, "naviframe/toolbar/default");
1008
1009         if (CAL_STRLEN(text))
1010                 elm_object_text_set(button, text);
1011
1012         evas_object_smart_callback_add(button, "clicked", func, data);
1013
1014         Elm_Object_Item *navi_item = elm_naviframe_top_item_get(naviframe);
1015         c_retv_if(!navi_item, NULL);
1016
1017         elm_object_item_part_content_set(navi_item, part, button);
1018
1019         return button;
1020 }
1021
1022 Evas_Object * cal_util_add_toolbar_button2(Evas_Object* naviframe, const char* part, const char* filename, Evas_Smart_Cb func, void* data)
1023 {
1024         c_retv_if(!naviframe, NULL);
1025         c_retv_if(!CAL_STRLEN(part), NULL);
1026
1027         Evas_Object* button = elm_button_add(naviframe);
1028         c_retv_if(!button, NULL);
1029
1030         elm_object_style_set(button, "naviframe/title_icon");
1031
1032         Evas_Object* icon = elm_icon_add(naviframe);
1033         elm_icon_file_set(icon, CAL_IMAGES_EDJ, filename);
1034
1035         evas_object_size_hint_aspect_set(icon, EVAS_ASPECT_CONTROL_VERTICAL, 1, 1);
1036         elm_icon_resizable_set(icon, EINA_TRUE, EINA_TRUE);
1037
1038         elm_object_part_content_set(button, "icon", icon);
1039
1040         evas_object_smart_callback_add(button, "clicked", func, data);
1041
1042         Elm_Object_Item *navi_item = elm_naviframe_top_item_get(naviframe);
1043         c_retv_if(!navi_item, NULL);
1044
1045         elm_object_item_part_content_set(navi_item, part, button);
1046
1047         return button;
1048 }
1049
1050 int cal_util_get_default_first_day_of_week()
1051 {
1052         UErrorCode status = U_ZERO_ERROR;
1053         UChar utf16_timezone[64] = {0};
1054
1055         char *locale = vconf_get_str(VCONFKEY_REGIONFORMAT);
1056
1057         if (CAL_STRLEN(locale)) {
1058                 uloc_setDefault(locale, &status);
1059                 free(locale);
1060         }
1061
1062         u_uastrncpy(utf16_timezone, _timezone, sizeof(_timezone));
1063
1064         UCalendar *cal = ucal_open(utf16_timezone, u_strlen(utf16_timezone), uloc_getDefault(), UCAL_TRADITIONAL, &status);
1065         c_retvm_if(!cal, -1, "cal is null");
1066
1067         int first_day_of_week = 0;
1068
1069         first_day_of_week = ucal_getAttribute(cal, UCAL_FIRST_DAY_OF_WEEK);
1070
1071         ucal_close(cal);
1072
1073         return first_day_of_week;
1074 }