3 * Copyright 2012 Samsung Electronics Co., Ltd
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
9 * http://floralicense.org/license/
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.
18 #include "list-provider.h"
19 #include <calendar2.h>
24 calendar_query_h query;
25 calendar_list_h current_batch;
26 calendar_list_h next_batch;
29 } cal_list_provider_s;
31 cal_list_provider_h cal_list_provider_create(calendar_query_h query)
33 cal_list_provider_s* p = (cal_list_provider_s*)calloc(1, sizeof(cal_list_provider_s));
40 static calendar_list_h __cal_list_provider_fetch(cal_list_provider_s* p)
42 calendar_list_h list = NULL;
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);
48 if (error == CALENDAR_ERROR_NO_DATA) {
49 DBG("Exhausted!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
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);
58 DBG("offset(%d), retreived_count(%d)", p->offset, retreived_count);
60 if (retreived_count == 0) {
61 DBG("Exhausted!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
62 error = calendar_list_destroy(list, false);
65 } else if (retreived_count < BATCH_SIZE) {
66 DBG("Exhausted!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
70 p->offset += retreived_count;
72 error = calendar_list_first(list);
73 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_first() has failed(%x)", error);
78 void cal_list_provider_fetch(cal_list_provider_h cursor)
80 cal_list_provider_s* p = (cal_list_provider_s*)cursor;
82 c_ret_if(p->next_batch != NULL);
83 c_ret_if(p->exhausted);
85 if (p->current_batch == NULL)
86 p->current_batch = __cal_list_provider_fetch(p);
88 if (p->current_batch != NULL)
89 p->next_batch = __cal_list_provider_fetch(p);
92 bool cal_list_provider_not_ready(const cal_list_provider_h cursor)
94 const cal_list_provider_s* p = (const cal_list_provider_s*)cursor;
95 return p->current_batch == NULL && !p->exhausted;
98 bool cal_list_provider_no_more(const cal_list_provider_h cursor)
100 const cal_list_provider_s* p = (const cal_list_provider_s*)cursor;
101 return p->current_batch == NULL && p->exhausted;
104 calendar_record_h cal_list_provider_top_p(cal_list_provider_h cursor)
106 const cal_list_provider_s* p = (const cal_list_provider_s*)cursor;
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);
115 calendar_record_h cal_list_provider_pop(cal_list_provider_h cursor)
117 cal_list_provider_s* p = (cal_list_provider_s*)cursor;
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)
124 error = calendar_list_next(p->current_batch);
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;
136 void cal_list_provider_destroy(cal_list_provider_h cursor)
138 cal_list_provider_s* p = (cal_list_provider_s*)cursor;
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);
143 error = calendar_list_destroy(p->next_batch, true);
144 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_destroy() has failed(%x)", error);
146 error = calendar_query_destroy(p->query);
147 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_destroy() has failed(%x)", error);