4 * Copyright (c) 2012 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include "cal_internal.h"
23 #include "cal_typedef.h"
25 #include "cal_filter.h"
26 #include "cal_utils.h"
28 static bool _cal_query_property_check(const cal_property_info_s *properties,
29 int count, unsigned int property_id)
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)
41 EXPORT_API int calendar_query_create(const char* view_uri, calendar_query_h* out_query)
43 CHECK_CALENDAR_SUPPORTED(CALENDAR_FEATURE);
47 RETV_IF(NULL == view_uri, CALENDAR_ERROR_INVALID_PARAMETER);
48 RETV_IF(NULL == out_query, CALENDAR_ERROR_INVALID_PARAMETER);
50 query = calloc(1, sizeof(cal_query_s));
51 RETV_IF(NULL == query, CALENDAR_ERROR_OUT_OF_MEMORY);
54 cal_view_initialize();
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;
60 return CALENDAR_ERROR_NONE;
63 EXPORT_API int calendar_query_set_projection(calendar_query_h query, unsigned int property_ids[], int count)
65 CHECK_CALENDAR_SUPPORTED(CALENDAR_FEATURE);
67 cal_query_s *que = NULL;
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);
75 que = (cal_query_s *)query;
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);
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);
87 CAL_FREE(que->projection);
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;
94 return CALENDAR_ERROR_NONE;
97 EXPORT_API int calendar_query_set_distinct(calendar_query_h query, bool set)
99 CHECK_CALENDAR_SUPPORTED(CALENDAR_FEATURE);
101 cal_query_s *que = NULL;
103 RETV_IF(NULL == query, CALENDAR_ERROR_INVALID_PARAMETER);
104 que = (cal_query_s *)query;
108 return CALENDAR_ERROR_NONE;
111 EXPORT_API int calendar_query_set_filter(calendar_query_h query, calendar_filter_h filter)
113 CHECK_CALENDAR_SUPPORTED(CALENDAR_FEATURE);
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;
120 RETV_IF(NULL == query, CALENDAR_ERROR_INVALID_PARAMETER);
121 RETV_IF(NULL == filter, CALENDAR_ERROR_INVALID_PARAMETER);
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 */
128 return CALENDAR_ERROR_NO_DATA;
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;
140 ret = cal_filter_clone(filter, &new_filter);
141 RETV_IF(ret != CALENDAR_ERROR_NONE, ret);
144 calendar_filter_destroy((calendar_filter_h)s_query->filter);
146 s_query->filter = (cal_composite_filter_s*)new_filter;
151 EXPORT_API int calendar_query_set_sort(calendar_query_h query, unsigned int property_id, bool asc)
153 CHECK_CALENDAR_SUPPORTED(CALENDAR_FEATURE);
158 RETV_IF(NULL == query, CALENDAR_ERROR_INVALID_PARAMETER);
159 que = (cal_query_s *)query;
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);
166 que->sort_property_id = property_id;
169 return CALENDAR_ERROR_NONE;
172 EXPORT_API int calendar_query_destroy(calendar_query_h query)
174 CHECK_CALENDAR_SUPPORTED(CALENDAR_FEATURE);
178 RETV_IF(NULL == query, CALENDAR_ERROR_INVALID_PARAMETER);
179 que = (cal_query_s *)query;
181 #ifdef CAL_IPC_CLIENT
186 calendar_filter_destroy((calendar_filter_h)que->filter);
188 CAL_FREE(que->view_uri);
189 CAL_FREE(que->projection);
192 return CALENDAR_ERROR_NONE;