cf9e81c7ef31a6160b6977ac5ef1d9bf875e98fa
[apps/core/preloaded/calendar.git] / src / view-day-list.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 #include <time.h>
19 #include "view-day-list.h"
20 #include "detail-view.h"
21 #include "noti-handler.h"
22 #include "view.h"
23 #include "list-base.h"
24 #include "query.h"
25
26 typedef struct {
27         cal_list_base_h list;
28
29         Evas_Object* layout;
30
31         struct appdata *ad;
32
33         struct tm tm_start;
34         struct tm tm_end;
35
36 } cal_day_list_data;
37
38 static void __cal_day_list_delete_layout(void *data, Evas *e, Evas_Object *obj, void *ei)
39 {
40         CAL_FN_START;
41
42         cal_day_list_data *p = data;
43         c_retm_if(!p, "priv is null.");
44         cal_list_base_delete(p->list);
45         free(p);
46
47         CAL_FN_END;
48 }
49
50 Evas_Object* cal_day_list_create(struct appdata *ad, Evas_Object *parent)
51 {
52         CAL_FN_START;
53
54         c_retv_if(!ad, NULL);
55         c_retv_if(!parent, NULL);
56
57         cal_day_list_data *p = calloc(1, sizeof(cal_day_list_data));
58         c_retv_if(!p, NULL);
59
60         p->ad = ad;
61
62         Evas_Object *layout = cal_util_add_layout(parent, "list/base/without_searchbar");
63         ad->tapbar_focus_view = CV_LIST;
64
65         if (!layout) {
66                 free(p);
67
68                 ERR("layout is null.");
69
70                 return NULL;
71         }
72
73         evas_object_data_set(layout, "priv", p);
74
75         p->layout = layout;
76
77         elm_object_disabled_set(p->ad->new_event_button, EINA_FALSE);
78
79         Evas_Object *bg = cal_util_add_bg(layout, EINA_FALSE);
80         if (!bg) {
81
82                 free(p);
83                 free(layout);
84
85                 ERR("bg is null.");
86
87                 return NULL;
88         }
89
90         elm_object_part_content_set(layout, "base", bg);
91
92         evas_object_event_callback_add(layout, EVAS_CALLBACK_DEL, __cal_day_list_delete_layout, p);
93
94         char buffer[128] = {0};
95         cal_util_get_time_text(buffer, sizeof(buffer), CAL_UTIL_DATE_FORMAT_1, NULL, &ad->base_tm);
96
97         elm_naviframe_item_push(ad->naviframe, strdup(buffer), NULL, NULL, layout, NULL);
98
99         CAL_FN_END;
100
101         return layout;
102 }
103
104 static void __cal_day_list_add_each_event_to_genlist(calendar_record_h record, void* data)
105 {
106         cal_day_list_data* p = (cal_day_list_data*)data;
107         cal_list_base_add_item(p->list, record, false);
108 }
109
110 void cal_day_list_update_list(Evas_Object *ly)
111 {
112         CAL_FN_START;
113
114         c_ret_if(!ly);
115
116         cal_day_list_data *p = CAL_UTIL_GET_PRIV_DATA(ly);
117         c_ret_if(!p);
118
119         struct appdata *ad = p->ad;
120         c_ret_if(!ad);
121
122         p->tm_start = ad->base_tm;
123         p->tm_start.tm_hour = 0;
124         p->tm_start.tm_min = 0;
125         p->tm_start.tm_sec = 0;
126
127         p->tm_end = p->tm_start;
128         p->tm_end.tm_hour = 23;
129         p->tm_end.tm_min = 59;
130         p->tm_end.tm_sec = 59;
131
132         cal_util_update_tm_month(&p->tm_start, -3);
133         cal_util_update_tm_month(&p->tm_end, 3);
134
135         cal_list_base_clear(p->list);
136
137         ad->today_item = NULL;
138
139         calendar_query_h query = cal_query_create_list_range_query(
140                         _calendar_instance_normal_calendar_book._uri,
141                         _calendar_instance_normal_calendar_book.start_time,
142                         _calendar_instance_normal_calendar_book.end_time,
143                         &p->tm_start, &p->tm_end, false);
144
145         calendar_list_h list = NULL;
146         calendar_error_e error =
147                         calendar_db_get_records_with_query(query, 0, 0, &list);
148         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_db_get_records_with_query() has failed(%x)", error);
149
150         error = calendar_list_first(list);
151         if (error != CALENDAR_ERROR_NONE) {
152                 ERR("calendar_list_first() is failed(%x)", error);
153                 calendar_query_destroy(query);
154                 return;
155         }
156
157         p->list = cal_list_base_create(ad, p->layout, "sw", true, NULL, NULL);
158
159         cal_do_for_each_record_in_list(list, __cal_day_list_add_each_event_to_genlist, p);
160
161         calendar_list_destroy(list, false);
162         calendar_query_destroy(query);
163
164         CAL_FN_END;
165 }