2.0 alpha
[apps/core/preloaded/calendar.git] / src / edit-repeat.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 "edit-repeat.h"
20
21 #define CAL_REPEAT_EVERY_2_WEEK (CALS_FREQ_SECONDLY+1)
22 #define CAL_REPEAT_EVERY_3_DAY (CAL_REPEAT_EVERY_2_WEEK+1)
23
24 static const char *_name = "calendar/edit/repeat";
25
26 enum __cal_edit_repeat_freq {
27         CAL_EDIT_REPEAT_FREQ_OFF = 0,
28         CAL_EDIT_REPEAT_FREQ_EVERY_DAY,
29         CAL_EDIT_REPEAT_FREQ_EVERY_3_DAY,
30         CAL_EDIT_REPEAT_FREQ_EVERY_WEEK,
31         CAL_EDIT_REPEAT_FREQ_EVERY_2_WEEK,
32         CAL_EDIT_REPEAT_FREQ_EVERY_MONTH,
33         CAL_EDIT_REPEAT_FREQ_EVERY_YEAR,
34         CAL_EDIT_REPEAT_FREQ_MAX
35 };
36
37 typedef struct {
38         const char *name;
39         struct appdata* ad;
40         Evas_Object *parent;
41         Evas_Object *ly; // self
42         Evas_Object *genlist;
43
44         Evas_Object *freq_radio_group;
45         Evas_Object *freq_radio_group_item[7];
46         Evas_Object *range_radio_group;
47         Evas_Object *range_radio_group_item[3];
48
49         Evas_Object *day_selector;
50         Evas_Object *repeat_occurrence_entry;
51         Evas_Object *end_date;
52
53         struct tm *start_date_time;
54
55         int count;
56         struct tm until;
57
58         int selected_freq_radio;
59         int prev_selected_freq_radio;
60         int selected_range_radio;
61 } cal_edit_repeat_data;
62
63 static int __cal_edit_repeat_get_radio_index(int freq)
64 {
65         switch (freq) {
66         case CALS_FREQ_ONCE:
67                 return CAL_EDIT_REPEAT_FREQ_OFF;
68         case CALS_FREQ_DAILY:
69                 return CAL_EDIT_REPEAT_FREQ_EVERY_DAY;
70         case CAL_REPEAT_EVERY_3_DAY:
71                 return CAL_EDIT_REPEAT_FREQ_EVERY_3_DAY;
72         case CALS_FREQ_WEEKLY:
73                 return CAL_EDIT_REPEAT_FREQ_EVERY_WEEK;
74         case CAL_REPEAT_EVERY_2_WEEK:
75                 return CAL_EDIT_REPEAT_FREQ_EVERY_2_WEEK;
76         case CALS_FREQ_MONTHLY:
77                 return CAL_EDIT_REPEAT_FREQ_EVERY_MONTH;
78         case CALS_FREQ_YEARLY:
79                 return CAL_EDIT_REPEAT_FREQ_EVERY_YEAR;
80         default:
81                 ERR("Invalid value : %d", freq);
82                 return 0;
83         }
84 }
85
86 static int __cal_edit_repeat_get_freq(int radio_index)
87 {
88         switch (radio_index) {
89         case CAL_EDIT_REPEAT_FREQ_OFF:
90                 return CALS_FREQ_ONCE;
91         case CAL_EDIT_REPEAT_FREQ_EVERY_DAY:
92                 return CALS_FREQ_DAILY;
93         case CAL_EDIT_REPEAT_FREQ_EVERY_3_DAY:
94                 return CAL_REPEAT_EVERY_3_DAY;
95         case CAL_EDIT_REPEAT_FREQ_EVERY_WEEK:
96                 return CALS_FREQ_WEEKLY;
97         case CAL_EDIT_REPEAT_FREQ_EVERY_2_WEEK:
98                 return CAL_REPEAT_EVERY_2_WEEK;
99         case CAL_EDIT_REPEAT_FREQ_EVERY_MONTH:
100                 return CALS_FREQ_MONTHLY;
101         case CAL_EDIT_REPEAT_FREQ_EVERY_YEAR:
102                 return CALS_FREQ_YEARLY;
103         default:
104                 ERR("Invalid value : %d", radio_index);
105                 return CALS_FREQ_ONCE;
106         }
107 }
108
109 static int __cal_edit_repeat_range_index[] = {
110         CALS_RANGE_NONE,
111         CALS_RANGE_COUNT,
112         CALS_RANGE_UNTIL,
113 };
114
115 typedef enum {
116         CAL_EDIT_REPEAT_SEPARATOR_FREQ,
117         CAL_EDIT_REPEAT_ITEM_FREQ,
118         CAL_EDIT_REPEAT_SEPARATOR_RANGE,
119         CAL_EDIT_REPEAT_ITEM_RANGE,
120 }cal_edit_repeat_item_type;
121
122 static Elm_Entry_Filter_Limit_Size _limit_2char = {
123         .max_char_count = 2,
124 };
125 static Elm_Entry_Filter_Accept_Set _digit_only = {
126         .accepted = "0123456789",
127 };
128
129 static Elm_Genlist_Item_Class itc_seperator, itc_1icon_1text, itc_1icon;
130
131 typedef struct __cal_edit_repeat_item_data cal_edit_repeat_item_data;
132 struct __cal_edit_repeat_item_data
133 {
134         cal_edit_repeat_data *p;
135         int index;
136         cal_edit_repeat_item_type type;
137         Elm_Object_Item *it;
138         Evas_Object *radio;
139 };
140
141 #define is_leap_year_tm(y)  (((y+1900)%4 == 0 && ((y+1900)%100 != 0 || (y+1900)%400 == 0))? 1 : 0)
142
143 static void __cal_edit_repeat_delete_layout(void *data, Evas *e, Evas_Object *obj, void *ei)
144 {
145         CAL_FN_START;
146
147         cal_edit_repeat_data *p = data;
148
149         free(p);
150 }
151
152 static void __cal_edit_repeat_clicked_callback(void *data, Evas *e, Evas_Object *obj, void *ei)
153 {
154         cal_edit_repeat_data *p;
155         Evas_Event_Mouse_Up *ev = ei;
156         int val = (int)data;
157
158         if (ev && ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD)
159                 return;
160
161         p = CAL_UTIL_GET_PRIV_DATA(obj);
162         elm_radio_value_set(p->freq_radio_group, val);
163         p->prev_selected_freq_radio = p->selected_freq_radio;
164         p->selected_freq_radio = val;
165 }
166
167 static void __cal_edit_repeat_entry_clicked_callback(void *data, Evas *e, Evas_Object *obj, void *ei)
168 {
169         c_ret_if(!ei);
170         c_ret_if(!data);
171
172         Evas_Event_Mouse_Up *ev = ei;
173         int val = (int)data;
174
175         if (ev && ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD)
176                 return;
177
178         cal_edit_repeat_data *p = CAL_UTIL_GET_PRIV_DATA(obj);
179         elm_radio_value_set(p->range_radio_group, val);
180
181         if (CALS_RANGE_COUNT == val) {
182                 elm_object_focus_set(p->repeat_occurrence_entry, EINA_TRUE);
183                 elm_entry_cursor_end_set(p->repeat_occurrence_entry);
184         }
185
186         p->selected_range_radio = val;
187
188         const char* str = elm_entry_entry_get(p->repeat_occurrence_entry);
189         c_ret_if(!CAL_STRLEN(str));
190
191         p->count = atoi(str);
192 }
193
194 int cal_edit_repeat_get_repeat(Evas_Object *ly, enum cals_range *range, struct tm *until, int *count)
195 {
196         c_retvm_if(!ly, CALS_FREQ_ONCE, "ly is null");
197
198         const char* text = NULL;
199
200         cal_edit_repeat_data *p = CAL_UTIL_GET_PRIV_DATA(ly);
201         c_retvm_if(!p, CALS_FREQ_ONCE, "p is null");
202
203         c_retvm_if(CAL_STRCMP(p->name, _name), CALS_FREQ_ONCE, "Invaild object.");
204         c_retvm_if(!p->freq_radio_group, CALS_FREQ_ONCE, "p->freq_radio_group is null");
205         c_retvm_if(!p->range_radio_group, CALS_FREQ_ONCE, "p->range_radio_group is null");
206
207         *range = __cal_edit_repeat_range_index[p->selected_range_radio];
208
209         switch (*range) {
210                 case CALS_RANGE_NONE:
211                         *count = 10;
212
213                         *until = *p->start_date_time;
214                         cal_util_update_tm_month(until, 1);
215                         break;
216
217                 case CALS_RANGE_COUNT:
218                         text = elm_entry_entry_get(p->repeat_occurrence_entry);
219                         c_retvm_if(!text, CALS_FREQ_ONCE, "text is null");
220
221                         *count = atoi(text);
222
223                         *until = *p->start_date_time;
224                         cal_util_update_tm_month(until, 1);
225
226                         break;
227
228                 case CALS_RANGE_UNTIL:
229                         *count = 10;
230
231                         *until = p->until;
232                         break;
233
234                 default:
235                         break;
236         }
237
238         return __cal_edit_repeat_get_freq(p->selected_freq_radio);
239 }
240
241 void _cal_edit_repeat_get_freq_str(int freq, char *buf, int size)
242 {
243         c_ret_if(!buf);
244
245         char *format = NULL;
246
247         switch (freq) {
248
249                 case CALS_FREQ_DAILY:
250
251                         snprintf(buf, size, "%s", C_("IDS_KC_BODY_EVERY_DAY"));
252                         break;
253
254                 case CAL_REPEAT_EVERY_3_DAY:
255
256                         format = C_("IDS_CLD_BODY_EVERY_PD_DAYS");
257                         snprintf(buf, size, format, 3);
258                         break;
259
260                 case CALS_FREQ_WEEKLY:
261
262                         snprintf(buf, size, "%s", C_("IDS_CLD_BODY_EVERY_WEEK"));
263                         break;
264
265                 case CAL_REPEAT_EVERY_2_WEEK:
266
267                         format = C_("IDS_CLD_BODY_EVERY_PD_WEEKS");
268                         snprintf(buf, size, format, 2);
269                         break;
270
271                 case CALS_FREQ_MONTHLY:
272
273                         snprintf(buf, size, "%s", C_("IDS_CLD_BODY_EVERY_MONTH"));
274                         break;
275
276                 case CALS_FREQ_YEARLY:
277
278                         snprintf(buf, size, "%s", C_("IDS_CLD_BODY_EVERY_YEAR"));
279                         break;
280
281                 default:
282
283                         snprintf(buf, size, "%s", S_("IDS_COM_BODY_OFF"));
284                         break;
285
286         }
287
288         buf[size-1] = '\0';
289 }
290
291 static void __cal_edit_repeat_get_range_str(enum cals_range range, char *buf, int size)
292 {
293         c_ret_if(!buf);
294
295         switch (range) {
296
297                 case CALS_RANGE_COUNT:
298
299                         snprintf(buf, size, "%s", C_("IDS_COM_POP_TIMES_LC"));
300                         break;
301
302                 default:
303
304                         snprintf(buf, size, "%s", S_("IDS_COM_BODY_NONE"));
305                         break;
306
307         }
308
309         buf[size-1] = '\0';
310 }
311
312
313 static char *__cal_edit_repeat_get_genlist_item_label(void *data, Evas_Object *obj, const char *part)
314 {
315         CAL_FN_START;
316
317         c_retv_if(!data, NULL);
318
319         cal_edit_repeat_item_data *item_data = data;
320
321         char string[32] = {0};
322
323         if (!CAL_STRCMP(part, "elm.text")) {
324                 switch (item_data->type) {
325                         case CAL_EDIT_REPEAT_SEPARATOR_FREQ:
326                                 return strdup(C_("IDS_CLD_BODY_REPEAT"));
327
328                         case CAL_EDIT_REPEAT_ITEM_FREQ:
329                                 _cal_edit_repeat_get_freq_str(__cal_edit_repeat_get_freq(item_data->index), string, sizeof(string));
330                                 return strdup(string);
331
332                         case CAL_EDIT_REPEAT_SEPARATOR_RANGE:
333                                 return strdup(C_("IDS_CLD_BODY_REPEAT_UNTIL"));
334
335                         case CAL_EDIT_REPEAT_ITEM_RANGE:
336                                 if (__cal_edit_repeat_range_index[item_data->index] == CALS_RANGE_NONE)
337                                         return strdup(S_("IDS_COM_BODY_NONE"));
338
339                         default:
340                                 return NULL;
341                 }
342         }
343
344         return NULL;
345 }
346
347 static void __cal_edit_repeat_genlist_item_delete_callback(void *data, Evas_Object *obj)
348 {
349         CAL_FN_START;
350
351         c_ret_if(!data);
352
353         cal_edit_repeat_item_data *item_data = data;
354
355         cal_edit_repeat_data* p = item_data->p;
356         c_ret_if(!p);
357
358         if (item_data->type == CAL_EDIT_REPEAT_ITEM_FREQ) {
359
360                 c_ret_if(!p->freq_radio_group_item);
361
362                 p->freq_radio_group_item[item_data->index] = NULL;
363
364         } else if (item_data->type == CAL_EDIT_REPEAT_ITEM_RANGE) {
365
366                 c_ret_if(!p->range_radio_group_item);
367
368                 p->range_radio_group_item[item_data->index] = NULL;
369         }
370
371         free(item_data);
372 }
373
374 static Eina_Bool __cal_edit_repeat_get_genlist_item_state(void *data, Evas_Object *obj, const char *part)
375 {
376         return EINA_FALSE;
377 }
378
379 static void __cal_edit_repeat_set_radio_group(cal_edit_repeat_data *p, Evas_Object *rd, cal_edit_repeat_item_type type, int val)
380 {
381         if (type == CAL_EDIT_REPEAT_ITEM_FREQ) {
382                 if (!val) {
383
384                         p->freq_radio_group = rd;
385
386                         elm_radio_group_add(p->freq_radio_group_item[1], p->freq_radio_group);
387                         elm_radio_group_add(p->freq_radio_group_item[2], p->freq_radio_group);
388                         elm_radio_group_add(p->freq_radio_group_item[3], p->freq_radio_group);
389                         elm_radio_group_add(p->freq_radio_group_item[4], p->freq_radio_group);
390                         elm_radio_group_add(p->freq_radio_group_item[5], p->freq_radio_group);
391                         elm_radio_group_add(p->freq_radio_group_item[6], p->freq_radio_group);
392                 }
393
394                 elm_radio_group_add(rd, p->freq_radio_group);
395         } else {
396
397                 if (!val) {
398                         p->range_radio_group = rd;
399
400                         elm_radio_group_add(p->range_radio_group_item[1], p->range_radio_group);
401                         elm_radio_group_add(p->range_radio_group_item[2], p->range_radio_group);
402
403                 }
404
405                 elm_radio_group_add(rd, p->range_radio_group);
406         }
407 }
408
409 static Evas_Object* __cal_edit_repeat_add_custom_text_object(cal_edit_repeat_data *p, Evas_Object *obj, const char *text)
410 {
411         Evas_Object *entry = NULL;
412
413         if (!text || text[0] == '\0')
414                 return entry;
415
416         entry = elm_entry_add(obj);
417         CAL_ASSERT(entry);
418
419         elm_entry_single_line_set(entry, EINA_TRUE);
420         elm_entry_context_menu_disabled_set(entry, EINA_TRUE);
421         evas_object_size_hint_weight_set(entry, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
422         evas_object_size_hint_align_set(entry, EVAS_HINT_FILL, EVAS_HINT_EXPAND);
423         elm_entry_editable_set(entry, EINA_FALSE);
424         elm_entry_input_panel_enabled_set(entry, EINA_FALSE);
425         elm_entry_autocapital_type_set(entry, EINA_TRUE);
426
427         elm_entry_entry_set(entry,text);
428
429         return entry;
430 }
431
432 static Evas_Object* __cal_edit_repeat_add_radio(Evas_Object *obj, cal_edit_repeat_item_data *item_data)
433 {
434         c_retvm_if(!obj, NULL, "dg is NULL.");
435         c_retvm_if(!item_data, NULL, "item_data is NULL.");
436
437         cal_edit_repeat_data *p = item_data->p;
438         c_retv_if(!p, NULL);
439
440         Evas_Object *rd = elm_radio_add(obj);
441         c_retv_if(!rd, NULL);
442
443         evas_object_propagate_events_set(rd, EINA_TRUE);
444
445         cal_edit_repeat_item_type type = item_data->type;
446
447         if (type == CAL_EDIT_REPEAT_ITEM_FREQ)
448                 *(p->freq_radio_group_item + item_data->index) = rd;
449         else if (type == CAL_EDIT_REPEAT_ITEM_RANGE)
450                 *(p->range_radio_group_item + item_data->index) = rd;
451
452         elm_radio_state_value_set(rd, item_data->index);
453
454         item_data->radio = rd;
455
456         __cal_edit_repeat_set_radio_group(p, rd, type, item_data->index);
457
458         evas_object_show(rd);
459         evas_object_data_set(rd, "priv", p);
460
461         if (item_data->type == CAL_EDIT_REPEAT_ITEM_FREQ)
462                 evas_object_event_callback_add(rd, EVAS_CALLBACK_MOUSE_UP, __cal_edit_repeat_clicked_callback, (void *)item_data->index);
463         else if (0 == item_data->index || 1 == item_data->index)
464                         evas_object_event_callback_add(rd, EVAS_CALLBACK_MOUSE_UP, __cal_edit_repeat_entry_clicked_callback, (void *)item_data->index);
465
466         return rd;
467 }
468
469 static void __cal_edit_repeat_entry_unfocused(void *data, Evas_Object *obj, void *event_info)
470 {
471         cal_edit_repeat_data *p = (cal_edit_repeat_data *)data;
472
473         char times[8] = {0,};
474         snprintf(times, 7, "%d", p->count);
475         elm_entry_entry_set(p->repeat_occurrence_entry, times);
476
477 }
478
479 static void __cal_edit_repeat_entry_changed_callback(void *data, Evas_Object *obj, void *event_info)
480 {
481         CAL_ASSERT(data);
482         cal_edit_repeat_data *p = data;
483
484         const char *s = elm_entry_entry_get(p->repeat_occurrence_entry);
485         p->count = atoi(s);
486 }
487
488 static Evas_Object* __cal_edit_repeat_add_entry(Evas_Object *parent, cal_edit_repeat_data *p)
489 {
490         Evas_Object *e;
491         Ecore_IMF_Context *ic;
492
493         e = elm_entry_add(parent);
494         if (!e)
495                 return NULL;
496
497         elm_object_style_set(e, "font_color_black");
498         elm_entry_single_line_set(e, EINA_TRUE);
499         elm_entry_markup_filter_append(e, elm_entry_filter_limit_size, &_limit_2char);
500         elm_entry_markup_filter_append(e, elm_entry_filter_accept_set, &_digit_only);
501         elm_entry_input_panel_layout_set(e, ELM_INPUT_PANEL_LAYOUT_NUMBERONLY);
502         elm_entry_context_menu_disabled_set(e, EINA_TRUE);
503
504         ic = elm_entry_imf_context_get(e);
505         if (!ic)
506                 return NULL;
507
508         evas_object_smart_callback_add(e, "unfocused", __cal_edit_repeat_entry_unfocused, p);
509         evas_object_smart_callback_add(e, "changed", __cal_edit_repeat_entry_changed_callback, p);
510
511         return e;
512 }
513
514
515 static Evas_Object* __cal_edit_repeat_add_times(Evas_Object *obj, cal_edit_repeat_item_data *item_data)
516 {
517         c_retv_if(!obj, NULL);
518         c_retv_if(!item_data, NULL);
519
520         cal_edit_repeat_data *p = item_data->p;
521         c_retv_if(!p, NULL);
522
523         Evas_Object *rd;
524         Evas_Object *entry;
525
526         char times[16] = {0};
527
528         Evas_Object *ly = cal_util_add_layout(obj, "dialoguegroup/repeat/times");
529         c_retv_if(!ly, NULL);
530
531         __cal_edit_repeat_get_range_str(__cal_edit_repeat_range_index[item_data->index], times, sizeof(times));
532
533         Evas_Object *entry_times = __cal_edit_repeat_add_custom_text_object(p, obj, times);
534         c_retv_if(!entry_times, NULL);
535
536         elm_object_part_content_set(ly, "text", entry_times);
537
538         evas_object_show(ly);
539         evas_object_data_set(ly, "priv", p);
540         evas_object_event_callback_add(ly, EVAS_CALLBACK_MOUSE_UP, __cal_edit_repeat_entry_clicked_callback, (void*)item_data->index);
541
542         rd = elm_radio_add(obj);
543         if (!rd) {
544                 evas_object_del(ly);
545                 return NULL;
546         }
547
548         elm_radio_state_value_set(rd, item_data->index);
549         elm_object_part_content_set(ly, "content", rd);
550         item_data->radio = rd;
551         __cal_edit_repeat_set_radio_group(p, rd, item_data->type, item_data->index);
552
553         entry = __cal_edit_repeat_add_entry(ly, p);
554         if (!entry)
555         {
556                 evas_object_del(ly);
557                 return NULL;
558         }
559         elm_object_part_content_set(ly, "input_sw", entry);
560
561         p->repeat_occurrence_entry = entry;
562         snprintf(times, sizeof(times), "%d", p->count);
563         elm_entry_entry_set(p->repeat_occurrence_entry, times);
564
565         return ly;
566 }
567
568 static void __cal_edit_repeat_end_date_changed_callback(void *data, Evas_Object *obj, void *ei)
569 {
570         cal_edit_repeat_data *p = data;
571
572         elm_datetime_value_get(obj, &p->until);
573 }
574
575 static Evas_Object* __cal_edit_repeat_add_radio_tm(Evas_Object *obj, cal_edit_repeat_item_data *item_data)
576 {
577         c_retv_if(!obj, NULL);
578         c_retv_if(!item_data, NULL);
579
580         cal_edit_repeat_data *p = item_data->p;
581         c_retv_if(!p, NULL);
582
583         Evas_Object *ly = cal_util_add_layout(obj, "dialoguegroup/tm");
584         c_retvm_if(!ly, NULL, "ly is null");
585
586         evas_object_show(ly);
587         evas_object_data_set(ly, "priv", p);
588
589         Evas_Object *rd = elm_radio_add(obj);
590         if (!rd) {
591                 evas_object_del(ly);
592                 return NULL;
593         }
594
595         evas_object_propagate_events_set(rd, EINA_TRUE);
596
597         elm_radio_state_value_set(rd, item_data->index);
598
599         elm_object_part_content_set(ly, "content", rd);
600         item_data->radio = rd;
601
602         *(p->range_radio_group_item + item_data->index) = rd;
603
604         __cal_edit_repeat_set_radio_group(p, rd, item_data->type,  item_data->index);
605
606         Evas_Object *datetime = cal_util_add_datetime(ly, NULL, &p->until);
607         c_retvm_if(!datetime, NULL, "datetime is null");
608
609         elm_datetime_field_visible_set(datetime, ELM_DATETIME_HOUR, EINA_FALSE);
610         elm_datetime_field_visible_set(datetime, ELM_DATETIME_MINUTE, EINA_FALSE);
611         elm_datetime_field_visible_set(datetime, ELM_DATETIME_AMPM, EINA_FALSE);
612
613         elm_datetime_field_limit_set(datetime, ELM_DATETIME_YEAR, 70, 137);
614
615         evas_object_smart_callback_add(datetime,"changed", __cal_edit_repeat_end_date_changed_callback, p);
616
617         elm_object_part_content_set(ly, "datefield", datetime);
618
619         return ly;
620 }
621
622 static Evas_Object* __cal_edit_repeat_add_radio_group(Evas_Object *obj, cal_edit_repeat_item_data *item_data)
623 {
624         CAL_FN_START;
625
626         cal_edit_repeat_data *p = CAL_UTIL_GET_PRIV_DATA(obj);
627         c_retv_if(!p, NULL);
628
629         if (item_data->type == CAL_EDIT_REPEAT_ITEM_FREQ) {
630
631                 return __cal_edit_repeat_add_radio(obj,  item_data);
632
633         } else if (item_data->type == CAL_EDIT_REPEAT_ITEM_RANGE){
634
635                 switch (__cal_edit_repeat_range_index[item_data->index]) {
636
637                 case CALS_RANGE_NONE:
638                         return __cal_edit_repeat_add_radio(obj, item_data);
639
640                 case CALS_RANGE_COUNT:
641                         return __cal_edit_repeat_add_times(obj, item_data);
642
643                 case CALS_RANGE_UNTIL:
644                         return __cal_edit_repeat_add_radio_tm(obj, item_data);
645
646                 default:
647                         return NULL;
648                 }
649         } else {
650
651                 ERR("Error!!");
652
653                 return NULL;
654         }
655
656         CAL_FN_END;
657 }
658
659 static void __cal_edit_repeat_set_selected_freq_state(cal_edit_repeat_data *p)
660 {
661         elm_radio_value_set(p->freq_radio_group, p->selected_freq_radio);
662 }
663
664 static void __cal_edit_repeat_set_selected_range_state(cal_edit_repeat_data *p)
665 {
666         CAL_FN_START;
667
668         c_ret_if(!p);
669
670         char times[10] = {0};
671
672         snprintf(times,sizeof(times),"%d", p->count);
673         elm_entry_entry_set(p->repeat_occurrence_entry, times);
674         elm_radio_value_set(p->range_radio_group, p->selected_range_radio);
675
676         CAL_FN_END;
677 }
678
679 static Evas_Object *__cal_edit_repeat_get_genlist_item_icon(void *data, Evas_Object *obj, const char *part)
680 {
681         CAL_FN_START;
682
683         c_retv_if(!data, NULL);
684         c_retv_if(!obj, NULL);
685
686         Evas_Object *e_obj = NULL;
687
688         if (!CAL_STRCMP(part, "elm.icon")) {
689                 cal_edit_repeat_item_data *item_data = data;
690
691                 cal_edit_repeat_data *p = item_data->p;
692                 cal_edit_repeat_item_type type = item_data->type;
693
694                 if (type == CAL_EDIT_REPEAT_ITEM_FREQ) {
695
696                         e_obj = __cal_edit_repeat_add_radio_group(obj, item_data);
697
698                         __cal_edit_repeat_set_selected_freq_state(p);
699
700                 } else if (type == CAL_EDIT_REPEAT_ITEM_RANGE){
701
702                         e_obj = __cal_edit_repeat_add_radio_group(obj, item_data);
703
704                         __cal_edit_repeat_set_selected_range_state(p);
705
706                 } else {
707
708                         ERR("!Error");
709
710                         return NULL;
711                 }
712         }
713
714         CAL_FN_END;
715
716         return e_obj;
717
718 }
719
720 static void __cal_edit_repeat_genlist_item_select_callback(void *data, Evas_Object *obj, void *event_info)
721 {
722         CAL_FN_START;
723
724         c_ret_if(!obj);
725
726         Elm_Object_Item *it = elm_genlist_selected_item_get(obj);
727         c_ret_if(!it);
728
729         elm_genlist_item_selected_set(it, EINA_FALSE);
730
731         cal_edit_repeat_item_data *item_data = elm_object_item_data_get(it);
732         c_ret_if(!item_data);
733
734         cal_edit_repeat_data *p = item_data->p;
735         c_ret_if(!p);
736
737         cal_edit_repeat_item_type type = item_data->type;
738         int index = item_data->index;
739
740         if (type == CAL_EDIT_REPEAT_ITEM_FREQ) {
741
742                 p->selected_freq_radio = index;
743                 elm_radio_value_set(p->freq_radio_group, index);
744                 elm_object_focus_set(p->repeat_occurrence_entry, EINA_FALSE);
745
746         } else if (type == CAL_EDIT_REPEAT_ITEM_RANGE){
747
748                 elm_radio_value_set(p->range_radio_group, index);
749                 p->selected_range_radio = index;
750
751                 if (index != __cal_edit_repeat_range_index[CALS_RANGE_COUNT])
752                         elm_object_focus_set(p->repeat_occurrence_entry, EINA_FALSE);
753         } else {
754
755                 ERR("Error!!");
756
757                 return;
758         }
759
760         CAL_FN_END;
761 }
762
763 static void __cal_edit_repeat_add_separator(Evas_Object *genlist, cal_edit_repeat_data *p, cal_edit_repeat_item_type type)
764 {
765         CAL_FN_START;
766
767         cal_edit_repeat_item_data *item_data = calloc(1, sizeof(cal_edit_repeat_item_data));
768         c_ret_if(!item_data);
769
770         item_data->type = type;
771         item_data->p = p;
772
773         item_data->it = elm_genlist_item_append(genlist, &itc_seperator, (void*)item_data, NULL, ELM_GENLIST_ITEM_NONE, __cal_edit_repeat_genlist_item_select_callback, NULL);
774
775         elm_genlist_item_select_mode_set(item_data->it, ELM_OBJECT_SELECT_MODE_NONE);
776
777         CAL_FN_END;
778 }
779
780 static void __cal_edit_repeat_add_freq(Evas_Object *genlist, cal_edit_repeat_data *p)
781 {
782         CAL_FN_START;
783
784         int i;
785         for (i = 0; i < CAL_EDIT_REPEAT_FREQ_MAX; i++) {
786                 cal_edit_repeat_item_data *item_data = calloc(1, sizeof(cal_edit_repeat_item_data));
787                 c_ret_if(!item_data);
788
789                 item_data->p = p;
790                 item_data->index = i;
791                 item_data->type = CAL_EDIT_REPEAT_ITEM_FREQ;
792
793                 item_data->it = elm_genlist_item_append(genlist, &itc_1icon_1text, (void*)item_data, NULL, ELM_GENLIST_ITEM_NONE, __cal_edit_repeat_genlist_item_select_callback, item_data);
794         }
795
796         CAL_FN_END;
797 }
798
799 static void __cal_edit_repeat_add_range(Evas_Object *genlist, cal_edit_repeat_data *p)
800 {
801         CAL_FN_START;
802
803         int i;
804         for (i = 0; i < sizeof(__cal_edit_repeat_range_index)/sizeof(__cal_edit_repeat_range_index[0]); i++) {
805                 cal_edit_repeat_item_data *item_data = calloc(1, sizeof(cal_edit_repeat_item_data));
806                 c_ret_if(!item_data);
807
808                 item_data->p = p;
809                 item_data->index = i;
810                 item_data->type = CAL_EDIT_REPEAT_ITEM_RANGE;
811
812                 item_data->it = elm_genlist_item_append(genlist, &itc_1icon_1text, (void*)item_data, NULL, ELM_GENLIST_ITEM_NONE, __cal_edit_repeat_genlist_item_select_callback, item_data);
813         }
814
815         CAL_FN_END;
816 }
817
818 static void __cal_edit_repeat_add_genlist_item(Evas_Object *genlist, cal_edit_repeat_data *p)
819 {
820         CAL_FN_START;
821
822         __cal_edit_repeat_add_separator(genlist, p, CAL_EDIT_REPEAT_SEPARATOR_FREQ);
823
824         __cal_edit_repeat_add_freq(genlist, p);
825
826         __cal_edit_repeat_add_separator(genlist, p, CAL_EDIT_REPEAT_SEPARATOR_RANGE);
827
828         __cal_edit_repeat_add_range(genlist, p);
829
830         CAL_FN_END;
831 }
832
833 static int __cal_edit_repeat_add_content(cal_edit_repeat_data *p)
834 {
835         CAL_FN_START;
836
837         p->genlist = elm_genlist_add(p->ly);
838         c_retv_if(!p->genlist, -1);
839
840         evas_object_data_set(p->genlist, "priv", p);
841
842         itc_seperator.item_style = "dialogue/title";
843         itc_seperator.func.text_get = __cal_edit_repeat_get_genlist_item_label;
844         itc_seperator.func.state_get = __cal_edit_repeat_get_genlist_item_state;
845         itc_seperator.func.del = __cal_edit_repeat_genlist_item_delete_callback;
846
847         itc_1icon_1text.item_style = "dialogue/1text.1icon.2";
848         itc_1icon_1text.func.text_get = __cal_edit_repeat_get_genlist_item_label;
849         itc_1icon_1text.func.del = __cal_edit_repeat_genlist_item_delete_callback;
850         itc_1icon_1text.func.content_get = __cal_edit_repeat_get_genlist_item_icon;
851
852         itc_1icon.item_style = "dialogue/1icon";
853         itc_1icon.func.text_get = __cal_edit_repeat_get_genlist_item_label;
854         itc_1icon.func.del = __cal_edit_repeat_genlist_item_delete_callback;
855         itc_1icon.func.content_get = __cal_edit_repeat_get_genlist_item_icon;
856
857         __cal_edit_repeat_add_genlist_item(p->genlist, p);
858
859         Evas_Object* cf = elm_conformant_add(p->ad->win);
860         c_retv_if(!cf, -1);
861
862         evas_object_size_hint_weight_set(cf, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
863         evas_object_show(cf);
864         elm_object_content_set(cf, p->genlist);
865         elm_object_style_set(cf, "internal_layout");
866         elm_object_part_content_set(p->ly, "sw", cf);
867
868         CAL_FN_END;
869
870         return 0;
871 }
872
873 static void __cal_edit_repeat_set_state(cal_edit_repeat_data *p, int freq, enum cals_range range, struct tm* until, int* occurrency)
874 {
875         p->selected_freq_radio = __cal_edit_repeat_get_radio_index(freq);
876         p->prev_selected_freq_radio = __cal_edit_repeat_get_radio_index(freq);
877         p->selected_range_radio = __cal_edit_repeat_range_index[range];
878 }
879
880 Evas_Object *cal_edit_repeat_create_view(Evas_Object *parent, struct appdata* ad, int freq, enum cals_range range, struct tm* start_date_time, struct tm* until, int* count)
881 {
882         CAL_FN_START;
883
884         c_retvm_if(!parent, NULL, "parent is null");
885         c_retvm_if(!ad, NULL, "ad is null");
886
887         cal_edit_repeat_data *p = calloc(1, sizeof(cal_edit_repeat_data));
888         c_retvm_if(!p, NULL, "p is null");
889
890         p->name = _name;
891         p->parent = parent;
892         p->freq_radio_group = NULL;
893         p->range_radio_group = NULL;
894         p->ad = ad;
895
896         Evas_Object *ly = cal_util_add_layout(parent, "edit");
897         if (!ly) {
898                 ERR("ly is null");
899                 free(p);
900                 return NULL;
901         }
902
903         p->ly = ly;
904         p->count = *count;
905         p->start_date_time = start_date_time;
906         p->until = *until;
907
908         evas_object_data_set(ly, "priv", p);
909
910         __cal_edit_repeat_set_state(p, freq, range, until, count);
911
912         int r = __cal_edit_repeat_add_content(p);
913         if (r) {
914                 evas_object_del(ly);
915                 free(p);
916                 return NULL;
917         }
918
919         evas_object_event_callback_add(ly, EVAS_CALLBACK_DEL, __cal_edit_repeat_delete_layout, p);
920
921         return ly;
922 }