0b935570dbc74e3785c78fcd3a9aff7402447a7e
[apps/core/preloaded/calendar.git] / src / list-broker.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 "list-provider.h"
19 #include "list-broker.h"
20 #include "list-comparator.h"
21 #include "query.h"
22
23 typedef struct {
24         cal_list_provider_h allday;
25         cal_list_provider_h normal;
26         cal_list_provider_h task;
27         struct tm current_day;
28         int direction;
29 } cal_list_broker_s;
30
31 cal_list_broker_h cal_list_broker_create(struct tm* start_date, int direction,
32                                          bool include_completed_tasks)
33 {
34         cal_list_broker_s* p = calloc(1, sizeof(cal_list_broker_s));
35
36         start_date->tm_hour = 0;
37         start_date->tm_min = 0;
38         start_date->tm_sec = 0;
39
40         calendar_query_h query_allday;
41         calendar_query_h query_normal;
42         calendar_query_h query_task;
43
44         if (direction > 0) {
45                 query_allday = cal_query_create_list_more_allday_query(
46                                 _calendar_instance_allday_calendar_book.end_time,
47                                 start_date, direction);
48                 query_normal = cal_query_create_list_more_normal_query(
49                                 _calendar_instance_normal_calendar_book.end_time,
50                                 start_date, direction);
51                 query_task = cal_query_create_list_more_task_query(
52                                 start_date, direction, include_completed_tasks);
53         } else {
54                 query_allday = cal_query_create_list_more_allday_query(
55                                 _calendar_instance_allday_calendar_book.start_time,
56                                 start_date, direction);
57                 query_normal = cal_query_create_list_more_normal_query(
58                                 _calendar_instance_normal_calendar_book.start_time,
59                                 start_date, direction);
60                 query_task = cal_query_create_list_more_task_query(
61                                 start_date, direction, include_completed_tasks);
62         }
63
64         p->allday = cal_list_provider_create(query_allday);
65         p->normal = cal_list_provider_create(query_normal);
66         p->task = cal_list_provider_create(query_task);
67
68         p->direction = direction;
69
70         return p;
71 }
72
73 bool cal_list_broker_no_more(cal_list_broker_h cursor)
74 {
75         cal_list_broker_s* p = (cal_list_broker_s*)cursor;
76         return
77                 cal_list_provider_no_more(p->allday) &&
78                 cal_list_provider_no_more(p->normal) &&
79                 cal_list_provider_no_more(p->task);
80 }
81
82 void cal_list_broker_fetch(cal_list_broker_h cursor)
83 {
84         cal_list_broker_s* p = (cal_list_broker_s*)cursor;
85
86         cal_list_provider_fetch(p->allday);
87         cal_list_provider_fetch(p->normal);
88         cal_list_provider_fetch(p->task);
89 }
90
91 calendar_record_h cal_list_broker_get_next(cal_list_broker_h cursor, bool* day_has_changed)
92 {
93         cal_list_broker_s* p = (cal_list_broker_s*)cursor;
94
95         *day_has_changed = false;
96
97         // Default case - next is indeterminable until more is fetched.
98         if (cal_list_provider_not_ready(p->allday) ||
99                 cal_list_provider_not_ready(p->normal) ||
100                 cal_list_provider_not_ready(p->task)) {
101                 DBG("One of the lists are not ready!");
102                 return NULL;
103         }
104
105         // Peak foremost record of each run.
106         // NULL record means there is no more!
107         calendar_record_h allday = cal_list_provider_top_p(p->allday);
108         calendar_record_h normal = cal_list_provider_top_p(p->normal);
109         calendar_record_h task = cal_list_provider_top_p(p->task);
110
111         int winner = cal_list_comparator_determine_next(allday, normal, task,
112                                                 _calendar_instance_allday_calendar_book.start_time,
113                                                 _calendar_instance_normal_calendar_book.start_time,
114                                                 _calendar_todo.due_time,
115                                                 p->direction,
116                                                 &p->current_day, day_has_changed);
117
118         switch (winner) {
119         case 1:
120                 DBG("Winner: allday");
121                 return cal_list_provider_pop(p->allday);
122         case 2:
123                 DBG("Winner: normal");
124                 return cal_list_provider_pop(p->normal);
125         case 3:
126                 DBG("Winner: task");
127                 return cal_list_provider_pop(p->task);
128         case 0:
129                 DBG("Winner: task");
130                 return cal_list_provider_pop(p->task);
131         default:
132                 DBG("Error!");
133                 return NULL;
134         }
135 }
136
137 const struct tm* cal_list_broker_get_current_day_p(cal_list_broker_h cursor)
138 {
139         cal_list_broker_s* p = (cal_list_broker_s*)cursor;
140
141         return &p->current_day;
142 }
143
144 void cal_list_broker_destroy(cal_list_broker_h cursor)
145 {
146         cal_list_broker_s* p = (cal_list_broker_s*)cursor;
147
148         cal_list_provider_destroy(p->allday);
149         cal_list_provider_destroy(p->normal);
150         cal_list_provider_destroy(p->task);
151 }