Rearrange contacts-service.service from default.target to delayed.target
[platform/core/pim/contacts-service.git] / common / ctsvc_query.c
1 /*
2  * Contacts Service
3  *
4  * Copyright (c) 2010 - 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 "contacts.h"
21 #include "ctsvc_internal.h"
22 #include "ctsvc_filter.h"
23
24 typedef enum {
25         QUERY_SORTKEY,
26         QUERY_PROJECTION,
27 } query_property_type_e;
28
29 static bool __ctsvc_query_property_check(const property_info_s *properties,
30                 int count, query_property_type_e property_type, unsigned int property_id)
31 {
32         int i;
33
34         for (i = 0; i < count; i++) {
35                 property_info_s *p = (property_info_s*)&(properties[i]);
36                 if (property_id == p->property_id) {
37                         if (property_type == QUERY_PROJECTION) {
38                                 if (p->property_type == CTSVC_SEARCH_PROPERTY_ALL ||
39                                                 p->property_type == CTSVC_SEARCH_PROPERTY_PROJECTION)
40                                         return true;
41                                 else
42                                         return false;
43                         } else {
44                                 return true;
45                         }
46                 }
47         }
48         return false;
49 }
50
51 EXPORT_API int contacts_query_create(const char *view_uri, contacts_query_h *out_query)
52 {
53         CHECK_CONTACT_SUPPORTED(CONTACT_FEATURE);
54         ctsvc_query_s *query;
55
56         RETV_IF(NULL == out_query, CONTACTS_ERROR_INVALID_PARAMETER);
57         *out_query = NULL;
58
59         RETV_IF(NULL == view_uri || NULL == out_query, CONTACTS_ERROR_INVALID_PARAMETER);
60
61         query = calloc(1, sizeof(ctsvc_query_s));
62         RETV_IF(NULL == query, CONTACTS_ERROR_OUT_OF_MEMORY);
63
64 #ifdef _CONTACTS_IPC_CLIENT
65         ctsvc_view_uri_init();
66 #endif
67
68         query->view_uri = strdup(view_uri);
69         query->properties = (property_info_s*)ctsvc_view_get_all_property_infos(view_uri, &query->property_count);
70         *out_query = (contacts_query_h)query;
71
72         return CONTACTS_ERROR_NONE;
73 }
74
75 EXPORT_API int contacts_query_set_projection(contacts_query_h query, unsigned int property_ids[], int count)
76 {
77         CHECK_CONTACT_SUPPORTED(CONTACT_FEATURE);
78         ctsvc_query_s *query_s;
79         int i;
80         bool find;
81
82         RETV_IF(NULL == query, CONTACTS_ERROR_INVALID_PARAMETER);
83         query_s = (ctsvc_query_s*)query;
84
85         for (i = 0; i < count; i++) {
86                 find = __ctsvc_query_property_check(query_s->properties, query_s->property_count,
87                                 QUERY_PROJECTION, property_ids[i]);
88                 RETVM_IF(false == find, CONTACTS_ERROR_INVALID_PARAMETER,
89                                 "property_id(%d) is not supported on view_uri(%s)", property_ids[i], query_s->view_uri);
90         }
91         if (query_s->projection)
92                 free(query_s->projection);
93
94         query_s->projection = calloc(count, sizeof(unsigned int));
95         if (NULL == query_s->projection) {
96                 /* LCOV_EXCL_START */
97                 ERR("calloc() Fail");
98                 return CONTACTS_ERROR_OUT_OF_MEMORY;
99                 /* LCOV_EXCL_STOP */
100         }
101         memcpy(query_s->projection, property_ids, sizeof(unsigned int) * count);
102         query_s->projection_count = count;
103
104         return CONTACTS_ERROR_NONE;
105 }
106
107 EXPORT_API int contacts_query_set_filter(contacts_query_h query, contacts_filter_h filter)
108 {
109         CHECK_CONTACT_SUPPORTED(CONTACT_FEATURE);
110         int ret;
111         ctsvc_query_s *s_query;
112         ctsvc_composite_filter_s *s_filter;
113         contacts_filter_h new_filter;
114
115         RETV_IF(NULL == query || NULL == filter, CONTACTS_ERROR_INVALID_PARAMETER);
116         s_query = (ctsvc_query_s*)query;
117         s_filter = (ctsvc_composite_filter_s*)filter;
118         if (s_query->view_uri && s_filter->view_uri && STRING_EQUAL != strcmp(s_query->view_uri, s_filter->view_uri))
119                 return CONTACTS_ERROR_INVALID_PARAMETER;
120
121         ret = ctsvc_filter_clone(filter, &new_filter);
122         RETVM_IF(ret != CONTACTS_ERROR_NONE, ret, "ctsvc_filter_clone Fail(%d)", ret);
123         s_query->filter = (ctsvc_composite_filter_s*)new_filter;
124
125         return CONTACTS_ERROR_NONE;
126 }
127
128 EXPORT_API int contacts_query_set_sort(contacts_query_h query, unsigned int property_id, bool asc)
129 {
130         CHECK_CONTACT_SUPPORTED(CONTACT_FEATURE);
131         ctsvc_query_s *query_s;
132         bool find = false;
133
134         RETV_IF(NULL == query, CONTACTS_ERROR_INVALID_PARAMETER);
135         query_s = (ctsvc_query_s*)query;
136
137         find = __ctsvc_query_property_check(query_s->properties, query_s->property_count, QUERY_SORTKEY, property_id);
138         RETVM_IF(false == find, CONTACTS_ERROR_INVALID_PARAMETER,
139                         "property_id(%d) is not supported on view_uri(%s)", property_id, query_s->view_uri);
140         query_s->sort_property_id = property_id;
141         query_s->sort_asc = asc;
142
143         return CONTACTS_ERROR_NONE;
144 }
145
146 EXPORT_API int contacts_query_destroy(contacts_query_h query)
147 {
148         CHECK_CONTACT_SUPPORTED(CONTACT_FEATURE);
149         ctsvc_query_s *s_query;
150         RETV_IF(NULL == query, CONTACTS_ERROR_INVALID_PARAMETER);
151         s_query = (ctsvc_query_s*)query;
152
153 #ifdef _CONTACTS_IPC_CLIENT
154         ctsvc_view_uri_deinit();
155 #endif
156
157         if (s_query->filter)
158                 contacts_filter_destroy((contacts_filter_h)s_query->filter);
159
160         free(s_query->projection);
161         free(s_query->view_uri);
162         free(s_query);
163
164         return CONTACTS_ERROR_NONE;
165 }
166
167 EXPORT_API int contacts_query_set_distinct(contacts_query_h query, bool set)
168 {
169         CHECK_CONTACT_SUPPORTED(CONTACT_FEATURE);
170         ctsvc_query_s *query_s;
171
172         RETV_IF(NULL == query, CONTACTS_ERROR_INVALID_PARAMETER);
173         query_s = (ctsvc_query_s*)query;
174         query_s->distinct = set;
175
176         return CONTACTS_ERROR_NONE;
177 }
178