Tizen 2.1 base
[framework/pim/calendar-service.git] / common / cal_query.c
1 /*
2  * Calendar Service
3  *
4  * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <stdlib.h>
21
22 #include "cal_internal.h"
23 #include "cal_typedef.h"
24 #include "cal_view.h"
25 #include "cal_filter.h"
26
27 #include "cal_query.h"
28
29 static bool __cal_query_property_check(const cal_property_info_s *properties,
30         int count, unsigned int property_id);
31
32 static bool __cal_query_property_check(const cal_property_info_s *properties,
33         int count, unsigned int property_id)
34 {
35     int i;
36
37     for (i=0;i<count;i++)
38     {
39         cal_property_info_s *p = (cal_property_info_s*)&(properties[i]);
40         if (property_id == p->property_id) {
41             return true;
42         }
43     }
44     return false;
45 }
46
47 API int calendar_query_create( const char* view_uri, calendar_query_h* out_query )
48 {
49     cal_query_s *query;
50
51     retv_if(NULL == view_uri || NULL == out_query, CALENDAR_ERROR_INVALID_PARAMETER);
52
53     query = (cal_query_s *)calloc(1, sizeof(cal_query_s));
54     query->view_uri = strdup(view_uri);
55     query->properties = (cal_property_info_s *)_cal_view_get_property_info(view_uri, &query->property_count);
56     *out_query = (calendar_query_h)query;
57
58     return CALENDAR_ERROR_NONE;
59 }
60
61 API int calendar_query_set_projection(calendar_query_h query, unsigned int property_ids[], int count)
62 {
63     cal_query_s *que = NULL;
64     int i;
65     bool find;
66
67     retv_if(NULL == query, CALENDAR_ERROR_INVALID_PARAMETER);
68     que = (cal_query_s *)query;
69
70     for (i=0;i<count;i++)
71     {
72         find = __cal_query_property_check(que->properties, que->property_count, property_ids[i]);
73         retvm_if(false == find, CALENDAR_ERROR_INVALID_PARAMETER,
74                     "Invalid parameter : property_id(%d) is not supported on view_uri(%s)", property_ids[i], que->view_uri);
75
76         find = CAL_PROPERTY_CHECK_FLAGS(property_ids[i], CAL_PROPERTY_FLAGS_FILTER);
77         retvm_if(true == find, CALENDAR_ERROR_INVALID_PARAMETER,
78                     "Invalid parameter : property_id(%d) is not supported on view_uri(%s)", property_ids[i], que->view_uri);
79     }
80
81     CAL_FREE(que->projection);
82
83     que->projection = calloc(count, sizeof(unsigned int));
84     memcpy(que->projection, property_ids, sizeof(unsigned int) * count);
85     que->projection_count = count;
86
87     return CALENDAR_ERROR_NONE;
88 }
89
90 API int calendar_query_set_distinct(calendar_query_h query, bool set)
91 {
92     cal_query_s *que = NULL;
93
94     retv_if(NULL == query, CALENDAR_ERROR_INVALID_PARAMETER);
95     que = (cal_query_s *)query;
96
97     que->distinct = set;
98
99     return CALENDAR_ERROR_NONE;
100 }
101
102 API int calendar_query_set_filter(calendar_query_h query, calendar_filter_h filter)
103 {
104     cal_query_s *que;
105     calendar_filter_h new_filter;
106     int ret = CALENDAR_ERROR_NONE;
107
108     retv_if(NULL == query || NULL == filter, CALENDAR_ERROR_INVALID_PARAMETER);
109
110     que = (cal_query_s *)query;
111
112         if(((cal_composite_filter_s*)filter)->filters == NULL)
113         {
114                 ERR("Empty filter");
115                 return CALENDAR_ERROR_NO_DATA;
116         }
117
118     ret = _cal_filter_clone(filter,&new_filter);
119     retv_if(ret!=CALENDAR_ERROR_NONE, ret);
120
121     if (que->filter)
122     {
123         calendar_filter_destroy((calendar_filter_h)que->filter);
124     }
125
126     que->filter = (cal_composite_filter_s*)new_filter;
127
128     return ret;
129 }
130
131 API int calendar_query_set_sort(calendar_query_h query, unsigned int property_id, bool asc)
132 {
133     cal_query_s *que;
134     bool find = false;
135
136     retv_if(NULL == query, CALENDAR_ERROR_INVALID_PARAMETER);
137     que = (cal_query_s *)query;
138
139
140     find = __cal_query_property_check(que->properties, que->property_count, property_id);
141     retvm_if(false == find, CALENDAR_ERROR_INVALID_PARAMETER,
142                 "Invalid paramter : property_id(%d) is not supported on view_uri(%s)", property_id, que->view_uri);
143
144     que->sort_property_id = property_id;
145     que->asc = asc;
146
147     return CALENDAR_ERROR_NONE;
148 }
149
150 API int calendar_query_destroy( calendar_query_h query )
151 {
152     cal_query_s *que;
153
154     retv_if(NULL == query, CALENDAR_ERROR_INVALID_PARAMETER);
155     que = (cal_query_s *)query;
156
157     if (que->filter)
158     {
159         calendar_filter_destroy((calendar_filter_h)que->filter);
160     }
161
162     CAL_FREE(que->view_uri);
163     CAL_FREE(que->projection);
164     CAL_FREE(que);
165
166     return CALENDAR_ERROR_NONE;
167 }
168
169 int _cal_query_clone(calendar_query_h query, calendar_query_h* out_query)
170 {
171     cal_query_s *que;
172     cal_query_s *out_que;
173     cal_filter_s *out_filter = NULL;
174     int ret = CALENDAR_ERROR_NONE;
175
176     retv_if(NULL == query, CALENDAR_ERROR_INVALID_PARAMETER);
177     que = (cal_query_s *)query;
178
179     ret = calendar_query_create(que->view_uri, out_query);
180     retv_if(CALENDAR_ERROR_NONE != ret, CALENDAR_ERROR_OUT_OF_MEMORY);
181     out_que = (cal_query_s *)*out_query;
182
183     if (que->filter)
184     {
185         _cal_filter_clone((calendar_filter_h)que->filter,(calendar_filter_h*)&out_filter);
186     }
187
188     if (que->projection_count > 0)
189     {
190         out_que->projection = calloc(que->projection_count, sizeof(unsigned int));
191         memcpy(out_que->projection, que->projection , sizeof(unsigned int) * que->projection_count);
192         out_que->projection_count = que->projection_count;
193     }
194     out_que->sort_property_id = que->sort_property_id;
195     out_que->asc = que->asc;
196
197     return CALENDAR_ERROR_NONE;
198 }
199