7aa4ff4c2e8b3a247fa239b2b2d7e0087e704081
[apps/core/preloaded/calendar.git] / src / list-data.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-data.h"
19 #include "list-comparator.h"
20
21 typedef struct {
22         calendar_list_h list1;
23         calendar_list_h list2;
24         calendar_list_h list3;
25         unsigned int time_property_id1;
26         unsigned int time_property_id2;
27         unsigned int time_property_id3;
28         struct tm current_day;
29 } cal_list_data_s;
30
31 static calendar_list_h __cal_list_data_retreive_list(calendar_query_h query)
32 {
33         c_retv_if(query == NULL, NULL);
34
35         calendar_list_h list = NULL;
36         calendar_error_e error = calendar_db_get_records_with_query(query, 0, 0, &list);
37         c_retvm_if(error != CALENDAR_ERROR_NONE, NULL, "calendar_db_get_records_with_query() has failed(%x)", error);
38
39         error = calendar_list_first(list);
40         c_retvm_if(error != CALENDAR_ERROR_NONE, NULL, "calendar_list_first() has failed(%x)", error);
41
42         return list;
43 }
44
45 cal_list_data_h
46         cal_list_data_create(
47                 calendar_query_h query1, calendar_query_h query2, calendar_query_h query3,
48                 unsigned int time_property_id1, unsigned int time_property_id2, unsigned int time_property_id3)
49 {
50         cal_list_data_s* p = (cal_list_data_s*)calloc(1, sizeof(cal_list_data_s));
51
52         p->list1 = __cal_list_data_retreive_list(query1);
53         p->list2 = __cal_list_data_retreive_list(query2);
54         p->list3 = __cal_list_data_retreive_list(query3);
55
56         p->time_property_id1 = time_property_id1;
57         p->time_property_id2 = time_property_id2;
58         p->time_property_id3 = time_property_id3;
59
60         return p;
61 }
62
63 static calendar_record_h __cal_list_data_get_next(calendar_list_h list)
64 {
65         calendar_record_h record = NULL;
66         calendar_error_e error = calendar_list_get_current_record_p(list, &record);
67         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_get_current_record_p() has failed(%x)", error);
68         return record;
69 }
70
71 calendar_record_h cal_list_data_get_next(cal_list_data_h cursor, bool* day_has_changed)
72 {
73         c_retv_if(cursor == NULL, NULL);
74         cal_list_data_s* p = (cal_list_data_s*)cursor;
75
76         // Peak foremost record of each run.
77         // NULL record means there is no more!
78         calendar_record_h record1 = __cal_list_data_get_next(p->list1);
79         calendar_record_h record2 = __cal_list_data_get_next(p->list2);
80         calendar_record_h record3 = __cal_list_data_get_next(p->list3);
81
82         int winner = cal_list_comparator_determine_next(
83                                                 record1, record2, record3,
84                                                 p->time_property_id1, p->time_property_id2, p->time_property_id3,
85                                                 1, &p->current_day, day_has_changed);
86
87         switch (winner) {
88         case 1:
89                 DBG("Winner: 1");
90                 calendar_list_next(p->list1);
91                 return record1;
92         case 2:
93                 DBG("Winner: 2");
94                 calendar_list_next(p->list2);
95                 return record2;
96         case 3:
97                 DBG("Winner: 3");
98                 calendar_list_next(p->list3);
99                 return record3;
100         case 0:
101                 DBG("Winner: All records have been processed!");
102                 return NULL;
103         default:
104                 DBG("Error!");
105                 return NULL;
106         }
107 }
108
109 void cal_list_data_destroy(cal_list_data_h cursor, bool delete_records)
110 {
111         c_ret_if(cursor == NULL);
112         cal_list_data_s* p = (cal_list_data_s*)cursor;
113
114         if (p->list1 != NULL)
115                 calendar_list_destroy(p->list1, delete_records);
116         if (p->list2 != NULL)
117                 calendar_list_destroy(p->list2, delete_records);
118         if (p->list3 != NULL)
119                 calendar_list_destroy(p->list3, delete_records);
120
121         free(p);
122 }