a79e307ada78f675329d5cdf3883717f662be0b4
[apps/core/preloaded/calendar.git] / src / list-provider.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 <calendar2.h>
20
21 #define BATCH_SIZE 10
22
23 typedef struct {
24         calendar_query_h query;
25         calendar_list_h current_batch;
26         calendar_list_h next_batch;
27         int offset;
28         bool exhausted;
29 } cal_list_provider_s;
30
31 cal_list_provider_h cal_list_provider_create(calendar_query_h query)
32 {
33         cal_list_provider_s* p = (cal_list_provider_s*)calloc(1, sizeof(cal_list_provider_s));
34
35         p->query = query;
36
37         return p;
38 }
39
40 static calendar_list_h __cal_list_provider_fetch(cal_list_provider_s* p)
41 {
42         calendar_list_h list = NULL;
43
44         calendar_error_e error =
45                         calendar_db_get_records_with_query(p->query, p->offset, BATCH_SIZE, &list);
46         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_db_get_records_with_query() has failed(%x)", error);
47
48         if (error == CALENDAR_ERROR_NO_DATA) {
49                 DBG("Exhausted!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
50                 p->exhausted = true;
51                 return NULL;
52         }
53
54         int retreived_count = 0;
55         error = calendar_list_get_count(list, &retreived_count);
56         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_get_count() has failed(%x)", error);
57
58         DBG("offset(%d), retreived_count(%d)", p->offset, retreived_count);
59
60         if (retreived_count == 0) {
61                 DBG("Exhausted!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
62                 error = calendar_list_destroy(list, false);
63                 p->exhausted = true;
64                 return NULL;
65         } else if (retreived_count < BATCH_SIZE) {
66                 DBG("Exhausted!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
67                 p->exhausted = true;
68         }
69
70         p->offset += retreived_count;
71
72         error = calendar_list_first(list);
73         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_first() has failed(%x)", error);
74
75         return list;
76 }
77
78 void cal_list_provider_fetch(cal_list_provider_h cursor)
79 {
80         cal_list_provider_s* p = (cal_list_provider_s*)cursor;
81
82         c_ret_if(p->next_batch != NULL);
83         c_ret_if(p->exhausted);
84
85         if (p->current_batch == NULL)
86                 p->current_batch = __cal_list_provider_fetch(p);
87
88         if (p->current_batch != NULL)
89                 p->next_batch = __cal_list_provider_fetch(p);
90 }
91
92 bool cal_list_provider_not_ready(const cal_list_provider_h cursor)
93 {
94         const cal_list_provider_s* p = (const cal_list_provider_s*)cursor;
95         return p->current_batch == NULL && !p->exhausted;
96 }
97
98 bool cal_list_provider_no_more(const cal_list_provider_h cursor)
99 {
100         const cal_list_provider_s* p = (const cal_list_provider_s*)cursor;
101         return p->current_batch == NULL && p->exhausted;
102 }
103
104 calendar_record_h cal_list_provider_top_p(cal_list_provider_h cursor)
105 {
106         const cal_list_provider_s* p = (const cal_list_provider_s*)cursor;
107
108         calendar_record_h record = NULL;
109         calendar_error_e error = calendar_list_get_current_record_p(p->current_batch, &record);
110         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_get_current_record_p() has failed(%x)", error);
111
112         return record;
113 }
114
115 calendar_record_h cal_list_provider_pop(cal_list_provider_h cursor)
116 {
117         cal_list_provider_s* p = (cal_list_provider_s*)cursor;
118
119         calendar_record_h record = NULL;
120         calendar_error_e error = calendar_list_get_current_record_p(p->current_batch, &record);
121         if (error != CALENDAR_ERROR_NONE)
122                 return NULL;
123
124         error = calendar_list_next(p->current_batch);
125
126         if (error) {
127                 DBG("Shifting next_batch to current_batch");
128                 error = calendar_list_destroy(p->current_batch, false);
129                 p->current_batch = p->next_batch;
130                 p->next_batch = NULL;
131         }
132
133         return record;
134 }
135
136 void cal_list_provider_destroy(cal_list_provider_h cursor)
137 {
138         cal_list_provider_s* p = (cal_list_provider_s*)cursor;
139
140         calendar_error_e error = calendar_list_destroy(p->current_batch, true);
141         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_destroy() has failed(%x)", error);
142
143         error = calendar_list_destroy(p->next_batch, true);
144         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_destroy() has failed(%x)", error);
145
146         error = calendar_query_destroy(p->query);
147         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_destroy() has failed(%x)", error);
148 }