639649f2db12252b377c611bebe71294ce3a31f0
[apps/home/calendar.git] / src / view-main-month-cal.c
1 /*
2
3 Copyright (c) 2000-2012 Samsung Electronics Co., Ltd All Rights Reserved
4
5 This file is part of org.tizen.efl-calendar
6 Written by Taeho Kang <taeho84.kang@samsung.com>
7
8 PROPRIETARY/CONFIDENTIAL
9
10 This software is the confidential and proprietary information of
11 SAMSUNG ELECTRONICS ("Confidential Information"). You shall not
12 disclose such Confidential Information and shall use it only in
13 accordance with the terms of the license agreement you entered
14 into with SAMSUNG ELECTRONICS.
15
16 SAMSUNG make no representations or warranties about the suitability
17 of the software, either express or implied, including but not limited
18 to the implied warranties of merchantability, fitness for a particular
19 purpose, or non-infringement. SAMSUNG shall not be liable for any
20 damages suffered by licensee as a result of using, modifying or
21 distributing this software or its derivatives.
22
23 */
24
25
26 #include "view.h"
27 #include "detail.h"
28
29 enum {
30         ST_PREV = -1,
31         ST_CURR = 0,
32         ST_NEXT = 1,
33 };
34
35 struct _day {
36         int enabled:1;
37         int checked:1;
38 };
39
40 typedef struct {
41         int cid;
42         int is_repeat;
43         int line;
44         int width;
45         int color;
46         int showed;
47         char title[512];
48         time_t start_time;
49         time_t end_time;
50 }weekly_event;
51
52 typedef struct {
53         const char *name;
54         struct appdata *ad;
55         Evas_Object *parent;
56         Evas_Object *ly;
57         Evas_Object *month;
58         struct calsvc *csv;
59         Evas_Object *gl;
60         Evas_Object *popup;
61
62         int base_pos_for_weekly;
63         int index_for_press;
64         struct tm cur_tm;
65         struct _day day[42];
66         int pos_today;
67         int pos_selected;
68         int pos_start;
69         int pos_end;
70         int pos_down;
71         GList* list[7];
72         int weekline;
73         int down_x;
74         int down_y;
75 } cal_view_main_month_cal_data;
76
77 static const char *_name = NULL;
78 static Calendar_color calendar_color;
79
80 static Evas_Object *__cal_month_calendar_get_genlist_item_icon(void *data, Evas_Object *obj, const char *part);
81 static char* __cal_month_calendar_get_genlist_item_label(void *data, Evas_Object *obj, const char *part);
82 static void __cal_month_calendar_signal_week(cal_view_main_month_cal_data *p,Evas_Object *eo);
83 static int __cal_month_calendar_get_weekly_events(cal_view_main_month_cal_data *p);
84
85 static Elm_Genlist_Item_Class itc = {
86         .item_style = "3text.5icon",
87         .func.content_get = __cal_month_calendar_get_genlist_item_icon,
88         .func.text_get = __cal_month_calendar_get_genlist_item_label,
89 };
90
91 static Evas_Object *__cal_month_calendar_get_genlist_item_icon(void *data, Evas_Object *obj, const char *part)
92 {
93         c_retvm_if(!data, NULL, "data is null");
94         c_retvm_if(!obj, NULL, "obj is null");
95
96         struct tmnode *tm = data;
97         c_retvm_if(!tm->cs, NULL, "tm->cs is null");
98
99         cal_view_main_month_cal_data* p = CAL_UTIL_GET_PRIV_DATA(obj);
100         c_retvm_if(!p, NULL, "p is null");
101
102         int r;
103         Evas_Object *icon = NULL;
104         cal_struct *calendar = NULL;
105
106         if (!CAL_STRCMP(part, "elm.swallow.colorbar"))
107         {
108                 r = CALENDAR_SVC_GET(CAL_STRUCT_CALENDAR, CALENDAR_SVC_STRUCT_GET_INT(tm->cs,CAL_VALUE_INT_CALENDAR_ID), NULL, &calendar);
109                 if (r != CAL_SUCCESS)
110                 {
111                         ERR("CALENDAR_SVC_GET fail. %d", r);
112                         return NULL;
113                 }
114
115                 icon = evas_object_rectangle_add(evas_object_evas_get(obj));
116                 evas_object_size_hint_align_set(icon, EVAS_HINT_FILL, EVAS_HINT_FILL);
117
118                 CALENDAR_SVC_GET_CALENDAR_COLOR(calendar, &calendar_color);
119
120                 evas_object_color_set(icon, calendar_color.red, calendar_color.green, calendar_color.blue, calendar_color.alpha);
121
122                 CALENDAR_SVC_STRUCT_FREE(&calendar);
123
124                 return icon;
125         }
126
127         return NULL;
128 }
129
130 static void __cal_month_calendar_make_time_text(time_t t, struct tm *tm, char *buf, int sz)
131 {
132         struct tm ttm;
133         const char* date;
134         const char* time;
135
136         localtime_r(&t, &ttm);
137
138         time = CAL_UTIL_TIME_FORMAT_6;
139
140         if (ttm.tm_year != tm->tm_year)
141                 date = CAL_UTIL_DATE_FORMAT_11;
142         else if (ttm.tm_mon != tm->tm_mon)
143                 date = CAL_UTIL_DATE_FORMAT_10;
144         else if (ttm.tm_mday != tm->tm_mday)
145                 date = CAL_UTIL_DATE_FORMAT_9;
146         else
147                 date = NULL;
148
149         cal_util_get_time_text(buf, sz, NULL, time, &ttm);
150 }
151
152
153 static char* __cal_month_calendar_get_time_text(struct tmnode *tm, cal_view_main_month_cal_data *p)
154 {
155         char buf[512];
156         char stime[512];
157
158         __cal_month_calendar_make_time_text(tm->st, &p->ad->base_tm, stime, sizeof(stime));
159
160         snprintf(buf, sizeof(buf), "%s", stime);
161
162         return strdup(buf);
163 }
164
165 static char* __cal_month_calendar_get_genlist_item_label(void *data, Evas_Object *obj, const char *part)
166 {
167         c_retvm_if(!data, NULL, "data is null");
168
169         const char *str;
170
171         struct tmnode *tm = data;
172         c_retvm_if(!tm->cs, NULL, "tm->cs is null");
173
174         if (!CAL_STRCMP(part, "elm.text.1"))
175         {
176                 str = CALENDAR_SVC_STRUCT_GET_STR(tm->cs, CAL_VALUE_TXT_SUMMARY);
177                 if (str)
178                         return strdup(str);
179
180                 return strdup(C_("IDS_CLD_BODY_NO_TITLE"));
181         }
182         else if (!CAL_STRCMP(part, "elm.text.2"))
183         {
184                 str = CALENDAR_SVC_STRUCT_GET_STR(tm->cs, CAL_VALUE_TXT_LOCATION);
185                 if (str)
186                         return strdup(str);
187
188                 return strdup(C_("IDS_CLD_BODY_NO_LOCATION_SELECTED"));
189         }
190         else if (!CAL_STRCMP(part, "elm.text.3"))
191         {
192                 int allday = CALENDAR_SVC_STRUCT_GET_INT(tm->cs, CAL_VALUE_INT_ALL_DAY_EVENT);
193                 if (allday)
194                         return strdup(C_("IDS_COM_BODY_ALL_DAY"));
195
196                 cal_view_main_month_cal_data *p = CAL_UTIL_GET_PRIV_DATA(obj);
197                 c_retvm_if(!p, NULL, "p is null");
198
199                 return __cal_month_calendar_get_time_text(tm, p);
200         }
201
202         return NULL;
203 }
204
205 static int __cal_month_calendar_get_first_wday(int mday, int wday)
206 {
207         return CAL_UTIL_GET_WDAY(wday - mday + 1);
208 }
209
210 static void __cal_month_calendar_delete_layout(void *data, Evas *e, Evas_Object *obj, void *ei)
211 {
212         CAL_FN_START;
213
214         c_retm_if(!data, "data is null");
215         cal_view_main_month_cal_data *p = data;
216
217         struct appdata *ad = p->ad;
218         c_retm_if(!ad, "ad is null");
219
220         if (ad->today_edj && ad->today_edj == CAL_UTIL_GET_EDJ_DATA(obj))
221                 ad->today_edj = NULL;
222
223         if (p->popup) {
224                 evas_object_del(p->popup);
225         }
226
227         free(p);
228
229         CAL_FN_END;
230 }
231
232 static inline void __cal_month_calendar_set_color(Evas_Object *ly, int pos, int wday)
233 {
234         const char *p;
235
236         switch (wday) {
237         case 0: p = "sunday"; break;
238         case 6: p = "satday"; break;
239         default: p = "weekday";
240         }
241
242         cal_util_emit_signal(CAL_UTIL_GET_EDJ_DATA(ly), "%s,%d", p, pos);
243 }
244
245 static inline void __cal_month_calendar_set_disabled(Evas_Object *ly, cal_view_main_month_cal_data *p, int pos)
246 {
247         struct _day *d = &p->day[pos];
248
249         cal_util_emit_signal(CAL_UTIL_GET_EDJ_DATA(ly), "disable,%d", pos);
250
251         d->enabled = 0;
252 }
253
254 static inline void __cal_month_calendar_set_enabled(Evas_Object *ly, cal_view_main_month_cal_data *p, int pos, int wday)
255 {
256         struct _day *d = &p->day[pos];
257
258         if (!d->enabled) {
259                 cal_util_emit_signal(CAL_UTIL_GET_EDJ_DATA(ly), "enable,%d", pos);
260
261                 __cal_month_calendar_set_color(ly, pos, wday);
262                 d->enabled = 1;
263         }
264 }
265
266 static inline void __cal_month_calendar_set_today(Evas_Object *eo, int pos)
267 {
268         if (pos == -1)
269                 return;
270
271         cal_util_emit_signal(eo, "today,%d", pos);
272 }
273
274 static inline void __cal_month_calendar_unset_today(Evas_Object *eo, int *pos)
275 {
276         if (*pos == -1)
277                 return;
278
279         cal_util_emit_signal(eo, "notoday,%d", *pos);
280         *pos = -1;
281 }
282
283 static inline void      __cal_month_calendar_set_focuse_week(cal_view_main_month_cal_data *p,Evas_Object *eo, int pos)
284 {
285         if (pos == -1)
286                 return;
287
288         int i = 0;
289
290         cal_util_emit_signal(eo, "today,%d", p->pos_today);
291
292         if(pos >= 0 && pos <= 6)//line 1
293         {
294                 for(i=0; i<=41; i++)
295                 {
296                         if(i >= 0 && i <= 6)
297                         {
298                                 cal_util_emit_signal(eo, "week,%d", i);
299                                 cal_util_emit_signal(eo, "uncheck,%d", i);
300                                 cal_util_emit_signal(eo, "unselect,%d", i);
301                                 p->base_pos_for_weekly = 0;
302                         }
303                         else
304                         {
305                                 cal_util_emit_signal(eo, "noweek,%d", i);
306                                 if( -1 == p->day[i].checked)
307                                 {
308                                         cal_util_emit_signal(eo, "check,%d", i);
309                                 }
310                         }
311                 }
312         }
313         else if(pos >= 7 && pos <= 13)//line 2
314         {
315                 for(i=0; i<=41; i++)
316                 {
317                         if(i >= 7 && i <= 13)
318                         {
319                                 cal_util_emit_signal(eo, "week,%d", i);
320                                 cal_util_emit_signal(eo, "uncheck,%d", i);
321                                 cal_util_emit_signal(eo, "unselect,%d", i);
322                                 p->base_pos_for_weekly = 7;
323                         }
324                         else
325                         {
326                                 cal_util_emit_signal(eo, "noweek,%d", i);
327                                 if( -1 == p->day[i].checked)
328                                 {
329                                         cal_util_emit_signal(eo, "check,%d", i);
330                                 }
331                         }
332                 }
333         }
334         else if(pos >= 14 && pos <= 20)//line 3
335         {
336                 for(i=0; i<=41; i++)
337                 {
338                         if(i >= 14 && i <= 20)
339                         {
340                                 cal_util_emit_signal(eo, "week,%d", i);
341                                 cal_util_emit_signal(eo, "uncheck,%d", i);
342                                 cal_util_emit_signal(eo, "unselect,%d", i);
343                                 p->base_pos_for_weekly = 7*2;
344                         }
345                         else
346                         {
347                                 cal_util_emit_signal(eo, "noweek,%d", i);
348                                 if( -1 == p->day[i].checked)
349                                 {
350                                         cal_util_emit_signal(eo, "check,%d", i);
351                                 }
352                         }
353                 }
354         }
355         else if(pos >= 21 && pos <= 27)//line 4
356         {
357                 for(i=0; i<=41; i++)
358                 {
359                         if(i >= 21 && i <= 27)
360                         {
361                                 cal_util_emit_signal(eo, "week,%d", i);
362                                 cal_util_emit_signal(eo, "uncheck,%d", i);
363                                 cal_util_emit_signal(eo, "unselect,%d", i);
364                                 p->base_pos_for_weekly = 7*3;
365
366                         }
367                         else
368                         {
369                                 cal_util_emit_signal(eo, "noweek,%d", i);
370
371                                 if( -1 == p->day[i].checked)
372                                 {
373                                         cal_util_emit_signal(eo, "check,%d", i);
374                                 }
375                         }
376                 }
377         }
378         else if(pos >= 28 && pos <= 34)//line 5
379         {
380                 for(i=0; i<=41; i++)
381                 {
382                         if(i >= 28 && i <= 34)
383                         {
384                                 cal_util_emit_signal(eo, "week,%d", i);
385                                 cal_util_emit_signal(eo, "uncheck,%d", i);
386                                 cal_util_emit_signal(eo, "unselect,%d", i);
387                                 p->base_pos_for_weekly = 7*4;
388                         }
389                         else
390                         {
391                                 cal_util_emit_signal(eo, "noweek,%d", i);
392                                 if( -1 == p->day[i].checked)
393                                 {
394                                         cal_util_emit_signal(eo, "check,%d", i);
395                                 }
396                         }
397                 }
398         }
399         else if(pos >= 35 && pos <= 41)//line 6
400         {
401                 for(i=0; i<=41; i++)
402                 {
403                         if(i >= 35 && i <= 41)
404                         {
405                                 cal_util_emit_signal(eo, "week,%d", i);
406                                 cal_util_emit_signal(eo, "uncheck,%d", i);
407                                 cal_util_emit_signal(eo, "unselect,%d", i);
408                                 p->base_pos_for_weekly = 7*5;
409                         }
410                         else
411                         {
412                                 cal_util_emit_signal(eo, "noweek,%d", i);
413                                 if( -1 == p->day[i].checked)
414                                 {
415                                         cal_util_emit_signal(eo, "check,%d", i);
416                                 }
417                         }
418                 }
419         }
420 }
421
422
423 static inline void __cal_month_calendar_set_selected(Evas_Object *eo, int pos)
424 {
425         if (pos < 0 || pos >= 42)
426                 return;
427
428         cal_util_emit_signal(eo, "select,%d", pos);
429 }
430
431 static inline void __cal_month_calendar_set_unselected(Evas_Object *eo, int *pos)
432 {
433         if (*pos == -1)
434                 return;
435
436         cal_util_emit_signal(eo, "unselect,%d", *pos);
437         *pos = -1;
438 }
439
440 static inline int __cal_month_calendar_get_start_day(int *st, struct tm *t, int start, int max)
441 {
442         int day;
443         int prev_day;
444
445         prev_day = CAL_UTIL_GET_WDAY(__cal_month_calendar_get_first_wday(t->tm_mday, t->tm_wday) - start);
446         if (prev_day > 0) {
447                 day = max - prev_day + 1;
448                 *st = ST_PREV;
449         } else {
450                 day = 1;
451                 *st = ST_CURR;
452         }
453
454         return day;
455 }
456
457 static int __cal_month_calendar_update_today(Evas_Object *eo, cal_view_main_month_cal_data *p)
458 {
459
460         struct appdata *ad = p->ad;
461         struct tm *t = &p->cur_tm;
462
463         CAL_ASSERT(ad);
464
465         __cal_month_calendar_unset_today(eo, &p->pos_today);
466         ad->today_edj = NULL;
467
468         if (t->tm_year == ad->today_tm.tm_year && t->tm_mon == ad->today_tm.tm_mon)
469         {
470                 p->pos_today = p->pos_start + ad->today_tm.tm_mday - 1;
471                 __cal_month_calendar_set_today(eo, p->pos_today);
472                 ad->today_edj = eo;
473         }
474
475         return 0;
476 }
477
478 void cal_month_calendar_update_select(Evas_Object *ly)
479 {
480         CAL_ASSERT(ly);
481         Evas_Object* eo = CAL_UTIL_GET_EDJ_DATA(ly);
482
483         cal_view_main_month_cal_data* p = CAL_UTIL_GET_PRIV_DATA(ly);
484         CAL_ASSERT(p);
485         CAL_ASSERT(p->ad);
486
487         cal_main_update_title_text(p->ad->title, CV_MONTH, &p->ad->base_tm, p->ad->wday_start);
488
489         __cal_month_calendar_set_unselected(eo, &p->pos_selected);
490         p->pos_selected = p->pos_start + p->ad->base_tm.tm_mday - 1;
491         __cal_month_calendar_set_selected(eo, p->pos_selected);
492
493         if(p->ad->is_weekly)
494         {
495                 __cal_month_calendar_get_weekly_events(p);
496                 __cal_month_calendar_set_focuse_week(p, eo, p->pos_selected);
497                 __cal_month_calendar_signal_week(p,CAL_UTIL_GET_EDJ_DATA(p->ly));
498         }
499 }
500
501 static void __cal_month_calendar_get_simple_check(cal_view_main_month_cal_data *p, time_t st, time_t et, char *map, int map_sz)
502 {
503         int* month;
504         int i = 0;
505         month = CALENDAR_SVC_GET_SIMPLE_CS(p->ad->acct_id, st, et);
506         if (!month)
507                 return;
508
509         for (i = 0; i < cal_util_get_max_days(p->cur_tm.tm_year, p->cur_tm.tm_mon); i++)
510         {
511                 if(month[i])
512                         map[i] = month[i];
513         }
514
515         free(month);
516 }
517
518 static void __cal_month_calendar_update_check(Evas_Object *ly, cal_view_main_month_cal_data *p)
519 {
520         time_t st, et;
521         int r, i;
522         char m[42] = { 0 }; // change to bitmap??
523         int max;
524         char part[32] = {0};
525         char text[32] = {0};
526
527         r = cal_util_get_month_time_t(&p->cur_tm, &st, &et);
528         if (r == -1)
529         {
530                 ERR("Invalid time conversion");
531                 return;
532         }
533         if (st < 0)
534                 st = 0;
535
536         max = cal_util_get_max_days(p->cur_tm.tm_year, p->cur_tm.tm_mon);
537
538         __cal_month_calendar_get_simple_check(p, st, et, &m[p->pos_start], max);
539
540         for (i = 0; i < 42; i++)
541         {
542                 if (m[i])
543                 {
544                         cal_util_emit_signal(CAL_UTIL_GET_EDJ_DATA(ly), "check,%d", i);
545                         p->day[i].checked = 1;
546
547                         memset(part,0x00, sizeof part);
548                         memset(text,0x00, sizeof text);
549
550                         snprintf(part,sizeof part,"db/%d/text_on_check",i);
551                         snprintf(text,sizeof text,"%d",m[i]>99?99:m[i]);
552                         edje_object_part_text_set(CAL_UTIL_GET_EDJ_DATA(ly),part,text);
553                 }
554                 else
555                 {
556                         cal_util_emit_signal(CAL_UTIL_GET_EDJ_DATA(ly), "uncheck,%d", i);
557                         p->day[i].checked = 0;
558                 }
559         }
560 }
561
562 static int __cal_month_calendar_fill_calendar(Evas_Object *ly, struct tm *t, int start)
563 {
564         cal_view_main_month_cal_data *p;
565         int line = 6; // default
566         int prev_max, cur_max;
567         int day, i, st, wday;
568
569         p = CAL_UTIL_GET_PRIV_DATA(ly);
570         CAL_ASSERT(p);
571
572         p->cur_tm = *t;
573
574         prev_max = cal_util_get_max_days(t->tm_year, t->tm_mon - 1);
575         cur_max = cal_util_get_max_days(t->tm_year, t->tm_mon);
576
577         day = __cal_month_calendar_get_start_day(&st, t, start, prev_max);
578         wday = start;
579         p->pos_start = 0;
580         p->pos_end = 41;
581
582         for (i = 0; i < 42; i++)
583         {
584                 cal_util_set_text(CAL_UTIL_GET_EDJ_DATA(ly), cal_util_get_part_text("db/%d/text", i), "%d", day);
585
586                 switch (st)
587                 {
588                 case ST_PREV:
589                         __cal_month_calendar_set_disabled(ly, p, i);
590                         if (day == prev_max)
591                         {
592                                 st = ST_CURR;
593                                 day = 0;
594                                 p->pos_start = i + 1;
595                         }
596                         break;
597
598                 case ST_CURR:
599                         __cal_month_calendar_set_enabled(ly, p, i, wday);
600                         if (day == cur_max)
601                         {
602                                 st = ST_NEXT;
603                                 day = 0;
604                                 line = (int)(i / 7) + 1;
605                                 p->pos_end = i;
606                         }
607                         break;
608
609                 case ST_NEXT:
610                         __cal_month_calendar_set_disabled(ly, p, i);
611                         break;
612                 }
613
614                 day++;
615                 wday = CAL_UTIL_GET_WDAY(wday + 1);
616         }
617
618         __cal_month_calendar_update_today(CAL_UTIL_GET_EDJ_DATA(ly), p);
619
620         return line;
621 }
622
623 static void __cal_month_calendar_click_event_callback(cal_view_main_month_cal_data *p, Evas_Object *obj, int pos)
624 {
625         CAL_ASSERT(p);
626
627         struct appdata *ad = p->ad;
628         CAL_ASSERT(ad);
629
630         if (p->pos_selected == pos) {
631                 DBG("cal_cb: double clicked %d", pos);
632
633                 if (p->ad->is_weekly)
634                         return;
635
636                 if (p->day[pos].checked) {
637                         cal_main_change_view(p->parent, CV_DAY);
638                 }
639                 else    {
640                         p->ad->base_hour = -1;
641                         p->ad->cid = 0; // new
642
643                         cal_edit_create_view(p->ad, p->parent);
644                 }
645
646                 return;
647         }
648
649         p->cur_tm.tm_mday = pos - p->pos_start + 1;
650         ad->base_tm = p->cur_tm;
651
652         cal_month_calendar_update_select(p->month);
653         edje_object_signal_emit(obj, "changed", "prog");
654 }
655
656 static void __cal_month_calendar_cal_event_callback(void *data, Evas_Object *obj, const char *e, const char *src)
657 {
658         cal_view_main_month_cal_data *p = data;
659         c_retm_if(!p, "p is null");
660
661         struct appdata *ad = p->ad;
662         c_retm_if(!ad, "ad is null");
663
664         int pos = atoi(&e[3]);
665
666         if (!strncmp(e, "up,", 3))
667         {
668                 if (p->pos_down == pos)
669                         __cal_month_calendar_click_event_callback(p, obj, pos);
670
671                 edje_object_signal_emit(obj, e, "prog");
672
673                 return;
674         }
675
676         if (!strncmp(e, "dn,", 3))
677         {
678                 p->pos_down = pos;
679
680                 edje_object_signal_emit(obj, e, "prog");
681
682                 return;
683         }
684 }
685
686 static void __cal_month_calendar_prog_event_callback(void *data, Evas_Object *obj, const char *e, const char *src)
687 {
688         cal_view_main_month_cal_data *p = data;
689
690         if (!CAL_STRCMP(e, "update_today")) {
691                 __cal_month_calendar_update_today(obj, p);
692                 return;
693         }
694
695         if (CAL_STRCMP(e, "moved")) {
696                 return;
697         }
698
699         if (p->pos_down == -1) {
700                 return;
701         }
702
703         cal_util_emit_signal(obj, "up,%d", p->pos_down);
704         p->pos_down = -1;
705 }
706
707 static void __cal_month_calendar_set_wday_text(Evas_Object *ly, int start)
708 {
709         int i;
710         struct tm t = { 0, };
711         cal_view_main_month_cal_data* p = CAL_UTIL_GET_PRIV_DATA(ly);
712         if(!p)
713         {
714                 ERR("p is null");
715                 return;
716         }
717
718         t = p->ad->base_tm;
719
720         cal_util_update_tm_day(&t, t.tm_wday*(-1)+start);
721
722         if (!t.tm_wday)
723         {
724                 for (i = 0; i < 7; i++) {
725                         cal_util_set_time_text(CAL_UTIL_GET_EDJ_DATA(ly), cal_util_get_part_text("wday/%d/text", i), CAL_UTIL_DATE_FORMAT_2, NULL, &t);
726
727                         if (t.tm_wday == 6) {
728                                 cal_util_emit_signal(CAL_UTIL_GET_EDJ_DATA(ly), "satday,%d", i);
729                                 cal_util_update_tm_day(&t, t.tm_wday*(-1));
730                                 continue;
731                         }
732
733                         if (t.tm_wday == 0)
734                                 cal_util_emit_signal(CAL_UTIL_GET_EDJ_DATA(ly), "sunday,%d", i);
735
736                         cal_util_update_tm_day(&t, 1);
737                 }
738         }
739         else
740         {
741                 for (i = 0; i < 7; i++) {
742                         cal_util_set_time_text(CAL_UTIL_GET_EDJ_DATA(ly), cal_util_get_part_text("wday/%d/text", i), CAL_UTIL_DATE_FORMAT_2, NULL, &t);
743
744                         if (t.tm_wday == 6)
745                         {
746                                 cal_util_emit_signal(CAL_UTIL_GET_EDJ_DATA(ly), "satday,%d", i);
747                                 cal_util_update_tm_day(&t, t.tm_wday*(-1));
748                                 cal_util_set_time_text(CAL_UTIL_GET_EDJ_DATA(ly), cal_util_get_part_text("wday/%d/text", ++i), CAL_UTIL_DATE_FORMAT_2, NULL, &t);
749                                 cal_util_emit_signal(CAL_UTIL_GET_EDJ_DATA(ly), "sunday,%d", i);
750                         }
751
752                         cal_util_update_tm_day(&t, 1);
753                 }
754         }
755 }
756
757 static gint __cal_month_calendar_events_compare(gconstpointer a,gconstpointer b)
758 {
759         weekly_event *p1 = (weekly_event *)a;
760         weekly_event *p2 = (weekly_event *)b;
761
762         if (p2->start_time < p1->start_time)
763                 return 1;
764         else if(p1->start_time < p2->start_time)
765                 return -1;
766         else
767                 return 0;
768 }
769
770 static void __cal_month_calendar_signal_clean(cal_view_main_month_cal_data *p,Evas_Object *eo)
771 {
772         int i = 0;
773         int line = 0;
774
775         for(i=0; i<7; i++)
776         {
777                 for(line=1; line<7; line++)
778                 {
779                         cal_util_emit_signal(eo,"%d,%d,%d",line,0,i);
780                         cal_util_emit_signal(eo,"%d,%s,%d",line,"nocolor",i);
781                 }
782         }
783
784 }
785
786 static int __cal_month_calendar_clean_list(cal_view_main_month_cal_data *p)
787 {
788         int i = 0;
789
790         if(NULL == p)
791         {
792                 return -1;
793         }
794
795         for(i=0; i<7; i++)
796         {
797                 g_list_free(p->list[i]);
798                 p->list[i] = NULL;
799         }
800         return 0;
801 }
802
803 static int __cal_month_calendar_get_week_time(cal_view_main_month_cal_data *p, int *first_day, int *last_day)
804 {
805         int day = 0;
806         int today = 0;
807         int start_day = 0;
808
809         if(-1 == p->pos_selected)
810         {
811                 day = p->pos_today;
812         }
813         else
814         {
815                 day = p->pos_selected;
816         }
817
818         today = day - p->pos_start + 1;
819         start_day = day - (day+1)%7 +1;
820
821         if(day <= 6)
822         {
823                 *first_day = 1;
824                 *last_day = 6 - p->pos_start + 1;
825         }
826         else if(0 == (day+1)%7)
827         {
828                 *first_day = today - 6;
829                 *last_day = today;
830         }
831         else
832         {
833                 *first_day = today - day%7;
834                 if(start_day+6 > p->pos_end)
835                 {
836                         *last_day = p->pos_end - p->pos_start + 1;
837                 }
838                 else
839                 {
840                         *last_day = *first_day + 6;
841                 }
842         }
843
844         return 0;
845 }
846
847 static bool __cal_month_calendar_is_last(GList** list)
848 {
849         int i = 0;
850
851         if (!list)
852                 return FALSE;
853
854         for (i=0; i<7; i++) {
855                 if (list[i])
856                         return TRUE;
857         }
858
859         return FALSE;
860 }
861
862 static void __cal_month_calendar_line_arrange(GList** list,int index,const int line)
863 {
864         weekly_event *temp = NULL;
865         GList *list_temp = list[index];
866
867         if (list_temp && list_temp->data) {
868                 temp = list_temp->data;
869                 temp->line = line;
870                 temp->showed = 1;
871
872                 if (0 == temp->width)
873                         temp->width = 1;
874
875                 if (7 <= index + temp->width)
876                         return;
877
878                 __cal_month_calendar_line_arrange(list, index + temp->width, line);
879         }
880         else {
881                 if (7 <= index + 1)
882                         return;
883
884                 __cal_month_calendar_line_arrange(list, index + 1, line);
885         }
886 }
887
888 static void __cal_month_calendar_get_next(GList** list)
889 {
890         c_retm_if(!list, "list is null");
891
892         int i = 0;
893         weekly_event *temp = NULL;
894
895         for (i=0;i<7;i++) {
896
897                 if (list[i] && list[i]->data) {
898
899                         temp = (weekly_event*)list[i]->data;
900
901                         if (temp->showed)
902                                 list[i] = list[i]->next;
903                 }
904         }
905 }
906
907 static int __cal_month_calendar_get_weekly_events(cal_view_main_month_cal_data *p)
908 {
909         c_retvm_if(!p, -1, "p is null");
910
911         int i = 0;
912         int start_i = 0;
913         int line = 1;
914         int start_day = 0;
915         int end_day = 0;
916         int day_num = 0;
917         cal_iter *iter = NULL;
918         cal_struct *record = NULL;
919         weekly_event *event = NULL;
920         struct tm end_time = {0};
921         struct tm start_time = {0};
922         struct tm st = {0};
923         struct tm et = {0};
924         time_t ls, le;
925         int index = 0;
926
927         //clean
928         __cal_month_calendar_signal_clean(p,CAL_UTIL_GET_EDJ_DATA(p->ly));
929
930         //free the list if not NULL
931         __cal_month_calendar_clean_list(p);
932
933         //calculate the time of this week
934         __cal_month_calendar_get_week_time(p,&start_day,&end_day);
935
936         day_num = end_day - start_day + 1;
937         if (day_num < 7) {
938                 if (end_day < 7)
939                         start_i = 7 - day_num;
940         }
941         else {
942                 start_i = 0;
943         }
944
945         memcpy(&start_time, &p->cur_tm, sizeof(struct tm));
946         memcpy(&end_time, &p->cur_tm, sizeof(struct tm));
947
948         start_time.tm_mday = start_day;
949         start_time.tm_hour = 0;
950         start_time.tm_min = 0;
951         start_time.tm_sec = 0;
952
953         end_time.tm_mday = end_day;
954         end_time.tm_hour = 23;
955         end_time.tm_min = 59;
956         end_time.tm_sec = 59;
957
958         ls = mktime(&start_time);
959         if (ls < 0)
960                 ls = 0;
961
962         le = mktime(&end_time);
963
964         if (CAL_SUCCESS != CALENDAR_SVC_GET_EVENT_LIST_BY_PERIOD(0,ls,le,&iter))
965                 return -1;
966
967         record = CALENDAR_SVC_STRUCT_NEW("schedule");
968
969         while (CAL_SUCCESS == CALENDAR_SVC_ITER_NEXT(iter)) {
970
971                 CALENDAR_SVC_ITER_GET_INFO(iter,&record);
972
973                 memset(&st,0x00,sizeof(struct tm));
974                 memset(&et,0x00,sizeof(struct tm));
975
976                 while (CAL_SUCCESS == CALENDAR_SVC_UTIL_NEXT_VAILD_EVENT_TM(record,&start_time,&end_time,&st,&et)) {
977
978                         if (end_day < st.tm_mday)
979                                 break;
980
981                         if (et.tm_mday < start_day)
982                                 continue;
983
984                         if (st.tm_mday < start_day)
985                                 st.tm_mday = start_day;
986
987                         if (et.tm_mday > end_day)
988                                 et.tm_mday = end_day;
989
990                         //get the info
991                         event = (weekly_event*)calloc(1, sizeof(weekly_event));
992                         c_retvm_if(!event, -1, "event is null");
993
994                         event->cid = CALENDAR_SVC_STRUCT_GET_INT(record,"index");
995
996                         g_snprintf(event->title, sizeof(event->title), CALENDAR_SVC_STRUCT_GET_STR(record,"summary"));
997
998                         event->is_repeat = 1;
999                         event->start_time = mktime(&st);
1000                         event->end_time = mktime(&et);
1001                         event->width = et.tm_mday - st.tm_mday + 1;
1002                         if (event->width < 1)
1003                                 event->width = 1;
1004
1005                         index = st.tm_mday - start_day + start_i;
1006
1007                         if (6 < index)
1008                                 index = 6;
1009                         else if (index < 0)
1010                                 index = 0;
1011
1012                         p->list[index] = g_list_append(p->list[index],event);
1013                 }
1014         }
1015
1016         if (record)
1017                 CALENDAR_SVC_STRUCT_FREE(&record);
1018
1019         if (iter)
1020                 CALENDAR_SVC_ITER_REMOVE(&iter);
1021
1022         for (i=start_i; i < day_num; i++) {
1023                 if (p->list[i])
1024                         p->list[i] = g_list_sort(p->list[i], __cal_month_calendar_events_compare);
1025         }
1026
1027         GList* list_t[7];
1028
1029         for (i=0; i < 7; i++)
1030                 list_t[i] = p->list[i];
1031
1032         //calculate the line of  each event
1033         while (__cal_month_calendar_is_last(p->list)) {
1034
1035                 __cal_month_calendar_line_arrange(p->list, 0, line);
1036
1037                 line++;
1038
1039                 if (5 < line)
1040                         break;
1041
1042                 __cal_month_calendar_get_next(p->list);
1043         }
1044
1045         for (i=0; i < 7; i++)
1046                 p->list[i] = list_t[i];
1047
1048         //free the memory
1049         if (record)
1050                 CALENDAR_SVC_STRUCT_FREE(&record);
1051
1052         return 0;
1053 }
1054
1055 static void __cal_month_calendar_signal_week(cal_view_main_month_cal_data *p,Evas_Object *eo)
1056 {
1057         c_retm_if(!p, "p is null");
1058         c_retm_if(!eo, "eo is null");
1059
1060         int i = 0;
1061         weekly_event *temp = NULL;
1062         char text[20] = {0};
1063         char *color[4] = {"green","blue","red","purple"};
1064         int color_index = -1;
1065         GList* list = NULL;
1066
1067         for (i=0; i<7; i++) {
1068
1069                 if (!p->list[i])
1070                         continue;
1071
1072                 list = g_list_first( p->list[i]);
1073                 while (list && list->data) {
1074                         color_index++;
1075                         temp = list->data;
1076
1077                         cal_util_emit_signal(eo,"%d,%d,%d",temp->line,temp->width,i);
1078                         if (0 == temp->is_repeat)
1079                                 cal_util_emit_signal(eo,"%d,%s,%d",temp->line,color[color_index%4],i);
1080                         else {
1081                                 cal_util_emit_signal(eo,"%d,%s,%d",temp->line,color[(temp->cid)%4],i);
1082                                 color_index = (temp->cid)%4;
1083                         }
1084
1085                         memset(text,0x00,sizeof(text));
1086                         g_snprintf(text,sizeof(text),"%d/text/%d",temp->line,i);
1087                         edje_object_part_text_set(CAL_UTIL_GET_EDJ_DATA(p->ly), text, temp->title);
1088
1089                         list = list->next;
1090                 }
1091         }
1092 }
1093
1094 static void __cal_month_calendar_mouse_down(void *data, Evas *e, Evas_Object *ly, void *ei)
1095 {
1096         cal_view_main_month_cal_data *p = data;
1097         Evas_Event_Mouse_Down *ev = ei;
1098         char eve[8] = {0};
1099         int offset = 0;
1100
1101         Evas_Coord_Rectangle r;
1102         cal_util_get_geometry (&r, ly);
1103         offset = ev->canvas.x/(r.w/7);
1104         if (6 < offset)
1105                 offset = 6;
1106
1107         p->down_x = ev->canvas.x;
1108         p->down_y = ev->canvas.y;
1109
1110         if ( -1 == p->day[p->base_pos_for_weekly + offset].enabled) {
1111                 p->index_for_press = p->base_pos_for_weekly + offset;
1112                 snprintf(eve, sizeof(eve) , "dn,%d", p->index_for_press);
1113                 edje_object_signal_emit(CAL_UTIL_GET_EDJ_DATA(p->month), eve, "prog");
1114         }
1115 }
1116
1117 static void __cal_month_calendar_mouse_move(void *data, Evas *e, Evas_Object *ly, void *ei)
1118 {
1119         cal_view_main_month_cal_data *p = data;
1120         char eve[8] = {0};
1121
1122         if( -1 != p->index_for_press)
1123         {
1124                 snprintf(eve, sizeof(eve) , "up,%d", p->index_for_press);
1125                 edje_object_signal_emit(CAL_UTIL_GET_EDJ_DATA(p->month), eve, "prog");
1126                 p->index_for_press = -1;
1127         }
1128 }
1129
1130 static bool __cal_month_calendar_event_exists(GList** list,int index)
1131 {
1132         int i = 0;
1133         GList *list_temp = NULL;
1134         weekly_event *event = NULL;
1135
1136         for(i=0; i<=index; i++)
1137         {
1138                 if(NULL == list[i])
1139                 {
1140                         continue;
1141                 }
1142
1143                 list_temp = g_list_first(list[i]);
1144                 while(NULL != list_temp)
1145                 {
1146                         event = list_temp->data;
1147                         if(NULL == event)
1148                         {
1149                                 list_temp = g_list_next(list_temp);
1150                                 continue;
1151                         }
1152                         if(event->width >= index - i + 1)
1153                         {
1154                                 return TRUE;
1155                         }
1156                         list_temp = g_list_next(list_temp);
1157                 }
1158         }
1159
1160         return FALSE;
1161 }
1162
1163 static void __cal_month_calendar_genlist_item_select_callback(void *data, Evas_Object *obj, void *ei)
1164 {
1165         struct tmnode* tm = data;
1166         cal_struct *cs = tm->cs;
1167         int idx;
1168         Elm_Object_Item *it;
1169         cal_view_main_month_cal_data *p;
1170         int repeat;
1171
1172         it = elm_genlist_selected_item_get(obj);
1173         if (it)
1174                 elm_genlist_item_selected_set(it, EINA_FALSE);
1175
1176         idx = CALENDAR_SVC_STRUCT_GET_INT(cs, CAL_VALUE_INT_INDEX);
1177
1178         p = CAL_UTIL_GET_PRIV_DATA(obj);
1179         CAL_ASSERT(p);
1180
1181         repeat = CALENDAR_SVC_STRUCT_GET_INT(cs, CAL_VALUE_INT_REPEAT_TERM);
1182         if (repeat) {
1183                 memcpy(p->ad->tm, tm, sizeof(struct tmnode));
1184         }
1185
1186         p->ad->cid = idx;
1187
1188         cal_detail_create_view(p->ad, p->parent);
1189
1190         if (p->popup)   {
1191                 evas_object_hide(p->popup);
1192         }
1193 }
1194
1195 static void __cal_month_calendar_get_events(cal_view_main_month_cal_data *p)
1196 {
1197         time_t st, et;
1198
1199         cal_util_get_day_time_t(&p->ad->base_tm, &st, &et);
1200
1201         p->csv = CALENDAR_SVC_GET_EVENT(p->ad->acct_id, st, et);
1202 }
1203
1204 static void __cal_month_calendar_update_genlist(cal_view_main_month_cal_data *p)
1205 {
1206         CAL_FN_START;
1207
1208         c_retm_if(!p, "p is null");
1209         c_retm_if(!p->gl, "p->gl is null");
1210
1211         Eina_List *l;
1212         struct tmnode *tm;
1213         Elm_Object_Item *it = NULL;
1214         elm_genlist_clear(p->gl);
1215
1216         CALENDAR_SVC_FREE_CS(&p->csv);
1217
1218         __cal_month_calendar_get_events(p);
1219         c_retm_if(!p->csv, "p->csv is null");
1220
1221         EINA_LIST_FOREACH(p->csv->tmlist, l, tm) {
1222                 if (tm) {
1223                         it = elm_genlist_item_append(p->gl, &itc, tm, NULL, ELM_GENLIST_ITEM_NONE, __cal_month_calendar_genlist_item_select_callback, tm);
1224                         c_retm_if(!it, "elm_genlist_item_append() returned null");
1225                 }
1226         }
1227
1228         CAL_FN_END;
1229 }
1230
1231 static void __cal_month_calendar_popup_response_callback(void *data, Evas_Object *obj, void *ei)
1232 {
1233         c_retm_if(!data, "data is null");
1234
1235         Evas_Object *popup = data;
1236
1237         evas_object_hide(popup);
1238 }
1239
1240 void __cal_month_calendar_events_of_one_weekday(void *data)
1241 {
1242         c_retm_if(!data, "data is null");
1243
1244         cal_view_main_month_cal_data *p = data;
1245         c_retm_if(!p->parent, "p->parent is null");
1246
1247         struct appdata *ad = p->ad;
1248         c_ret_if(!ad);
1249
1250         char title[128] = {0};
1251
1252         cal_util_get_time_text(title, sizeof(title), CAL_UTIL_DATE_FORMAT_13, NULL, &p->ad->base_tm);
1253
1254         if (!p->popup) {
1255                 p->popup = cal_util_add_popup(ad->win, "menustyle", title, NULL,
1256                         __cal_month_calendar_popup_response_callback, p,
1257                         S_("IDS_COM_BODY_CLOSE"), NULL);
1258
1259                 c_retm_if(!p->popup, "cal_util_add_popup returned null");
1260         }
1261         else {
1262                 elm_object_part_text_set(p->popup, "title,text", title);
1263                 evas_object_show(p->popup);
1264         }
1265
1266         if (!p->gl) {
1267                 p->gl = elm_genlist_add(p->popup);
1268                 evas_object_data_set(p->gl, "priv", p);
1269                 evas_object_show(p->gl);
1270                 elm_object_content_set(p->popup, p->gl);
1271         }
1272
1273         __cal_month_calendar_update_genlist(p);
1274 }
1275
1276 static void __cal_month_calendar_mouse_up(void *data, Evas *e, Evas_Object *ly, void *ei)
1277 {
1278         cal_view_main_month_cal_data *p = data;
1279         Evas_Event_Mouse_Down *ev = ei;
1280         int start_day = 0;
1281         int end_day = 0;
1282         int num_of_enable = 0;
1283         char eve[8] = {0};
1284
1285         if (ev->canvas.x != p->down_x && ev->canvas.y != p->down_y)
1286                 return;
1287
1288         Evas_Coord_Rectangle r;
1289         cal_util_get_geometry (&r, ly);
1290         int day_width = r.w/7;
1291
1292         __cal_month_calendar_get_week_time(p,&start_day,&end_day);
1293
1294         if (7 != (num_of_enable = end_day - start_day + 1)) {
1295
1296                 if (end_day < 7) {
1297
1298                         if (ev->canvas.x < (7 - num_of_enable) * day_width)
1299                                 return;
1300
1301                         start_day -= (7 - num_of_enable);
1302                 }
1303                 else if (start_day > 7) {
1304
1305                         if (ev->canvas.x > num_of_enable * day_width)
1306                                 return;
1307                 }
1308         }
1309
1310         if (ev->canvas.x > 0 && ev->canvas.x < 1*day_width) {
1311
1312                 p->ad->base_tm.tm_mday = start_day;
1313
1314                 if (!__cal_month_calendar_event_exists(p->list,0)) {
1315                         p->ad->base_hour = -1;
1316                         p->ad->cid = 0; // new
1317                         cal_edit_create_view(p->ad, p->parent);
1318                 }
1319                 else
1320                         __cal_month_calendar_events_of_one_weekday(p);
1321         }
1322         else if (ev->canvas.x > 1*day_width && ev->canvas.x < 2*day_width) {
1323
1324                 p->ad->base_tm.tm_mday = start_day+1;
1325
1326                 if (!__cal_month_calendar_event_exists(p->list,1)) {
1327
1328                         p->ad->base_hour = -1;
1329                         p->ad->cid = 0; // new
1330                         cal_edit_create_view(p->ad, p->parent);
1331                 }
1332                 else
1333                         __cal_month_calendar_events_of_one_weekday(p);
1334         }
1335         else if (ev->canvas.x > 2*day_width && ev->canvas.x < 3*day_width) {
1336
1337                 p->ad->base_tm.tm_mday = start_day+2;
1338                 if (!__cal_month_calendar_event_exists(p->list,2)) {
1339
1340                         p->ad->base_hour = -1;
1341                         p->ad->cid = 0; // new
1342                         cal_edit_create_view(p->ad, p->parent);
1343                 }
1344                 else
1345                         __cal_month_calendar_events_of_one_weekday(p);
1346         }
1347         else if (ev->canvas.x > 3*day_width && ev->canvas.x < 4*day_width) {
1348
1349                 p->ad->base_tm.tm_mday = start_day+3;
1350                 if (!__cal_month_calendar_event_exists(p->list,3)) {
1351
1352                         p->ad->base_hour = -1;
1353                         p->ad->cid = 0; // new
1354                         cal_edit_create_view(p->ad, p->parent);
1355                 }
1356                 else
1357                         __cal_month_calendar_events_of_one_weekday(p);
1358         }
1359         else if (ev->canvas.x > 4*day_width && ev->canvas.x < 5*day_width) {
1360
1361                 p->ad->base_tm.tm_mday = start_day+4;
1362
1363                 if (!__cal_month_calendar_event_exists(p->list,4)) {
1364
1365                         p->ad->base_hour = -1;
1366                         p->ad->cid = 0; // new
1367                         cal_edit_create_view(p->ad, p->parent);
1368                 }
1369                 else
1370                         __cal_month_calendar_events_of_one_weekday(p);
1371         }
1372         else if (ev->canvas.x > 5*day_width && ev->canvas.x < 6*day_width) {
1373
1374                 p->ad->base_tm.tm_mday = start_day+5;
1375
1376                 if (!__cal_month_calendar_event_exists(p->list,5)) {
1377                         p->ad->base_hour = -1;
1378                         p->ad->cid = 0; // new
1379                         cal_edit_create_view(p->ad, p->parent);
1380                 }
1381                 else
1382                         __cal_month_calendar_events_of_one_weekday(p);
1383         }
1384         else {
1385
1386                 p->ad->base_tm.tm_mday = start_day+6;
1387
1388                 if (!__cal_month_calendar_event_exists(p->list,6)) {
1389                         p->ad->base_hour = -1;
1390                         p->ad->cid = 0; // new
1391                         cal_edit_create_view(p->ad, p->parent);
1392                 }
1393                 else
1394                         __cal_month_calendar_events_of_one_weekday(p);
1395         }
1396
1397         if (-1 != p->index_for_press) {
1398                 snprintf(eve, sizeof(eve) , "up,%d", p->index_for_press);
1399                 edje_object_signal_emit(CAL_UTIL_GET_EDJ_DATA(p->month), eve, "prog");
1400                 p->index_for_press = -1;
1401         }
1402
1403 }
1404
1405 Evas_Object* cal_month_calendar_create_content(struct tm *t, struct appdata *ad,
1406                 Evas_Object *main, Eina_Bool tmp)
1407 {
1408         CAL_FN_START;
1409
1410         cal_view_main_month_cal_data *p;
1411         Evas_Object *ly;
1412         time_t r;
1413
1414         CAL_ASSERT(ad);
1415
1416         r = mktime(t);
1417         if (r == -1)
1418                 return NULL;
1419
1420         CAL_CALLOC(p, 1, cal_view_main_month_cal_data);
1421
1422         p->ad = ad;
1423         p->parent = main;
1424         p->pos_down = p->pos_today = p->pos_selected = -1;
1425         p->pos_start = p->pos_end = -1;
1426
1427         if(!ad->is_weekly)
1428                 p->name = _name = "main/month/cal";
1429         else if(ad->is_weekly)
1430                 p->name = _name = "main/month/cal/weekly";
1431         else
1432                 return NULL;
1433
1434         ly = cal_util_add_layout(ad->nv, p->name);
1435         if (!ly)
1436         {
1437                 free(p);
1438                 return NULL;
1439         }
1440
1441         p->month = ly;
1442         evas_object_data_set(ly, "priv", p);
1443         edje_object_signal_callback_add(CAL_UTIL_GET_EDJ_DATA(ly), "*", "cal", __cal_month_calendar_cal_event_callback, p);
1444         edje_object_signal_callback_add(CAL_UTIL_GET_EDJ_DATA(ly), "*", "prog", __cal_month_calendar_prog_event_callback, p);
1445
1446         __cal_month_calendar_set_wday_text(ly, ad->wday_start);
1447
1448         if (tmp == EINA_TRUE)
1449         {
1450                 p->weekline = __cal_month_calendar_fill_calendar(ly, t, ad->wday_start);
1451                 __cal_month_calendar_update_check(ly, p);
1452         }
1453
1454         if (p->ad->is_weekly)
1455         {
1456                 Evas_Object *events = cal_util_add_layout(ad->nv, "main/month/week/events");
1457
1458                 elm_object_part_content_set(ly, "list/sw", events);
1459                 evas_object_event_callback_add(events, EVAS_CALLBACK_MOUSE_DOWN, __cal_month_calendar_mouse_down, p);
1460                 evas_object_event_callback_add(events, EVAS_CALLBACK_MOUSE_UP, __cal_month_calendar_mouse_up, p);
1461                 evas_object_event_callback_add(events, EVAS_CALLBACK_MOUSE_MOVE, __cal_month_calendar_mouse_move, p);
1462
1463                 p->ly = events;
1464
1465                 __cal_month_calendar_get_weekly_events(p);
1466
1467                 if(-1 == p->pos_selected)
1468                 {
1469                         __cal_month_calendar_set_focuse_week(p, CAL_UTIL_GET_EDJ_DATA(ly), p->pos_today);
1470                 }
1471                 else
1472                 {
1473                         __cal_month_calendar_set_focuse_week(p, CAL_UTIL_GET_EDJ_DATA(ly), p->pos_selected);
1474                 }
1475
1476                 __cal_month_calendar_signal_week(p,CAL_UTIL_GET_EDJ_DATA(p->ly));
1477         }
1478
1479         evas_object_event_callback_add(ly, EVAS_CALLBACK_DEL, __cal_month_calendar_delete_layout, p);
1480
1481         CAL_FN_END;
1482
1483         return ly;
1484 }
1485
1486 void cal_month_update_month_calendar(Evas_Object *ly, struct tm *t)
1487 {
1488         CAL_FN_START;
1489
1490         c_ret_if(!ly);
1491         c_ret_if(!t);
1492
1493         cal_view_main_month_cal_data *p = CAL_UTIL_GET_PRIV_DATA(ly);
1494         if (!p || CAL_STRCMP(p->name, _name)) {
1495                 ERR("update month calendar: Invalid object");
1496                 return;
1497         }
1498
1499         struct appdata *ad = p->ad;
1500         c_ret_if(!ad);
1501
1502         p->weekline = __cal_month_calendar_fill_calendar(ly, t, ad->wday_start);
1503         cal_month_calendar_update_select(ly);
1504         __cal_month_calendar_update_check(ly, p);
1505
1506         if (p->ad->is_weekly) {
1507
1508                 __cal_month_calendar_get_weekly_events(p);
1509
1510                 if (-1 == p->pos_selected)
1511                         __cal_month_calendar_set_focuse_week(p, CAL_UTIL_GET_EDJ_DATA(ly), p->pos_today);
1512                 else
1513                         __cal_month_calendar_set_focuse_week(p, CAL_UTIL_GET_EDJ_DATA(ly), p->pos_selected);
1514
1515                 __cal_month_calendar_signal_week(p,CAL_UTIL_GET_EDJ_DATA(p->ly));
1516         }
1517
1518         CAL_FN_END;
1519 }
1520
1521 int cal_month_get_weekline(Evas_Object *ly)
1522 {
1523         cal_view_main_month_cal_data *p;
1524
1525         CAL_ASSERT(ly);
1526
1527         p = CAL_UTIL_GET_PRIV_DATA(ly);
1528         if (!p || CAL_STRCMP(p->name, _name)) {
1529                 ERR("get weekline: Invalid object");
1530                 return 6;
1531         }
1532
1533         return p->weekline;
1534 }
1535