Resolved Native API Reference issues for calendar-service
[platform/core/pim/calendar-service.git] / common / cal_query.c
1 /*
2  * Calendar Service
3  *
4  * Copyright (c) 2012 - 2015 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 #include "cal_utils.h"
27
28 static bool _cal_query_property_check(const cal_property_info_s *properties,
29                 int count, unsigned int property_id)
30 {
31         int i;
32
33         for (i = 0; i < count; i++) {
34                 cal_property_info_s *p = (cal_property_info_s*)&(properties[i]);
35                 if (property_id == p->property_id)
36                         return true;
37         }
38         return false;
39 }
40
41 EXPORT_API int calendar_query_create(const char* view_uri, calendar_query_h* out_query)
42 {
43         CHECK_CALENDAR_SUPPORTED(CALENDAR_FEATURE);
44
45         cal_query_s *query;
46
47         RETV_IF(NULL == view_uri, CALENDAR_ERROR_INVALID_PARAMETER);
48         RETV_IF(NULL == out_query, CALENDAR_ERROR_INVALID_PARAMETER);
49
50         query = calloc(1, sizeof(cal_query_s));
51         RETV_IF(NULL == query, CALENDAR_ERROR_OUT_OF_MEMORY);
52
53 #ifdef CAL_IPC_CLIENT
54         cal_view_initialize();
55 #endif
56         query->view_uri = cal_strdup(view_uri);
57         query->properties = (cal_property_info_s *)cal_view_get_property_info(view_uri, &query->property_count);
58         *out_query = (calendar_query_h)query;
59
60         return CALENDAR_ERROR_NONE;
61 }
62
63 EXPORT_API int calendar_query_set_projection(calendar_query_h query, unsigned int property_ids[], int count)
64 {
65         CHECK_CALENDAR_SUPPORTED(CALENDAR_FEATURE);
66
67         cal_query_s *que = NULL;
68         int i;
69         bool find;
70
71         RETV_IF(NULL == query, CALENDAR_ERROR_INVALID_PARAMETER);
72         RETV_IF(NULL == property_ids, CALENDAR_ERROR_INVALID_PARAMETER);
73         RETVM_IF(count < 0, CALENDAR_ERROR_INVALID_PARAMETER, "count(%d) < 0", count);
74
75         que = (cal_query_s *)query;
76
77         for (i = 0; i < count; i++) {
78                 find = _cal_query_property_check(que->properties, que->property_count, property_ids[i]);
79                 RETVM_IF(false == find, CALENDAR_ERROR_INVALID_PARAMETER,
80                                 "Invalid parameter : property_id(%d) is not supported on view_uri(%s)", property_ids[i], que->view_uri);
81
82                 find = CAL_PROPERTY_CHECK_FLAGS(property_ids[i], CAL_PROPERTY_FLAGS_FILTER);
83                 RETVM_IF(true == find, CALENDAR_ERROR_INVALID_PARAMETER,
84                                 "Invalid parameter : property_id(%d) is not supported on view_uri(%s)", property_ids[i], que->view_uri);
85         }
86
87         CAL_FREE(que->projection);
88
89         que->projection = calloc(count, sizeof(unsigned int));
90         RETVM_IF(NULL == que->projection, CALENDAR_ERROR_OUT_OF_MEMORY, "calloc() Fail");
91         memcpy(que->projection, property_ids, sizeof(unsigned int) * count);
92         que->projection_count = count;
93
94         return CALENDAR_ERROR_NONE;
95 }
96
97 EXPORT_API int calendar_query_set_distinct(calendar_query_h query, bool set)
98 {
99         CHECK_CALENDAR_SUPPORTED(CALENDAR_FEATURE);
100
101         cal_query_s *que = NULL;
102
103         RETV_IF(NULL == query, CALENDAR_ERROR_INVALID_PARAMETER);
104         que = (cal_query_s *)query;
105
106         que->distinct = set;
107
108         return CALENDAR_ERROR_NONE;
109 }
110
111 EXPORT_API int calendar_query_set_filter(calendar_query_h query, calendar_filter_h filter)
112 {
113         CHECK_CALENDAR_SUPPORTED(CALENDAR_FEATURE);
114
115         cal_query_s *s_query;
116         cal_composite_filter_s *s_filter;
117         calendar_filter_h new_filter;
118         int ret = CALENDAR_ERROR_NONE;
119
120         RETV_IF(NULL == query, CALENDAR_ERROR_INVALID_PARAMETER);
121         RETV_IF(NULL == filter, CALENDAR_ERROR_INVALID_PARAMETER);
122
123         s_query = (cal_query_s *)query;
124         s_filter = (cal_composite_filter_s*)filter;
125         if (NULL == s_filter->filters) {
126                 /* LCOV_EXCL_START */
127                 ERR("Empty filter");
128                 return CALENDAR_ERROR_NO_DATA;
129                 /* LCOV_EXCL_STOP */
130         }
131
132         if (s_query->view_uri && s_filter->view_uri
133                         && CAL_STRING_EQUAL != strcmp(s_query->view_uri, s_filter->view_uri)) {
134                 /* LCOV_EXCL_START */
135                 ERR("View is not same query[%s] filter[%s]", s_query->view_uri, s_filter->view_uri);
136                 return CALENDAR_ERROR_INVALID_PARAMETER;
137                 /* LCOV_EXCL_STOP */
138         }
139
140         ret = cal_filter_clone(filter, &new_filter);
141         RETV_IF(ret != CALENDAR_ERROR_NONE, ret);
142
143         if (s_query->filter)
144                 calendar_filter_destroy((calendar_filter_h)s_query->filter);
145
146         s_query->filter = (cal_composite_filter_s*)new_filter;
147
148         return ret;
149 }
150
151 EXPORT_API int calendar_query_set_sort(calendar_query_h query, unsigned int property_id, bool asc)
152 {
153         CHECK_CALENDAR_SUPPORTED(CALENDAR_FEATURE);
154
155         cal_query_s *que;
156         bool find = false;
157
158         RETV_IF(NULL == query, CALENDAR_ERROR_INVALID_PARAMETER);
159         que = (cal_query_s *)query;
160
161
162         find = _cal_query_property_check(que->properties, que->property_count, property_id);
163         RETVM_IF(false == find, CALENDAR_ERROR_INVALID_PARAMETER,
164                         "Invalid paramter : property_id(%d) is not supported on view_uri(%s)", property_id, que->view_uri);
165
166         que->sort_property_id = property_id;
167         que->asc = asc;
168
169         return CALENDAR_ERROR_NONE;
170 }
171
172 EXPORT_API int calendar_query_destroy(calendar_query_h query)
173 {
174         CHECK_CALENDAR_SUPPORTED(CALENDAR_FEATURE);
175
176         cal_query_s *que;
177
178         RETV_IF(NULL == query, CALENDAR_ERROR_INVALID_PARAMETER);
179         que = (cal_query_s *)query;
180
181 #ifdef CAL_IPC_CLIENT
182         cal_view_finalize();
183 #endif
184
185         if (que->filter)
186                 calendar_filter_destroy((calendar_filter_h)que->filter);
187
188         CAL_FREE(que->view_uri);
189         CAL_FREE(que->projection);
190         CAL_FREE(que);
191
192         return CALENDAR_ERROR_NONE;
193 }