apply FSL license
[apps/core/preloaded/calendar.git] / src / view-main-day.c
1 /*
2   *
3   *  Copyright 2012  Samsung Electronics Co., Ltd
4   *
5   *  Licensed under the Flora License, Version 1.0 (the "License");
6   *  you may not use this file except in compliance with the License.
7   *  You may obtain a copy of the License at
8   *
9   *       http://www.tizenopensource.org/license
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17
18
19
20
21
22
23 #include "view.h"
24 #include "detail.h"
25
26 static const char *_name = "main/day";
27
28 struct _priv {
29         const char *name;
30         struct appdata *ad;
31         Evas_Object *parent;
32         Evas_Object *ly; // self
33
34         Evas_Object *time_block; // layout
35
36         Evas_Object *genlist;
37         struct calsvc *csv;
38 };
39
40 static char* __cal_day_get_genlist_item_label(void *data, Evas_Object *obj, const char *part);
41 static Elm_Genlist_Item_Class itc = {
42         .item_style = "dialogue/1text",
43         .func.text_get = __cal_day_get_genlist_item_label,
44 };
45
46 static void __cal_day_delete_layout(void *data, Evas *e, Evas_Object *obj, void *ei)
47 {
48         CAL_FN_START;
49
50         CAL_ASSERT(data);
51
52         struct _priv *p = data;
53
54         CALENDAR_SVC_FREE_CS(&p->csv);
55
56         free(p);
57 }
58
59 static char* __cal_day_get_genlist_item_label(void *data, Evas_Object *obj, const char *part)
60 {
61         struct tmnode *tm = (struct tmnode *)data;
62         const char *str;
63
64         if (!tm || !tm->cs)
65                 return NULL;
66
67         if (!CAL_STRCMP(part, "elm.text")) {
68                 str = CALENDAR_SVC_STRUCT_GET_STR(tm->cs, CAL_VALUE_TXT_SUMMARY);
69                 if (str)
70                         return strdup(str);
71
72                 return strdup(C_("IDS_CLD_BODY_NO_TITLE"));
73         }
74
75         return NULL;
76 }
77
78 static void __cal_day_genlist_item_select_callback(void *data, Evas_Object *obj, void *ei)
79 {
80         struct _priv *p;
81         struct tmnode* tm = (struct tmnode*)data;
82         cal_struct *cs = tm->cs;
83         Elm_Object_Item *it;
84         int idx;
85
86         it = elm_genlist_selected_item_get(obj);
87         if (it)
88                 elm_genlist_item_selected_set(it, EINA_FALSE);
89
90         idx = CALENDAR_SVC_STRUCT_GET_INT(cs, CAL_VALUE_INT_INDEX);
91
92         p = CAL_UTIL_GET_PRIV_DATA(obj);
93         CAL_ASSERT(p);
94
95         p->ad->cid = idx;
96         memcpy(p->ad->tm, tm, sizeof(struct tmnode));
97
98         cal_detail_create_view(p->ad, p->parent);
99 }
100
101 static void __cal_day_get_events(struct _priv *p)
102 {
103         time_t st, et;
104
105         cal_util_get_day_time_t(&p->ad->base_tm, &st, &et);
106
107         p->csv = CALENDAR_SVC_GET_EVENT(p->ad->acct_id, st, et);
108 }
109
110 void cal_day_update_genlist(Evas_Object *ly)
111 {
112         CAL_FN_START;
113
114         c_ret_if(!ly);
115
116         struct _priv *p = CAL_UTIL_GET_PRIV_DATA (ly);
117         c_ret_if(!p);
118         c_ret_if(!p->genlist);
119
120         Eina_List *l;
121         struct tmnode *tm;
122         int line;
123
124         elm_genlist_clear(p->genlist);
125         CALENDAR_SVC_FREE_CS(&p->csv);
126
127         __cal_day_get_events(p);
128
129         line = 0;
130         if (p->csv) {
131                 EINA_LIST_FOREACH(p->csv->tmlist, l, tm) {
132                         if(NULL != tm)
133                         {
134                                 int allday = CALENDAR_SVC_STRUCT_GET_INT(tm->cs, CAL_VALUE_INT_ALL_DAY_EVENT);
135                                 if (allday) {
136                                         elm_genlist_item_append(p->genlist, &itc, tm, NULL,
137                                                         ELM_GENLIST_ITEM_NONE, __cal_day_genlist_item_select_callback, tm);
138                                         line++;
139                                 }
140                         }
141                 }
142         }
143         if (line > 2)
144                 line = 2;
145
146         cal_util_emit_signal(CAL_UTIL_GET_EDJ_DATA(p->ly), "go,line%d", line);
147 }
148
149 Evas_Object* cal_day_create_view(struct appdata *ad, Evas_Object *main)
150 {
151         CAL_FN_START;
152
153         struct _priv *p;
154         Evas_Object *ly;
155         Evas_Object *genlist;
156         Evas_Object *time_block;
157
158         CAL_ASSERT(ad);
159
160         CAL_CALLOC(p, 1, struct _priv);
161
162         p->name = _name;
163         p->ad = ad;
164         p->parent = main;
165
166         ly = cal_util_add_layout(ad->nv, p->name);
167         CAL_ASSERT(ly);
168
169         p->ly = ly;
170         evas_object_data_set(ly, "priv", p);
171
172         genlist = elm_genlist_add(ly);
173         CAL_ASSERT(genlist);
174
175         p->genlist = genlist;
176         elm_object_part_content_set(ly, "list/sw", genlist);
177         evas_object_data_set(genlist, "priv", p);
178
179         time_block = cal_day_create_time_block(&ad->base_tm, ad, ly);
180         CAL_ASSERT (time_block);
181
182         p->time_block = time_block;
183         elm_object_part_content_set(ly, "cont/sw", time_block);
184
185         evas_object_event_callback_add(ly, EVAS_CALLBACK_DEL, __cal_day_delete_layout, p);
186
187         return ly;
188 }
189
190 void cal_day_update_view(Evas_Object *ly)
191 {
192         CAL_FN_START;
193
194         struct _priv *p;
195
196         CAL_ASSERT(ly);
197
198         p = CAL_UTIL_GET_PRIV_DATA(ly);
199         if (!p || CAL_STRCMP(p->name, _name)) {
200                 ERR("Invalid object");
201                 return;
202         }
203
204         cal_day_update_genlist(ly);
205
206         cal_day_time_update_time_block(p->time_block, &p->ad->base_tm);
207 }