Tizen 2.1 base
[apps/core/preloaded/calendar.git] / src / view-main-week.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 <vconf.h>
20
21 #include "view.h"
22 #include "edit-view.h"
23 #include "detail-view.h"
24 #include "external-ug.h"
25 #include "cld-images.h"
26 #include "list-base.h"
27 #include "week-data.h"
28
29 #define WEEK_DAY_NUMS 7
30 #define BUF_SIZE_64 64
31 #define MAX_DATA_LEN 10
32
33 typedef enum{
34         DISPLAY_SMALL_WEEKEND = 0,
35         DISPLAY_TODO
36 }cal_week_view_type;
37
38 typedef struct {
39         const char *name;
40         struct appdata *ad;
41         Evas_Object *parent;
42         Evas_Object *ly;
43
44         Evas_Object *left;
45         Evas_Object *right;
46
47         int ign;
48         int downed;
49         Evas_Coord_Rectangle rect;
50         Evas_Coord_Point down;
51         Evas_Coord_Point move;
52
53         int pos_selected;
54
55         Eina_List *week_event_list;
56         int event_count[7];
57
58         int pos_down;
59
60         int is_moving;
61
62         cal_week_view_type week_view_type;
63
64         int expanded_week_day;
65
66         Evas_Coord_Point multi_mouse_down;
67         Evas_Coord_Point multi_mouse_move;
68
69         Evas_Coord mouse_down_distance;
70         int multi_touched;
71
72         Evas_Object *ctx;
73         Evas_Object *popup;
74 }cal_week_data;
75
76 typedef struct {
77         cal_week_data *p;
78         Elm_Object_Item *it;
79         calendar_record_h record;
80 }cal_week_genlist_item_data;
81
82 static _calendar_book_color calendar_color;
83 static char signal_source[32];
84
85 static Evas_Object* __cal_week_cal_get_genlist_item_icon(void *data, Evas_Object *obj, const char *part);
86 static void __cal_week_cal_expand_week_day(cal_week_data *p, int pos);
87 static void __cal_week_cal_unexpand_week_day(cal_week_data *p, int pos);
88 static void __cal_week_more_event_callback(void *data, Evas_Object *obj, const char *e, const char *src);
89 static void __cal_week_genlist_mouse_up_event_callback(void *data, Evas *e, Evas_Object *ly, void *ei);
90 static void __cal_week_genlist_mouse_down_event_callback(void *data, Evas *e, Evas_Object *ly, void *ei);
91 static void __cal_week_genlist_mouse_move_event_callback(void *data, Evas *e, Evas_Object *ly, void *ei);
92 static void __cal_week_set_more_text(cal_week_data *p, int count, int pos);
93
94 static char* __cal_week_get_genlist_item_label(void *data, Evas_Object *obj, const char *part);
95 static void __cal_week_delete_genlist_item(void *data, Evas_Object *obj);
96 static char* __cal_week_share_genlist_item_label(void *data, Evas_Object *obj, const char *part);
97
98 static Elm_Genlist_Item_Class _itc = {
99         .item_style = "2text.2icon.edit.1",
100         .func.content_get = __cal_week_cal_get_genlist_item_icon,
101         .func.text_get = __cal_week_get_genlist_item_label,
102         .func.del  = __cal_week_delete_genlist_item,
103 };
104
105 static Elm_Genlist_Item_Class itc_1text = {
106         .item_style = "1text",
107         .func.content_get = NULL,
108         .func.text_get = __cal_week_share_genlist_item_label,
109 };
110
111 static char* __cal_week_share_genlist_item_label(void *data, Evas_Object *obj, const char *part)
112 {
113         c_retvm_if(!data || !obj || !part, NULL, "data is null");
114
115         char *name = (char *)data;
116
117         if (!CAL_STRCMP(part, "elm.text")) {
118                 return strdup(name);
119         }
120
121         return NULL;
122 }
123
124
125 static void __cal_week_delete_genlist_item(void *data, Evas_Object *obj)
126 {
127         c_retm_if(!data, "data is null.");
128         cal_week_genlist_item_data *item_data = data;
129
130         free(item_data);
131         item_data = NULL;
132 }
133
134 static char* __cal_week_get_genlist_item_label(void *data, Evas_Object *obj, const char *part)
135 {
136         c_retv_if(!data, NULL);
137         c_retv_if(!obj, NULL);
138         c_retv_if(!part, NULL);
139
140         char *tmp = NULL;
141
142         cal_week_data *p = CAL_UTIL_GET_PRIV_DATA(obj);
143         c_retv_if(!p, NULL);
144
145         cal_week_genlist_item_data *item_data = data;
146         calendar_record_h record = item_data->record;
147         c_retv_if(!record, NULL);
148
149         calendar_error_e error = CALENDAR_ERROR_NONE;
150         Eina_Bool is_task =EINA_FALSE;
151         Eina_Bool is_done = EINA_FALSE;
152         int todo_status = CALENDAR_TODO_STATUS_COMPLETED;
153
154         if(_calendar_is_task_record(record))
155         {
156                 is_task = EINA_TRUE;
157                 error = calendar_record_get_int(record, _calendar_todo_calendar_book.todo_status, &todo_status);
158                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_int() is failed(%x)", error);
159
160                 if (CALENDAR_TODO_STATUS_COMPLETED == todo_status) {
161                         is_done = EINA_TRUE;
162                 } else {
163                         is_done = EINA_FALSE;
164                 }
165         }
166
167
168         if (!CAL_STRCMP(part, "elm.textblock.1")) {
169                 if (is_task && is_done) {
170                         tmp = _calendar_get_summary(record);
171
172                         char *text = calloc(1, strlen(tmp)+500);
173                         if(!text)       {
174                                 ERR("text is NULL!");
175                                 free(tmp);
176                                 return NULL;
177                         }
178
179                         sprintf(text, "<strikethrough=on strikethrough_color=#333333><color=#3333337F>%s</color></strikethrough>", tmp);
180
181                         free(tmp);
182
183                         return text;
184                 }
185                 return NULL;
186         } else if (!CAL_STRCMP(part, "elm.text.1")) {
187                 if (!is_task || !is_done) {
188                         return _calendar_get_summary(record);
189                 }
190                 return NULL;
191         } else if (!CAL_STRCMP(part, "elm.textblock.2")) {
192                 struct tm tm_start;
193                 struct tm tm_end;
194                 char buf[512] = {0};
195                 char etime[256] = {0};
196                 char stime[256] = {0};
197
198                 if (_calendar_is_allday_record(record)) {
199                         return strdup(C_("IDS_COM_BODY_ALL_DAY"));
200                 } else {
201                         calendar_time_s start_time = {0};
202                         calendar_time_s end_time = {0};
203                         _calendar_get_start_time(record, &start_time);
204                         _calendar_get_end_time(record, &end_time);
205
206                         cal_util_convert_lli_to_tm(NULL, start_time.time.utime, &tm_start);
207                         cal_util_convert_lli_to_tm(NULL, end_time.time.utime, &tm_end);
208
209                         if (is_hour24) {
210                                 cal_util_get_time_text(stime, sizeof(stime), NULL, CAL_UTIL_TIME_FORMAT_6, &tm_start);
211                                 cal_util_get_time_text(etime, sizeof(etime), NULL, CAL_UTIL_TIME_FORMAT_6, &tm_end);
212                         } else {
213                                 cal_util_get_time_text(stime, sizeof(stime), NULL, CAL_UTIL_TIME_FORMAT_1, &tm_start);
214                                 cal_util_get_time_text(etime, sizeof(etime), NULL, CAL_UTIL_TIME_FORMAT_1, &tm_end);
215                         }
216                 }
217
218                 if (!is_task) {
219                         snprintf(buf, sizeof(buf), "%s - %s", stime, etime);
220                         return strdup(buf);
221                 } else if (is_done) {
222                         sprintf(buf, "<color=#2A89C27F>%s</color>", etime);
223                         return strdup(buf);
224                 } else {
225                         return strdup(etime);
226                 }
227                 return NULL;
228         }
229
230         return NULL;
231 }
232
233 static void __cal_week_free_week_event_list(cal_week_data *p)
234 {
235         c_ret_if(!p);
236         c_ret_if(!p->week_event_list);
237
238         Eina_List *l = NULL;
239         Eina_List *list = NULL;
240
241         EINA_LIST_FOREACH(p->week_event_list, l, list) {
242                 if (list)
243                         _calendar_free_record_list(&list);
244         }
245
246         p->week_event_list = eina_list_free(p->week_event_list);
247 }
248
249 static void __cal_week_text_refresh_callback(void *data)
250 {
251         CAL_FN_START;
252
253         c_retm_if(!data, "data is null");
254         cal_week_data *p = data;
255
256         cal_main_update_title_text(p->ad);
257
258         elm_object_text_set(p->ad->new_event_button, S_("IDS_COM_BODY_CREATE"));
259
260         int day_index;
261         const char *part = NULL;
262         Evas_Object *box = NULL;
263         Evas_Object *genlist = NULL;
264         Eina_List *list = NULL;
265         struct tm t = p->ad->base_tm;
266         cal_util_update_tm_day(&t, p->ad->wday_start - t.tm_wday);
267         for (day_index = 0; day_index < WEEK_DAY_NUMS; day_index++) {
268                 part = cal_util_get_part_text("wdb/%d/sw", day_index);
269
270                 cal_util_set_time_text(CAL_UTIL_GET_EDJ_DATA(p->ly), cal_util_get_part_text("wdb/%d/text_wday", day_index), CAL_UTIL_DATE_FORMAT_2, NULL, &t);
271                 cal_util_update_tm_day(&t, 1);
272
273                 box = evas_object_data_get(p->ly, part);
274                 list = elm_box_children_get(box);
275                 genlist = eina_list_nth(list, 0);
276                 elm_genlist_realized_items_update(genlist);
277         }
278         eina_list_free(list);
279 }
280
281 static void __cal_week_delete_layout(void *data, Evas *e, Evas_Object *obj, void *ei)
282 {
283         CAL_FN_START;
284
285         c_ret_if(!obj);
286
287         cal_week_data *p = CAL_UTIL_GET_PRIV_DATA(obj);
288         c_ret_if(!p);
289
290         __cal_week_free_week_event_list(p);
291
292         cal_util_delete_evas_object(&p->left);
293         cal_util_delete_evas_object(&p->right);
294
295         if (p->ctx) {
296                 evas_object_del(p->ctx);
297                 p->ctx = NULL;
298         }
299
300         if (p->popup) {
301                 evas_object_del(p->popup);
302                 p->popup = NULL;
303         }
304
305         cal_lang_manager_remove_callback(__cal_week_text_refresh_callback, p);
306
307         free(p);
308 }
309
310 static void __cal_week_resize_view(void *data, Evas *e, Evas_Object *ly, void *ei)
311 {
312         c_ret_if(!data);
313
314         cal_week_data *p = data;
315
316         cal_util_get_geometry(&p->rect, p->ly);
317 }
318
319 static void __cal_week_mouse_down_event_callback(void *data, Evas *e, Evas_Object *ly, void *ei)
320 {
321         cal_week_data *p = data;
322         Evas_Event_Mouse_Down *ev = ei;
323
324         p->down.x = p->move.x = ev->canvas.x;
325         p->down.y = p->move.y = ev->canvas.y;
326
327         p->downed = 1;
328         p->multi_touched = 0;
329         p->is_moving = 0;
330
331         cal_util_get_geometry(&p->rect, p->ly);
332 }
333
334 static inline void __cal_week_set_block(Evas_Object *ly, int pos, Evas_Object *eo)
335 {
336         c_ret_if(!ly);
337         c_ret_if(!eo);
338
339         const char *part = cal_util_get_part_text("wdb/%d/disable", pos);
340         c_ret_if(!part);
341
342         Evas_Object *sw = evas_object_data_get(ly, part);
343         if (sw)
344                 evas_object_del(sw);
345
346         Evas_Object *obj = elm_object_part_content_get(ly, part);
347
348         elm_object_part_content_unset(ly, part);
349
350         if (obj)
351                 evas_object_del(obj);
352
353         elm_object_part_content_set(ly, part, eo);
354
355         evas_object_data_set(ly, part, eo);
356 }
357
358 static inline void __cal_week_set_box(Evas_Object *ly, int pos)
359 {
360         c_ret_if(!ly);
361
362         const char *part = cal_util_get_part_text("wdb/%d/sw", pos);
363
364         Evas_Object *sw = evas_object_data_get(ly, part);
365         if (sw)
366                 evas_object_del(sw);
367
368         Evas_Object *obj = elm_object_part_content_get(ly, part);
369
370         elm_object_part_content_unset(ly, part);
371
372         if (obj)
373                 evas_object_del(obj);
374
375         elm_object_part_content_set(ly, part, NULL);
376
377         evas_object_data_set(ly, part, NULL);
378 }
379
380 static void __cal_week_highlight_today_title(struct tm *tm, Evas_Object *obj, int index)
381 {
382         c_ret_if(!tm);
383         c_ret_if(!obj);
384
385         time_t t;
386         struct tm cur;
387
388         t = time(NULL);
389         localtime_r(&t, &cur);
390
391         if (cur.tm_year == tm->tm_year
392                 && cur.tm_mon ==tm->tm_mon
393                 && cur.tm_mday == tm->tm_mday) {
394                         cal_util_emit_signal(obj, "focus,%d,today", index);
395         } else {
396                 cal_util_emit_signal(obj, "unfocus,%d,today", index);
397         }
398 }
399
400 static void __cal_week_update_days(Evas_Object *ly, cal_week_data *p, struct tm *tm_base, int start)
401 {
402         c_ret_if(!ly);
403         c_ret_if(!p);
404         c_ret_if(!p->ad);
405         c_ret_if(!tm_base);
406
407         int day_index;
408
409         struct tm t = *tm_base;
410         cal_util_update_tm_day(&t, start - t.tm_wday);
411         char date_text[8]={0};
412
413         for (day_index = 0; day_index < WEEK_DAY_NUMS; day_index++) {
414                 memset(date_text, 0x00, 8);
415                 sprintf(date_text, "%d", t.tm_mday);
416                 edje_object_part_text_set(CAL_UTIL_GET_EDJ_DATA(ly), cal_util_get_part_text("wdb/%d/text", day_index), date_text);
417
418                 cal_util_set_time_text(CAL_UTIL_GET_EDJ_DATA(ly), cal_util_get_part_text("wdb/%d/text_wday", day_index), CAL_UTIL_DATE_FORMAT_2, NULL, &t);
419
420                 cal_util_set_text(CAL_UTIL_GET_EDJ_DATA(ly), cal_util_get_part_text("wdb/%d/more", day_index), "");
421
422                 __cal_week_set_box(ly, day_index);
423
424                 __cal_week_highlight_today_title(&t, CAL_UTIL_GET_EDJ_DATA(ly), day_index);
425
426                 cal_util_update_tm_day(&t, 1);
427         }
428 }
429
430 static void __cal_week_fill_days(Evas_Object *ly, cal_week_data *p, struct tm *tm, int start)
431 {
432         int i;
433
434         for (i = 0; i < WEEK_DAY_NUMS; i++) {
435                 int wday = CAL_UTIL_GET_WDAY(i + start);
436
437                 switch (wday) {
438                 case 0:
439                         cal_util_emit_signal(CAL_UTIL_GET_EDJ_DATA(ly), "sunday,%d", i);
440                         break;
441                 case 6:
442                         cal_util_emit_signal(CAL_UTIL_GET_EDJ_DATA(ly), "satday,%d", i);
443                         break;
444                 }
445         }
446
447         __cal_week_update_days(ly, p, tm, start);
448 }
449
450 static Evas_Object* __cal_week_create_week_view(cal_week_data *p, int d)
451 {
452         c_retv_if(!p, NULL);
453
454         struct appdata* ad = p->ad;
455         c_retv_if(!ad, NULL);
456
457         Evas_Object *ly;
458         struct tm tm_s, tm_e;
459         int r_s, r_e;
460         tm_s = ad->base_tm;
461
462         tm_s.tm_mday -= CAL_UTIL_GET_WDAY(tm_s.tm_wday - ad->wday_start);
463         tm_e = tm_s;
464         tm_e.tm_mday += 7;
465
466         r_s = cal_util_update_tm_day(&tm_s, d * 7);
467         r_e = cal_util_update_tm_day(&tm_e, d * 7);
468
469         if (r_s == -1 && r_e == -1) {
470                 return NULL;
471         } else {
472                 ly = cal_util_add_layout(p->parent, p->name);
473
474                 c_retv_if(!ly, NULL);
475
476                 __cal_week_fill_days(ly, p, &tm_s, ad->wday_start);
477         }
478
479         evas_object_resize(ly, p->rect.w, p->rect.h);
480
481         Evas_Object *clip = (Evas_Object*)edje_object_part_object_get(CAL_UTIL_GET_EDJ_DATA(ad->main), "clip");
482         c_retv_if(!clip, NULL);
483
484         evas_object_clip_set(ly, (Evas_Object*)clip);
485
486         return ly;
487 }
488
489 static void __cal_week_move_left(cal_week_data *p, Evas_Coord x)
490 {
491         c_ret_if(!p);
492
493         if (!p->left)
494                 p->left = __cal_week_create_week_view(p, -1);
495
496         c_ret_if(!p->left);
497
498         evas_object_move(p->left, x, p->rect.y);
499 }
500
501 static void __cal_week_move_right(cal_week_data *p, Evas_Coord x)
502 {
503         c_ret_if(!p);
504
505         if (!p->right)
506                 p->right = __cal_week_create_week_view(p, 1);
507
508         c_ret_if(!p->right);
509
510         evas_object_move(p->right, x, p->rect.y);
511 }
512
513 static int __cal_week_check_is_out_date(cal_week_data *p, int d)
514 {
515         c_retvm_if(!p, -1,  "p is null");
516
517         struct tm tm_s, tm_e;
518         int r_s, r_e;
519
520         tm_s = p->ad->base_tm;
521
522         tm_s.tm_mday -= CAL_UTIL_GET_WDAY(tm_s.tm_wday - p->ad->wday_start);
523         tm_e = tm_s;
524         tm_e.tm_mday += 7;
525
526         r_s = cal_util_update_tm_day(&tm_s, d * 7);
527         r_e = cal_util_update_tm_day(&tm_e, d * 7);
528
529         if(r_s == -1 && r_e == -1)
530                 return -1;
531
532         return 0;
533 }
534
535 static void __cal_week_mouse_up_event_callback(void *data, Evas *e, Evas_Object *ly, void *ei)
536 {
537         cal_week_data *p = data;
538
539         Evas_Coord dx;
540         Evas_Coord d;
541         int r;
542         Evas_Event_Mouse_Up *ev = ei;
543
544         if (!p->downed || p->multi_touched)
545                 return;
546
547         p->downed = 0;
548         p->is_moving = 0;
549
550         d = 1;
551         r = -1;
552
553         dx = p->down.x - ev->canvas.x;
554         if (dx < 0) {
555                 d = -1;
556                 dx = dx * -1;
557         }
558
559         if (cal_util_get_flick_distance_threshold() < dx) {
560                 r = __cal_week_check_is_out_date(p, d);
561                 if (-1 != r) {
562                         cal_util_update_tm_day(&(p->ad->base_tm), d * 7);
563                         cal_main_update_title_text(p->ad);
564                         cal_week_update_view(ly);
565                 }
566         }
567
568         if (r == -1) {
569                 __cal_week_move_left(p, -p->rect.w);
570                 __cal_week_move_right(p, p->rect.w);
571         }
572
573         evas_object_move(ly, p->rect.x, p->rect.y);
574 }
575
576 static void __cal_week_mouse_move_event_callback(void *data, Evas *e, Evas_Object *ly, void *ei)
577 {
578         c_retm_if(!data,  "data is null");
579         c_retm_if(!ei,  "ei is null");
580
581         cal_week_data *p = data;
582         Evas_Event_Mouse_Move *ev = ei;
583
584         Evas_Coord dx;
585         Evas_Coord dy;
586         Evas_Coord x;
587
588         c_retm_if(!p->ad, "p->ad is null");
589
590         p->move.x = ev->cur.canvas.x;
591         p->move.y = ev->cur.canvas.y;
592
593         if (!p->downed || p->multi_touched)
594                 return;
595
596         dx = p->move.x - p->down.x;
597         x = p->rect.x + dx;
598
599         dy = p->down.y - p->move.y;
600
601 }
602
603 static int __cal_week_get_position(cal_week_data *p, struct tm *tm, int start)
604 {
605         return CAL_UTIL_GET_WDAY(tm->tm_wday - start);
606 }
607
608 static void __cal_week_update_select(cal_week_data *p, int pos)
609 {
610         c_ret_if(!p);
611         c_ret_if(!p->ly);
612
613         cal_util_emit_signal(CAL_UTIL_GET_EDJ_DATA(p->ly), "unfocus,%d,wday", p->pos_selected);
614         p->pos_selected = pos;
615         cal_util_emit_signal(CAL_UTIL_GET_EDJ_DATA(p->ly), "focus,%d,wday", p->pos_selected);
616 }
617
618 static void __cal_week_down_event_callback(void *data, Evas_Object *obj, const char *e, const char *src)
619 {
620         c_retm_if(!data, "data is null");
621         cal_week_data *p = data;
622         p->pos_down = atoi(e);
623         c_retm_if(WEEK_DAY_NUMS == p->pos_down, "pos_down is todo.");
624 }
625
626 static void __cal_week_cal_event_callback(void *data, Evas_Object *obj, const char *e, const char *src)
627 {
628         CAL_FN_START;
629         c_retm_if(!data, "data is null");
630         cal_week_data *p = data;
631
632         if (p->pos_selected == -1)
633         {
634                 ERR("");
635                 return;
636         }
637
638         if (p->ign) {
639                 p->ign = 0;
640                 ERR("");
641                 return;
642         }
643
644         struct appdata *ad = p->ad;
645         c_retm_if(!ad, "ad is null");
646
647         int pos = atoi(e);
648         if (pos == WEEK_DAY_NUMS) {
649
650
651                 if (pos != p->pos_selected) {
652                         __cal_week_update_select(p, pos);
653                         return;
654                 }
655                 return;
656         } else {
657                 int d = pos - p->pos_selected;
658
659                 cal_util_update_tm_day(&ad->base_tm, d);
660
661
662                 if (pos != p->pos_selected) {
663                         __cal_week_update_select(p, pos);
664                         return;
665                 }
666         }
667 }
668
669 static void __cal_week_genlist_item_check_changed_callback(void *data, Evas_Object *obj, void *event_info)
670 {
671         c_ret_if(!data);
672         c_ret_if(!obj);
673
674         cal_week_genlist_item_data *item_data = data;
675
676         calendar_record_h record =item_data->record;
677         c_ret_if(!record);
678         calendar_error_e error = CALENDAR_ERROR_NONE;
679
680         calendar_record_h todo_original_record = NULL;
681         int todo_id = _calendar_get_record_index(record);
682         error = calendar_db_get_record(_calendar_todo._uri, todo_id, &todo_original_record);
683         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_db_get_record(%d) is failed(%x)", todo_id, error);
684
685         Eina_Bool check_status = elm_check_state_get(obj);
686
687         if (check_status) {
688                 error = calendar_record_set_int(todo_original_record, _calendar_todo.todo_status, CALENDAR_TODO_STATUS_COMPLETED);
689                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_set_int() is failed(%x)", error);
690
691                 error = calendar_record_set_int(record, _calendar_todo_calendar_book.todo_status, CALENDAR_TODO_STATUS_COMPLETED);
692                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_set_int() is failed(%x)", error);
693         }
694         else {
695                 error = calendar_record_set_int(todo_original_record, _calendar_todo.todo_status, CALENDAR_TODO_STATUS_IN_PROCESS);
696                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_set_int() is failed(%x)", error);
697
698                 error = calendar_record_set_int(record, _calendar_todo_calendar_book.todo_status, CALENDAR_TODO_STATUS_IN_PROCESS);
699                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_set_int() is failed(%x)", error);
700         }
701
702         Evas_Object *genlist = evas_object_data_get(obj, "genlist");
703         c_ret_if(!genlist);
704
705         cal_week_data *p = evas_object_data_get(genlist, "priv");
706         c_ret_if(!p);
707         c_ret_if(!p->ad);
708
709         cal_main_remove_db_changed_callback(p->ad);
710
711         error = calendar_db_update_record(todo_original_record);
712         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_db_update_record() is failed(%x)", error);
713
714         cal_main_add_db_changed_callback(p->ad);
715
716         elm_genlist_item_update(item_data->it);
717
718 }
719
720 static Evas_Object* __cal_week_cal_get_genlist_item_icon(void *data, Evas_Object *obj, const char *part)
721 {
722         cal_week_data *p = evas_object_data_get(obj, "priv");
723         c_retv_if(!p, NULL);
724         c_retv_if(!p->ad, NULL);
725
726         cal_week_genlist_item_data *item_data = data;
727         calendar_record_h record = item_data->record;
728         c_retv_if(!record, NULL);
729
730         if (!CAL_STRCMP(part, "elm.swallow.colorbar")) {
731                 Evas_Object *icon;
732
733                 icon = evas_object_rectangle_add(evas_object_evas_get(obj));
734                 evas_object_size_hint_align_set(icon, EVAS_HINT_FILL, EVAS_HINT_FILL);
735
736                 _calendar_get_calendar_color(record, &calendar_color);
737
738                 evas_object_color_set(icon, calendar_color.red, calendar_color.green, calendar_color.blue, calendar_color.alpha);
739
740                 return icon;
741         } else if (!CAL_STRCMP(part, "elm.edit.icon.1")) {
742
743                 if(!_calendar_is_task_record(record))
744                         return NULL;
745
746                 Evas_Object *check = elm_check_add(obj);
747                 c_retvm_if(!check, NULL, "elm_check_add returned null");
748
749                 Eina_Bool check_status = EINA_FALSE;
750                 calendar_error_e error = CALENDAR_ERROR_NONE;
751                 int todo_status = 0;
752                 error = calendar_record_get_int(record, _calendar_todo_calendar_book.todo_status, &todo_status);
753                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_int() is failed(%x)", error);
754
755                 if (CALENDAR_TODO_STATUS_COMPLETED == todo_status) {
756                         check_status = EINA_TRUE;
757                 } else if (CALENDAR_TODO_STATUS_IN_PROCESS == todo_status) {
758                         check_status = EINA_FALSE;
759                 }
760
761                 elm_check_state_pointer_set(check, &check_status);
762                 evas_object_data_set(check, "genlist", obj);
763                 evas_object_smart_callback_add(check, "changed", __cal_week_genlist_item_check_changed_callback, item_data);
764                 evas_object_propagate_events_set(check, EINA_FALSE);
765
766                 return check;
767         } else if (!CAL_STRCMP(part, "elm.icon.1")) {
768                 if (_calendar_is_facebook_record(record)) {
769                         Evas_Object *icon = elm_icon_add(obj);
770                         c_retv_if(!icon, NULL);
771
772                         int r = elm_icon_file_set(icon, CAL_IMAGES_EDJ, CAL_IMAGES_FACEBOOK);
773                         c_retv_if(!r, NULL);
774
775                         return icon;
776                 }
777         }
778
779         return NULL;
780 }
781
782 static void __cal_week_cal_genlist_item_select_callback(void *data, Evas_Object *obj, void *event_info)
783 {
784         c_ret_if(!data);
785         calendar_record_h record = data;
786
787         Elm_Object_Item *it = elm_genlist_selected_item_get(obj);
788         if (it)
789                 elm_genlist_item_selected_set(it, EINA_FALSE);
790
791         cal_week_data *p = CAL_UTIL_GET_PRIV_DATA(obj);
792         c_ret_if(!p);
793
794         if (p->ctx) {
795                 return;
796         }
797
798         p->ad->cid = _calendar_get_record_index(record);
799
800         int day_index = (int)evas_object_data_get(obj, "pos");
801         int diff = day_index - p->pos_selected;
802         if(diff)        {
803                 cal_util_update_tm_day(&(p->ad->base_tm), diff);
804                 __cal_week_update_select(p, day_index);
805         }
806
807         cal_detail_create_view(p->ad, record, NULL, NULL);
808 }
809
810 static void __cal_week_ctx_popup_hide_callback(void *data, Evas_Object *obj, void *ei)
811 {
812         c_ret_if(!data);
813
814         cal_week_data* p = data;
815
816         p->ctx = NULL;
817
818         evas_object_del(obj);
819 }
820
821 static void __cal_week_init_ctx_data(calendar_record_h record, cal_week_data *p)
822 {
823         c_ret_if(!p);
824         c_ret_if(!record);
825
826         p->ad->cid = _calendar_get_record_index(record);
827 }
828
829 static void __cal_week_edit_popup_response_event_callback(void *data, Evas_Object *obj, void *event_info)
830 {
831         CAL_FN_START;
832
833         c_retm_if(!data, "data is null");
834         c_retm_if(!obj, "obj is null");
835
836         Evas_Object *popup = data;
837         Evas_Object *button = obj;
838
839         cal_week_data* p = evas_object_data_get(popup, "data");
840         c_retm_if(!p, "p is null");
841
842         struct appdata* ad = p->ad;
843         c_retm_if(!ad, "ad is null");
844
845         calendar_record_h record = evas_object_data_get(p->popup, "record");
846         c_retm_if(!record, "record is null");
847
848         if (!strcmp(elm_object_text_get(button), C_("IDS_CLD_BODY_ONLY_THIS_EVENT"))) {
849                 cal_edit_create_view(ad, record, NULL, NULL);
850         }else if (!strcmp(elm_object_text_get(button), C_("IDS_CLD_POP_ALL_REPETITIVE_EVENTS"))) {
851                 calendar_record_h origin_record = NULL;
852                 int index = _calendar_get_record_index(record);
853                 origin_record = _calendar_get_record_with_index(index);
854
855                 cal_edit_create_view(ad, origin_record, NULL, NULL);
856                 calendar_record_destroy(origin_record, true);
857         }
858
859         evas_object_del(popup);
860         p->popup = NULL;
861 }
862
863 static void __cal_week_delete_event(cal_week_data* p, Eina_Bool is_delete_all)
864 {
865         c_ret_if(!p);
866
867         struct appdata* ad = p->ad;
868         c_ret_if(!ad);
869
870         if (!is_delete_all) {
871
872                 calendar_record_h instance  = NULL;
873
874                 instance = evas_object_data_get(p->ly, "record");
875
876                 _calendar_delete_recurrence_instance(instance);
877
878         } else
879                 _calendar_delete_record_with_index(ad->cid);
880 }
881
882 static void __cal_week_delete_popup_response_event_callback(void *data, Evas_Object *obj, void *event_info)
883 {
884         CAL_FN_START;
885
886         c_retm_if(!data, "data is null");
887         c_retm_if(!obj, "obj is null");
888
889         Evas_Object *popup = data;
890         Evas_Object *button = obj;
891
892         cal_week_data* p = evas_object_data_get(popup, "data");
893         c_retm_if(!p, "p is null");
894
895         if (!strcmp(elm_object_text_get(button), C_("IDS_CLD_BODY_ONLY_THIS_EVENT")))
896                 __cal_week_delete_event(p, EINA_FALSE);
897         else if (!strcmp(elm_object_text_get(button), C_("IDS_CLD_POP_ALL_REPETITIVE_EVENTS")))
898                 __cal_week_delete_event(p, EINA_TRUE);
899
900         evas_object_del(popup);
901         p->popup = NULL;
902 }
903
904 static void __cal_week_show_popup(cal_week_data *p, const char *tit, Eina_Bool is_edit)
905 {
906         c_ret_if(!p);
907
908         void (*callback_func)(void *data, Evas_Object *obj, void *ei) = NULL;
909
910         if (is_edit) {
911                 callback_func = __cal_week_edit_popup_response_event_callback;
912         } else {
913                 callback_func = __cal_week_delete_popup_response_event_callback;
914         }
915
916         p->popup = cal_util_add_popup(p->ad->win, "verticalbuttonstyle", NULL, tit, callback_func,
917                 p, C_("IDS_CLD_BODY_ONLY_THIS_EVENT"),
918                 C_("IDS_CLD_POP_ALL_REPETITIVE_EVENTS"),
919                 S_("IDS_COM_SK_CANCEL"), NULL);
920
921
922         c_retm_if(!p->popup, "cal_util_add_popup(p->ad->win) returned null");
923
924         evas_object_data_set(p->popup, "priv", p);
925 }
926
927 static void __cal_week_edit_norepeat_popup_response_event_callback(void *data, Evas_Object *obj, void *event_info)
928 {
929         c_retm_if(!data, "data is null");
930         c_retm_if(!obj, "obj is null");
931
932         Evas_Object *popup = data;
933         Evas_Object *button = obj;
934
935         cal_week_data* p = evas_object_data_get(popup, "data");
936         c_retm_if(!p, "p is null");
937
938         calendar_record_h record = evas_object_data_get(p->popup, "record");
939         c_retm_if(!record, "record is null");
940
941         if (!strcmp(elm_object_text_get(button), S_("IDS_COM_SK_OK"))) {
942                 calendar_record_h origin_record = NULL;
943                 int index = _calendar_get_record_index(record);
944                 origin_record = _calendar_get_record_with_index(index);
945
946                 cal_edit_create_view(p->ad, origin_record, NULL, NULL);
947                 calendar_record_destroy(origin_record, true);
948         }
949
950         evas_object_del(popup);
951         p->popup = NULL;
952 }
953
954 static void __cal_week_edit_call_callback(void *data, Evas_Object *obj, void *ei)
955 {
956         c_ret_if(!data);
957         c_ret_if(!obj);
958         c_ret_if(!ei);
959
960         cal_week_genlist_item_data *item_data = data;
961         cal_week_data *p = item_data->p;
962         c_ret_if(!p );
963
964         calendar_record_h record = item_data->record;
965         c_ret_if(!record );
966
967         elm_ctxpopup_dismiss(obj);
968
969         __cal_week_init_ctx_data(record, p);
970
971         if (_calendar_is_task_record(record)) {
972                 p->popup = cal_util_add_popup(p->ad->win, NULL, NULL, C_("IDS_TASK_HEADER_EDIT_TASK"), __cal_week_edit_norepeat_popup_response_event_callback, p,
973                                 S_("IDS_COM_SK_OK"),S_("IDS_COM_SK_CANCEL"), NULL);
974         } else {
975                 if (_calendar_is_recurrent_record(record))
976                         __cal_week_show_popup(p, C_("IDS_CLD_HEADER_EDIT_EVENT"), EINA_TRUE);
977                 else {
978                         p->popup = cal_util_add_popup(p->ad->win, NULL, NULL, C_("IDS_CLD_HEADER_EDIT_EVENT"), __cal_week_edit_norepeat_popup_response_event_callback, p,
979                                 S_("IDS_COM_SK_OK"),S_("IDS_COM_SK_CANCEL"), NULL);
980                 }
981         }
982         evas_object_data_set(p->popup, "record", record);
983 }
984
985 static void __cal_week_delete_norepeat_popup_response_event_callback(void *data, Evas_Object *obj, void *event_info)
986 {
987         c_retm_if(!data, "data is null");
988         c_retm_if(!obj, "obj is null");
989
990         Evas_Object *popup = data;
991         Evas_Object *button = obj;
992
993         cal_week_data* p = evas_object_data_get(popup, "data");
994         c_retm_if(!p, "p is null");
995         c_retm_if(!p->ly, "p->ly is null");
996
997         if (NULL != strstr(elm_object_text_get(button), S_("IDS_COM_BODY_DELETE"))) {
998                 calendar_record_h record = evas_object_data_get(p->ly, "record");
999
1000                 _calendar_delete_record(record);
1001         }
1002
1003         evas_object_del(p->popup);
1004         p->popup = NULL;
1005 }
1006
1007 static void __cal_week_delete_call_callback(void *data, Evas_Object *obj, void *ei)
1008 {
1009         c_ret_if(!data);
1010         c_ret_if(!obj);
1011         c_ret_if(!ei);
1012
1013         cal_week_genlist_item_data *item_data = data;
1014         cal_week_data *p = item_data->p;
1015         c_ret_if(!p );
1016
1017         calendar_record_h record = item_data->record;
1018         c_retm_if(!record, "record is NULL.");
1019
1020         elm_ctxpopup_dismiss(obj);
1021
1022         __cal_week_init_ctx_data(record, p);
1023
1024         if (p->ad->cid) {
1025
1026                 evas_object_data_set(p->ly, "record", record);
1027
1028                 if (_calendar_is_task_record(record)) {
1029                         p->popup = cal_util_add_popup(p->ad->win, NULL, NULL, S_("IDS_COM_POP_DELETE_Q"), __cal_week_delete_norepeat_popup_response_event_callback, p,
1030                                         S_("IDS_COM_BODY_DELETE"),S_("IDS_COM_SK_CANCEL"), NULL);
1031                 } else {
1032                         if (_calendar_is_recurrent_record(record)) {
1033                                 __cal_week_show_popup(p, C_("IDS_CLD_OPT_DELETE_EVENT"), EINA_FALSE);
1034                         } else {
1035                                 p->popup = cal_util_add_popup(p->ad->win, NULL, NULL, S_("IDS_COM_POP_DELETE_Q"), __cal_week_delete_norepeat_popup_response_event_callback, p,
1036                                         S_("IDS_COM_BODY_DELETE"),S_("IDS_COM_SK_CANCEL"), NULL);
1037                         }
1038                 }
1039         }
1040 }
1041
1042 static void __cal_week_share_popup_response_callback(void *data, Evas_Object *obj, void *ei)
1043 {
1044         c_retm_if(!data, "data is null");
1045         c_retm_if(!obj, "obj is null");
1046
1047         Evas_Object *popup = data;
1048
1049         cal_week_data* p = evas_object_data_get(popup, "data");
1050         c_retm_if(!p, "p is null");
1051
1052         evas_object_del(popup);
1053         p->popup = NULL;
1054
1055 }
1056
1057 static inline void __cal_week_delete_popup(cal_week_data *p)
1058 {
1059         c_retm_if(!p || !p->popup, "param is null");
1060
1061         evas_object_del(p->popup);
1062         p->popup = NULL;
1063 }
1064
1065
1066 static void __cal_week_via_message_callback(void *data, Evas_Object *obj, void *ei)
1067 {
1068         c_retm_if(!data, "data is null");
1069         c_retm_if(!obj, "obj is null");
1070
1071         cal_week_data *p = data;
1072         c_retm_if(!p->ad, "p->ad is null");
1073
1074         calendar_record_h record = _calendar_get_record_with_index(p->ad->cid);
1075
1076         _calendar_export_record_to_vcs(record, CAL_MESSAGE_VCS);
1077
1078         p->ad->ug = cal_launch_ug_with_var(NULL, CAL_MESSAGE_COMPOSER_UG, NULL, "ATTACHFILE", CAL_MESSAGE_VCS, NULL);
1079
1080         __cal_week_delete_popup(p);
1081 }
1082
1083 static void __cal_week_via_email_callback(void *data, Evas_Object *obj, void *ei)
1084 {
1085         c_retm_if(!data, "data is null");
1086         c_retm_if(!obj, "obj is null");
1087
1088         cal_week_data *p = data;
1089         c_retm_if(!p->ad, "p->ad is null");
1090
1091         calendar_record_h record = _calendar_get_record_with_index(p->ad->cid);
1092
1093         _calendar_export_record_to_vcs(record, CAL_MESSAGE_VCS);
1094
1095         p->ad->ug = cal_launch_ug_with_var(NULL, CAL_EMAIL_COMPOSER_UG, (struct ug_cbs *)p->ad, "RUN_TYPE", "5", "ATTACHMENT", CAL_EMAIL_VCS, NULL);
1096
1097         __cal_week_delete_popup(p);
1098 }
1099
1100 static void __cal_week_via_bluetooth_callback(void *data, Evas_Object *obj, void *ei)
1101 {
1102         c_retm_if(!data, "data is null");
1103         c_retm_if(!obj, "obj is null");
1104
1105         cal_week_data *p = data;
1106         c_retm_if(!p->ad, "p->ad is null");
1107
1108         calendar_record_h record = _calendar_get_record_with_index(p->ad->cid);
1109
1110         _calendar_export_record_to_vcs(record, CAL_MESSAGE_VCS);
1111
1112         p->ad->ug = cal_launch_ug_with_var(NULL, CAL_BLUETOOTH_UG, NULL, "launch-type", "send", "filecount", "1", "files", CAL_BLUETOOTH_VCS, NULL);
1113
1114         __cal_week_delete_popup(p);
1115 }
1116
1117 static void __cal_week_share_call_callback(void *data, Evas_Object *obj, void *ei)
1118 {
1119         c_ret_if(!data);
1120         c_ret_if(!obj);
1121         c_ret_if(!ei);
1122
1123         cal_week_genlist_item_data *item_data = data;
1124         cal_week_data *p = item_data->p;
1125         c_ret_if(!p );
1126
1127         calendar_record_h record = item_data->record;
1128         c_ret_if(!record);
1129
1130         elm_ctxpopup_dismiss(obj);
1131
1132         __cal_week_init_ctx_data(record, p);
1133
1134         p->popup = cal_util_add_popup(p->ad->win, "menustyle", S_("IDS_COM_BUTTON_SHARE"), NULL,
1135                                 __cal_week_share_popup_response_callback, p,
1136                                 S_("IDS_COM_BODY_CLOSE"),  NULL);
1137
1138         c_retm_if(!p->popup, "cal_util_add_popup returned null");
1139         evas_object_data_set(p->popup, "data", p);
1140
1141         Evas_Object *genlist = elm_genlist_add(p->popup);
1142         c_retm_if(!genlist, "elm_genlist_add returned null");
1143
1144         elm_genlist_item_append(genlist, &itc_1text, S_("IDS_COM_BODY_EMAIL"), NULL, ELM_GENLIST_ITEM_NONE, __cal_week_via_email_callback, p);
1145         elm_genlist_item_append(genlist, &itc_1text, S_("IDS_COM_BODY_MESSAGE"), NULL, ELM_GENLIST_ITEM_NONE, __cal_week_via_message_callback, p);
1146         elm_genlist_item_append(genlist, &itc_1text, S_("IDS_COM_BODY_BLUETOOTH"), NULL, ELM_GENLIST_ITEM_NONE, __cal_week_via_bluetooth_callback, p);
1147
1148         elm_object_content_set(p->popup, genlist);
1149         evas_object_show(p->popup);
1150 }
1151
1152 static void __cal_week_new_call_callback(void *data, Evas_Object *obj, void *ei)
1153 {
1154         c_ret_if(!data);
1155         c_ret_if(!obj);
1156         c_ret_if(!ei);
1157
1158         cal_week_genlist_item_data *item_data = data;
1159
1160         cal_week_data *p = item_data->p;
1161         c_ret_if(!p );
1162
1163         struct appdata *ad = p->ad;
1164         c_ret_if(!ad);
1165
1166         ad->base_hour = -1;
1167         ad->cid = 0;
1168
1169         elm_ctxpopup_dismiss(obj);
1170
1171         calendar_record_h record = item_data->record;
1172         c_ret_if(!record);
1173
1174         cal_edit_create_view(p->ad, record, NULL, NULL);
1175 }
1176
1177 static void __cal_week_show_ctx_popup(cal_week_genlist_item_data *item_data, cal_week_data *p)
1178 {
1179         c_ret_if(!item_data);
1180         c_ret_if(!p);
1181
1182         if (p->ctx) {
1183                 evas_object_del(p->ctx);
1184                 p->ctx = NULL;
1185         }
1186
1187         p->ctx = elm_ctxpopup_add(p->ad->naviframe);
1188         c_ret_if(!p->ctx);
1189
1190         evas_object_smart_callback_add(p->ctx, "dismissed", __cal_week_ctx_popup_hide_callback, p);
1191
1192         {
1193                 elm_ctxpopup_item_append(p->ctx, S_("IDS_COM_SK_EDIT"), NULL, __cal_week_edit_call_callback, item_data);
1194                 elm_ctxpopup_item_append(p->ctx, S_("IDS_COM_BODY_DELETE"), NULL, __cal_week_delete_call_callback, item_data);
1195         }
1196
1197         elm_ctxpopup_item_append(p->ctx, S_("IDS_COM_BUTTON_SHARE"), NULL, __cal_week_share_call_callback, item_data);
1198         elm_ctxpopup_item_append(p->ctx, S_("IDS_COM_SK_NEW"), NULL, __cal_week_new_call_callback, item_data);
1199
1200         elm_ctxpopup_direction_priority_set(p->ctx, ELM_CTXPOPUP_DIRECTION_DOWN,
1201                                                                                                                         ELM_CTXPOPUP_DIRECTION_RIGHT,
1202                                                                                                                         ELM_CTXPOPUP_DIRECTION_LEFT,
1203                                                                                                                         ELM_CTXPOPUP_DIRECTION_UP);
1204         evas_object_data_set(p->ctx, "priv", p);
1205         evas_object_move(p->ctx, p->down.x, p->down.y);
1206         evas_object_show(p->ctx);
1207
1208
1209 }
1210
1211 static void __cal_week_add_event(Eina_List *list, int day_index, cal_week_data *p)
1212 {
1213         c_ret_if(!list);
1214         c_ret_if(!p);
1215
1216         Eina_List *l = NULL;
1217         calendar_record_h record = NULL;
1218
1219         int ncount = eina_list_count(list);
1220
1221         Evas_Object *box = cal_util_add_box(p->ly);
1222         c_ret_if(!box);
1223
1224         Evas_Object *genlist = elm_genlist_add(box);
1225         c_ret_if(!genlist);
1226
1227         evas_object_data_set(genlist, "priv", p);
1228         evas_object_size_hint_align_set(genlist, EVAS_HINT_FILL, EVAS_HINT_FILL);
1229         evas_object_size_hint_weight_set(genlist, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1230         evas_object_show(box);
1231         evas_object_show(genlist);
1232
1233         EINA_LIST_FOREACH(list, l, record) {
1234                 if (record) {
1235                         cal_week_genlist_item_data *item_data = calloc(1, sizeof(cal_week_genlist_item_data));
1236                         c_ret_if(!item_data);
1237
1238                         item_data->record = record;
1239                         item_data->p = p;
1240                         item_data->it = elm_genlist_item_append(genlist, &_itc, item_data, NULL, ELM_GENLIST_ITEM_NONE, __cal_week_cal_genlist_item_select_callback, record);
1241                         if (item_data->it)
1242                                 elm_object_item_data_set(item_data->it, item_data);
1243                 }
1244         }
1245
1246         elm_box_pack_start(box, genlist);
1247         elm_object_part_content_set(p->ly, cal_util_get_part_text("wdb/%d/sw", day_index), box);
1248
1249         evas_object_data_set(genlist, "pos", (void *)day_index);
1250         evas_object_data_set(genlist, "priv", p);
1251
1252         evas_object_propagate_events_set(genlist, EINA_FALSE);
1253
1254         evas_object_event_callback_add(genlist, EVAS_CALLBACK_MOUSE_DOWN, __cal_week_genlist_mouse_down_event_callback, p);
1255         evas_object_event_callback_add(genlist, EVAS_CALLBACK_MOUSE_UP, __cal_week_genlist_mouse_up_event_callback, p);
1256         evas_object_event_callback_add(genlist, EVAS_CALLBACK_MOUSE_MOVE, __cal_week_genlist_mouse_move_event_callback, p);
1257
1258         p->event_count[day_index] = ncount;
1259
1260         __cal_week_set_more_text(p,
1261                         week_data_get_overflow_count(ncount, day_index, false, p->ad->is_landscape_mode),
1262                         day_index);
1263 }
1264
1265 static void __cal_week_unset_week_day_content(cal_week_data *p)
1266 {
1267         c_ret_if(!p);
1268         c_ret_if(!p->ly);
1269
1270         int i = 0;
1271
1272         for (i = 0; i < WEEK_DAY_NUMS; i++) {
1273
1274                 Evas_Object *box = elm_object_part_content_get(p->ly, cal_util_get_part_text("wdb/%d/sw", i));
1275                 c_ret_if(!box);
1276
1277                 elm_object_part_content_unset(p->ly, cal_util_get_part_text("wdb/%d/sw", i));
1278
1279                 evas_object_del(box);
1280         }
1281 }
1282
1283 static void __cal_week_get_week_events(cal_week_data *p)
1284 {
1285         c_ret_if(!p);
1286
1287         struct appdata *ad = p->ad;
1288         c_ret_if(!ad);
1289
1290         Eina_List *event_list = NULL;
1291
1292         __cal_week_unset_week_day_content(p);
1293
1294         struct tm tm_start = ad->base_tm;
1295         tm_start.tm_hour = 0;
1296         tm_start.tm_min = 0;
1297         tm_start.tm_sec = 0;
1298
1299         cal_util_update_tm_day(&tm_start, ad->wday_start - tm_start.tm_wday);
1300
1301         struct tm tm_end = tm_start;
1302         tm_end.tm_hour = 23;
1303         tm_end.tm_min = 59;
1304         tm_end.tm_sec = 59;
1305
1306         int day_index = 0;
1307         for (day_index = 0; day_index < 7; day_index++) {
1308
1309                 p->event_count[day_index] = 0;
1310
1311                 event_list = _calendar_get_all_instance_list(&tm_start, &tm_end);
1312
1313                 Eina_List *todo_list = _calendar_get_due_date_task_list(&tm_start, &tm_end, p->ad->is_display_complete_todo, _CALENDAR_TASK_SORT_TYPE_DUEDATE_ASC);
1314
1315                 event_list = eina_list_merge(event_list, todo_list);
1316
1317                 if (event_list) {
1318
1319                         p->week_event_list = eina_list_append(p->week_event_list, event_list);
1320                         __cal_week_add_event(event_list, day_index, p);
1321
1322                         event_list = NULL;
1323                 }
1324
1325                 cal_util_update_tm_day(&tm_start, 1);
1326                 cal_util_update_tm_day(&tm_end, 1);
1327         }
1328 }
1329
1330 Evas_Object* cal_week_create_view(struct appdata *ad, Evas_Object *main)
1331 {
1332         CAL_FN_START;
1333
1334         c_retv_if(!ad, NULL);
1335         c_retv_if(!main, NULL);
1336
1337         cal_week_data *p = calloc(1, sizeof(cal_week_data));
1338         c_retv_if(!p, NULL);
1339
1340         if (!ad->is_landscape_mode) {
1341                 p->name = "main/week/small";
1342         } else {
1343                 p->name = "landscape/main/week/small";
1344         }
1345
1346         p->ad = ad;
1347         ad->tapbar_focus_view = CV_WEEK;
1348         p->parent = main;
1349         p->pos_down = -1;
1350         p->expanded_week_day = -1;
1351         Evas_Object *ly = cal_util_add_layout(ad->naviframe, p->name);
1352         if (!ly) {
1353                 free(p);
1354                 return NULL;
1355         }
1356
1357         evas_object_data_set(ly, "priv", p);
1358         p->ly = ly;
1359         p->pos_selected = 0;
1360
1361         __cal_week_fill_days(ly, p, &ad->base_tm, ad->wday_start);
1362
1363         edje_object_signal_callback_add(CAL_UTIL_GET_EDJ_DATA(ly), "*", "cal", __cal_week_cal_event_callback, p);
1364         edje_object_signal_callback_add(CAL_UTIL_GET_EDJ_DATA(ly), "*", "more", __cal_week_more_event_callback, p);
1365         edje_object_signal_callback_add(CAL_UTIL_GET_EDJ_DATA(ly), "*", "down", __cal_week_down_event_callback, p);
1366
1367         evas_object_event_callback_add(ly, EVAS_CALLBACK_MOUSE_DOWN, __cal_week_mouse_down_event_callback, p);
1368         evas_object_event_callback_add(ly, EVAS_CALLBACK_MOUSE_UP, __cal_week_mouse_up_event_callback, p);
1369         evas_object_event_callback_add(ly, EVAS_CALLBACK_MOUSE_MOVE, __cal_week_mouse_move_event_callback, p);
1370         evas_object_event_callback_add(ly, EVAS_CALLBACK_RESIZE, __cal_week_resize_view, p);
1371         evas_object_event_callback_add(ly, EVAS_CALLBACK_DEL, __cal_week_delete_layout, NULL);
1372
1373         cal_lang_manager_add_callback(__cal_week_text_refresh_callback, p);
1374
1375         return ly;
1376 }
1377
1378 void cal_week_update_view(Evas_Object *ly)
1379 {
1380         CAL_FN_START;
1381
1382         c_ret_if(!ly);
1383
1384         cal_week_data *p = CAL_UTIL_GET_PRIV_DATA(ly);
1385         c_ret_if(!p);
1386
1387         struct appdata *ad = p->ad;
1388         c_ret_if(!ad);
1389
1390         __cal_week_free_week_event_list(p);
1391
1392         __cal_week_update_days(p->ly, p, &ad->base_tm, ad->wday_start);
1393
1394         __cal_week_update_select(p, __cal_week_get_position(p, &ad->base_tm, ad->wday_start));
1395
1396         __cal_week_get_week_events(p);
1397
1398         cal_util_delete_evas_object(&p->left);
1399         cal_util_delete_evas_object(&p->right);
1400
1401 }
1402
1403 static inline void __cal_week_cal_expand_up(cal_week_data *p, int pos)
1404 {
1405         cal_util_emit_signal(CAL_UTIL_GET_EDJ_DATA(p->ly), "set_expand_up,%d", pos);
1406         __cal_week_set_more_text(p,
1407             week_data_get_overflow_count(p->event_count[pos], pos, true, p->ad->is_landscape_mode),
1408             pos);
1409 }
1410
1411 static inline void __cal_week_cal_expand_up_1(cal_week_data *p, int pos)
1412 {
1413         cal_util_emit_signal(CAL_UTIL_GET_EDJ_DATA(p->ly), "set_expand_up_1,%d", pos);
1414         __cal_week_set_more_text(p,
1415             week_data_get_overflow_count(p->event_count[pos], pos, true, p->ad->is_landscape_mode),
1416             pos);
1417 }
1418
1419 static inline void __cal_week_cal_expand_up_2(cal_week_data *p, int pos)
1420 {
1421         cal_util_emit_signal(CAL_UTIL_GET_EDJ_DATA(p->ly), "set_expand_up_2,%d", pos);
1422         __cal_week_set_more_text(p,
1423             week_data_get_overflow_count(p->event_count[pos], pos, true, p->ad->is_landscape_mode),
1424             pos);
1425 }
1426
1427 static inline void __cal_week_cal_expand_down(cal_week_data *p, int pos)
1428 {
1429         cal_util_emit_signal(CAL_UTIL_GET_EDJ_DATA(p->ly), "set_expand_down,%d", pos);
1430         __cal_week_set_more_text(p,
1431             week_data_get_overflow_count(p->event_count[pos], pos, true, p->ad->is_landscape_mode),
1432             pos);
1433 }
1434
1435 static inline void __cal_week_cal_expand_default(cal_week_data *p, int pos)
1436 {
1437         cal_util_emit_signal(CAL_UTIL_GET_EDJ_DATA(p->ly), "set_expand_default,%d", pos);
1438         __cal_week_set_more_text(p,
1439             week_data_get_overflow_count(p->event_count[pos], pos, false, p->ad->is_landscape_mode),
1440             pos);
1441 }
1442
1443 static inline void __cal_week_cal_collapse_up(cal_week_data *p, int pos)
1444 {
1445         cal_util_emit_signal(CAL_UTIL_GET_EDJ_DATA(p->ly), "set_expand_hide_up,%d", pos);
1446         __cal_week_set_more_text(p, p->event_count[pos], pos);
1447 }
1448
1449 static inline void __cal_week_cal_collapse_down(cal_week_data *p, int pos)
1450 {
1451         cal_util_emit_signal(CAL_UTIL_GET_EDJ_DATA(p->ly), "set_expand_hide_down,%d", pos);
1452         __cal_week_set_more_text(p, p->event_count[pos], pos);
1453 }
1454
1455 static void __cal_week_cal_expand_week_day(cal_week_data *p, int pos)
1456 {
1457         c_retm_if(!p, "p is null");
1458         c_retm_if(-1 == pos, "pos is -1");
1459         c_retm_if(p->expanded_week_day == pos, "Week day was expanded.");
1460
1461         if (-1 != p->expanded_week_day) {
1462                 __cal_week_cal_unexpand_week_day(p, p->expanded_week_day);
1463                 cal_util_emit_signal(CAL_UTIL_GET_EDJ_DATA(p->ly), "show,%d,more", p->expanded_week_day);
1464         }
1465
1466         cal_util_emit_signal(CAL_UTIL_GET_EDJ_DATA(p->ly), "hide,%d,more", pos);
1467
1468         if (!p->ad->is_landscape_mode) {
1469                 switch (pos) {
1470                 case 0:
1471                         __cal_week_cal_expand_down(p, pos);
1472                         __cal_week_cal_collapse_down(p, pos+2);
1473                         break;
1474                 case 1:
1475                         __cal_week_cal_expand_down(p, pos);
1476                         __cal_week_cal_collapse_down(p, pos+2);
1477                         __cal_week_cal_collapse_down(p, pos+4);
1478                         __cal_week_cal_collapse_down(p, pos+5);
1479                         break;
1480                 case 2:
1481                         __cal_week_cal_collapse_up(p, pos-2);
1482                         __cal_week_cal_expand_up(p, pos);
1483                         __cal_week_cal_collapse_down(p, pos+2);
1484                         break;
1485                 case 3:
1486                         __cal_week_cal_expand_up(p, pos);
1487                         __cal_week_cal_collapse_up(p, pos-2);
1488                         __cal_week_cal_collapse_down(p, pos+2);
1489                         __cal_week_cal_collapse_down(p, pos+3);
1490                         break;
1491                 case 4:
1492                         __cal_week_cal_collapse_up(p, pos-4);
1493                         __cal_week_cal_collapse_up(p, pos-2);
1494                         __cal_week_cal_expand_up(p, pos);
1495                         break;
1496                 case 5:
1497                         __cal_week_cal_collapse_up(p, pos-4);
1498                         __cal_week_cal_collapse_up(p, pos-2);
1499                         __cal_week_cal_expand_up_1(p, pos);
1500                         __cal_week_cal_collapse_down(p, pos+1);
1501                         break;
1502                 case 6:
1503                         __cal_week_cal_collapse_up(p, pos-5);
1504                         __cal_week_cal_collapse_up(p, pos-3);
1505                         __cal_week_cal_collapse_up(p, pos-1);
1506                         __cal_week_cal_expand_up_2(p, pos);
1507                         break;
1508                 default:
1509                         break;
1510                 }
1511         } else {
1512                 switch (pos) {
1513                 case 0:
1514                 case 1:
1515                         __cal_week_cal_expand_down(p, pos);
1516                         __cal_week_cal_collapse_down(p, pos+3);
1517                         break;
1518                 case 2:
1519                         __cal_week_cal_expand_down(p, pos);
1520                         __cal_week_cal_collapse_down(p, pos+3);
1521                         __cal_week_cal_collapse_down(p, pos+4);
1522                         break;
1523                 case 3:
1524                 case 4:
1525                         __cal_week_cal_expand_up(p, pos);
1526                         __cal_week_cal_collapse_up(p, pos-3);
1527                         break;
1528                 case 5:
1529                         __cal_week_cal_expand_up_1(p, pos);
1530                         __cal_week_cal_collapse_up(p, pos-3);
1531                         __cal_week_cal_collapse_down(p, pos+1);
1532                         break;
1533                 case 6:
1534                         __cal_week_cal_expand_up_2(p, pos);
1535                         __cal_week_cal_collapse_up(p, pos-1);
1536                         __cal_week_cal_collapse_up(p, pos-4);
1537                         break;
1538                 default:
1539                         break;
1540                 }
1541         }
1542         p->expanded_week_day = pos;
1543 }
1544
1545 static void __cal_week_cal_unexpand_week_day(cal_week_data *p, int pos)
1546 {
1547         c_retm_if(!p, "p is null");
1548         c_retm_if(-1 == pos, "pos is -1");
1549         c_retm_if(p->expanded_week_day != pos, "Week day was unexpanded.");
1550
1551         __cal_week_cal_expand_default(p, pos);
1552         cal_util_emit_signal(CAL_UTIL_GET_EDJ_DATA(p->ly), "show,%d,more", pos);
1553
1554         if (!p->ad->is_landscape_mode) {
1555                 switch (pos) {
1556                 case 0:
1557                         __cal_week_cal_expand_default(p, pos);
1558                         __cal_week_cal_expand_default(p, pos+2);
1559                         __cal_week_cal_expand_default(p, pos+4);
1560                         break;
1561                 case 1:
1562                         __cal_week_cal_expand_default(p, pos);
1563                         __cal_week_cal_expand_default(p, pos+2);
1564                         __cal_week_cal_expand_default(p, pos+4);
1565                         __cal_week_cal_expand_default(p, pos+5);
1566                         break;
1567                 case 2:
1568                         __cal_week_cal_expand_default(p, pos-2);
1569                         __cal_week_cal_expand_default(p, pos);
1570                         __cal_week_cal_expand_default(p, pos+2);
1571                         break;
1572                 case 3:
1573                         __cal_week_cal_expand_default(p, pos-2);
1574                         __cal_week_cal_expand_default(p, pos);
1575                         __cal_week_cal_expand_default(p, pos+2);
1576                         __cal_week_cal_expand_default(p, pos+3);
1577                         break;
1578                 case 4:
1579                         __cal_week_cal_expand_default(p, pos-4);
1580                         __cal_week_cal_expand_default(p, pos-2);
1581                         __cal_week_cal_expand_default(p, pos);
1582                         break;
1583                 case 5:
1584                         __cal_week_cal_expand_default(p, pos-4);
1585                         __cal_week_cal_expand_default(p, pos-2);
1586                         __cal_week_cal_expand_default(p, pos);
1587                         __cal_week_cal_expand_default(p, pos+1);
1588                         break;
1589                 case 6:
1590                         __cal_week_cal_expand_default(p, pos);
1591                         __cal_week_cal_expand_default(p, pos-1);
1592                         __cal_week_cal_expand_default(p, pos-3);
1593                         __cal_week_cal_expand_default(p, pos-5);
1594                         break;
1595                 default:
1596                         break;
1597                 }
1598         } else {
1599                 switch (pos) {
1600                 case 0:
1601                 case 1:
1602                         __cal_week_cal_expand_default(p, pos+3);
1603                         break;
1604                 case 2:
1605                         __cal_week_cal_expand_default(p, pos+3);
1606                         __cal_week_cal_expand_default(p, pos+4);
1607                         break;
1608                 case 3:
1609                 case 4:
1610                         __cal_week_cal_expand_default(p, pos-3);
1611                         break;
1612                 case 5:
1613                         __cal_week_cal_expand_default(p, pos-3);
1614                         __cal_week_cal_expand_default(p, pos+1);
1615                         break;
1616                 case 6:
1617                         __cal_week_cal_expand_default(p, pos-4);
1618                         __cal_week_cal_expand_default(p, pos-1);
1619                         break;
1620                 default:
1621                         break;
1622                 }
1623         }
1624         p->expanded_week_day = -1;
1625 }
1626
1627
1628 static void __cal_week_more_event_callback(void *data, Evas_Object *obj, const char *e, const char *src)
1629 {
1630         CAL_FN_START;
1631
1632         c_retm_if(!data, "data is null");
1633         cal_week_data *p = data;
1634
1635         if (p->ign) {
1636                 p->ign = 0;
1637                 ERR("");
1638                 return;
1639         }
1640
1641         struct appdata *ad = p->ad;
1642         c_retm_if(!ad, "ad is null");
1643
1644         int pos = atoi(e);
1645
1646         int d = pos - p->pos_selected;
1647
1648         cal_util_update_tm_day(&ad->base_tm, d);
1649
1650         __cal_week_update_select(p, pos);
1651
1652         if (pos == p->expanded_week_day)
1653                 __cal_week_cal_unexpand_week_day(p, p->expanded_week_day);
1654         else
1655                 __cal_week_cal_expand_week_day(p, pos);
1656 }
1657
1658 static void __cal_week_genlist_mouse_up_event_callback(void *data, Evas *e, Evas_Object *ly, void *ei)
1659 {
1660         c_retm_if(!data, "data is null");
1661         c_retm_if(!ly, "ly is null");
1662
1663         cal_week_data *p = data;
1664         int pos = (int)evas_object_data_get(ly, "pos");
1665         snprintf(signal_source, 31, "wdb/%d/base", pos);
1666         __cal_week_mouse_up_event_callback(p, NULL, p->ly, ei);
1667         edje_object_signal_emit(CAL_UTIL_GET_EDJ_DATA(p->ly), "mouse,up,1", signal_source);
1668         edje_object_signal_emit(CAL_UTIL_GET_EDJ_DATA(p->ly), "mouse,clicked,1", signal_source);
1669 }
1670
1671 static void __cal_week_genlist_mouse_down_event_callback(void *data, Evas *e, Evas_Object *ly, void *ei)
1672 {
1673         c_retm_if(!data, "data is null");
1674         c_retm_if(!ly, "ly is null");
1675
1676         cal_week_data *p = data;
1677         int pos = (int)evas_object_data_get(ly, "pos");
1678         snprintf(signal_source, 31, "wdb/%d/base", pos);
1679         __cal_week_mouse_down_event_callback(p, NULL, p->ly, ei);
1680         edje_object_signal_emit(CAL_UTIL_GET_EDJ_DATA(p->ly), "mouse,down,1", signal_source);
1681 }
1682
1683 static void __cal_week_genlist_mouse_move_event_callback(void *data, Evas *e, Evas_Object *ly, void *ei)
1684 {
1685         c_retm_if(!data, "data is null");
1686         c_retm_if(!ly, "ly is null");
1687
1688         cal_week_data *p = data;
1689         __cal_week_mouse_move_event_callback(p, NULL, p->ly, ei);
1690 }
1691
1692 static void __cal_week_set_more_text(cal_week_data *p, int count, int pos)
1693 {
1694         c_retm_if(!p, "p is null");
1695         c_retm_if(pos < 0, "pos is wrong");
1696         c_retm_if(!p->ly, "p->ly is null");
1697
1698         char part[32] = {0};
1699         char text[32] = {0};
1700
1701         snprintf(part, sizeof part, "wdb/%d/more", pos);
1702
1703         if (0 < count)
1704                 snprintf(text, sizeof text, "+%d", count);
1705         edje_object_part_text_set(CAL_UTIL_GET_EDJ_DATA(p->ly), part, text);
1706 }