Rename some macros
[platform/core/api/media-content.git] / src / media_filter.c
1 /*
2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18 #include <media_info_private.h>
19 #include <media_util_private.h>
20 #include <vconf.h>
21
22 static bool __is_pinyin_needed(void)
23 {
24         char *lang = NULL;
25         const char *china = "zh_CN";
26         const char *hongkong = "zh_HK";
27         int ret = FALSE;
28
29         /*Check CSC first*/
30         bool pinyin_support = FALSE;
31         media_svc_check_pinyin_support(&pinyin_support);
32         if (pinyin_support) {
33                 /*Check Language Setting*/
34                 lang = vconf_get_str(VCONFKEY_LANGSET);
35                 content_retvm_if(lang == NULL, ret, "Fail to get string of language set");
36
37                 if ((strncmp(china, lang, strlen(china)) == 0) ||
38                         (strncmp(hongkong, lang, strlen(hongkong)) == 0)) {
39                         ret = TRUE;
40                 }
41
42                 SAFE_FREE(lang);
43         }
44
45         return ret;
46 }
47
48 static const char *__get_order_str(media_content_order_e order_enum)
49 {
50         switch (order_enum) {
51         case MEDIA_CONTENT_ORDER_ASC:
52                 return "ASC ";
53         case MEDIA_CONTENT_ORDER_DESC:
54                 return "DESC ";
55         default:
56                 return " ";
57         }
58 }
59
60 static const char *__get_collate_str(media_content_collation_e collate_type)
61 {
62         switch (collate_type) {
63         case MEDIA_CONTENT_COLLATE_NOCASE:
64                 return " COLLATE NOCASE ";
65         case MEDIA_CONTENT_COLLATE_RTRIM:
66                 return " COLLATE RTRIM ";
67         case MEDIA_CONTENT_COLLATE_LOCALIZED:
68                 if (__is_pinyin_needed())
69                         return " COLLATE NOCASE ";
70                 else
71                         return " COLLATE localized ";
72         default:
73                 return " ";
74         }
75 }
76
77 static bool __check_collate_type(media_content_collation_e collate_type)
78 {
79         switch (collate_type) {
80         case MEDIA_CONTENT_COLLATE_DEFAULT:
81         case MEDIA_CONTENT_COLLATE_NOCASE:
82         case MEDIA_CONTENT_COLLATE_RTRIM:
83         case MEDIA_CONTENT_COLLATE_LOCALIZED:
84                 return true;
85         default:
86                 return false;
87         }
88 }
89
90 static bool __check_order_type(media_content_order_e order)
91 {
92         switch (order) {
93         case MEDIA_CONTENT_ORDER_ASC:
94         case MEDIA_CONTENT_ORDER_DESC:
95         case MEDIA_CONTENT_ORDER_OTHER:
96                 return true;
97         default:
98                 return false;
99         }
100 }
101
102 int _media_filter_build_condition(bool is_full, const char *condition, media_content_collation_e collate_type, char **result)
103 {
104         content_retvm_if(!result, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid result");
105         content_retvm_if(!STRING_VALID(condition), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid condition");
106         content_retvm_if(!__check_collate_type(collate_type), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid collate_type");
107
108         if (is_full)
109                 *result = g_strdup_printf("(%s)", condition);
110         else
111                 *result = g_strdup_printf("(%s%s)", condition, __get_collate_str(collate_type));
112
113         content_sec_debug("Condition : %s", *result);
114
115         return MEDIA_CONTENT_ERROR_NONE;
116 }
117
118 int _media_filter_build_option(filter_h filter, char **result)
119 {
120         filter_s *_filter = (filter_s *)filter;
121
122         content_retvm_if(!_filter, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid filter");
123         content_retvm_if(!result, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid result");
124
125         if (!STRING_VALID(_filter->order_keyword)) {
126                 *result = g_strdup_printf(" LIMIT %d, %d", _filter->offset, _filter->count);
127                 return MEDIA_CONTENT_ERROR_NONE;
128         }
129
130         if (_filter->is_full_order) {
131                 *result = g_strdup_printf("ORDER BY %s LIMIT %d, %d", _filter->order_keyword, _filter->offset, _filter->count);
132                 return MEDIA_CONTENT_ERROR_NONE;
133         }
134
135         if (_filter->order_type == MEDIA_CONTENT_ORDER_ASC || _filter->order_type == MEDIA_CONTENT_ORDER_DESC) {
136                 *result = g_strdup_printf("ORDER BY %s%s%s LIMIT %d, %d",
137                         _filter->order_keyword,
138                         __get_collate_str(_filter->order_collate_type),
139                         __get_order_str(_filter->order_type),
140                         _filter->offset,
141                         _filter->count);
142         } else {
143                 *result = g_strdup_printf("%s LIMIT %d, %d", _filter->order_keyword, _filter->offset, _filter->count);
144         }
145
146         return MEDIA_CONTENT_ERROR_NONE;
147 }
148
149 int media_filter_create(filter_h *filter)
150 {
151         int ret = MEDIA_CONTENT_ERROR_NONE;
152
153         content_retvm_if(filter == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid filter");
154
155         filter_s *_filter = (filter_s *)calloc(1, sizeof(filter_s));
156         content_retvm_if(_filter == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
157
158         _filter->storage_id = NULL;
159         _filter->condition = NULL;
160         _filter->order_keyword = NULL;
161         _filter->order_type = -1;
162         _filter->condition_collate_type = MEDIA_CONTENT_COLLATE_DEFAULT;
163         _filter->order_collate_type = MEDIA_CONTENT_COLLATE_DEFAULT;
164         _filter->offset = -1;
165         _filter->count = -1;
166         _filter->is_full_condition = false;
167         _filter->is_full_order = false;
168
169         *filter = (filter_h)_filter;
170
171         return ret;
172 }
173
174 int media_filter_destroy(filter_h filter)
175 {
176         filter_s *_filter = (filter_s *)filter;
177         content_retvm_if(!_filter, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid filter");
178
179         SAFE_FREE(_filter->storage_id);
180         SAFE_FREE(_filter->condition);
181         SAFE_FREE(_filter->order_keyword);
182         SAFE_FREE(_filter);
183
184         return MEDIA_CONTENT_ERROR_NONE;
185 }
186
187 int media_filter_set_offset(filter_h filter, int offset, int count)
188 {
189         filter_s *_filter = (filter_s *)filter;
190         content_retvm_if(!_filter, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid filter");
191
192         _filter->offset = offset;
193         _filter->count = count;
194
195         return MEDIA_CONTENT_ERROR_NONE;
196 }
197
198 int media_filter_set_condition(filter_h filter, const char *condition, media_content_collation_e collate_type)
199 {
200         int ret = MEDIA_CONTENT_ERROR_NONE;
201         filter_s *_filter = (filter_s *)filter;
202         content_retvm_if(!_filter, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid filter");
203         content_retvm_if(!STRING_VALID(condition), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid condition");
204         content_retvm_if(!__check_collate_type(collate_type), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid collate_type");
205
206         _filter->is_full_condition = false;
207         SAFE_FREE(_filter->condition);
208
209         _filter->condition = _media_content_replace_path_in_condition(condition);
210
211         /* FIXME
212                 If an error is occured in _media_content_replace_path_in_condition(),
213                 A suitable return value is 'MEDIA_CONTENT_ERROR_INVALID_OPERATION'.
214                 However, it is not stated in the description of media_filter_set_condition().
215                 Therefore, use 'MEDIA_CONTENT_ERROR_OUT_OF_MEMORY' temporarily.
216                 It will be modified after removing _media_content_replace_path_in_condition() function.
217         */
218         content_retvm_if(_filter->condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "Fail to set condition");
219
220         content_sec_debug("Condition string : %s", _filter->condition);
221
222         _filter->condition_collate_type = collate_type;
223
224         return ret;
225 }
226
227 int media_filter_set_order(filter_h filter, media_content_order_e order_type, const char *order_keyword, media_content_collation_e collate_type)
228 {
229         filter_s *_filter = (filter_s *)filter;
230         content_retvm_if(!_filter, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid filter");
231         content_retvm_if(!STRING_VALID(order_keyword), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid order_keyword");
232         content_retvm_if(!__check_order_type(order_type), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid order_type");
233         content_retvm_if(!__check_collate_type(collate_type), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid collate_type");
234
235         _filter->is_full_order = false;
236         SAFE_FREE(_filter->order_keyword);
237
238         _filter->order_keyword = g_strdup(order_keyword);
239         _filter->order_type = order_type;
240         _filter->order_collate_type = collate_type;
241
242         return MEDIA_CONTENT_ERROR_NONE;
243 }
244
245 int media_filter_set_storage(filter_h filter, const char *storage_id)
246 {
247         content_warn("DEPRECATION WARNING: media_filter_set_storage() is deprecated and will be removed from next release. Use media_filter_set_condition() with MEDIA_PATH keyword instead.");
248         filter_s *_filter = (filter_s *)filter;
249         content_retvm_if(!_filter || !STRING_VALID(storage_id), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid parameter");
250
251         SAFE_FREE(_filter->storage_id);
252
253         _filter->storage_id = g_strdup(storage_id);
254         content_sec_debug("storage_id : %s", _filter->storage_id);
255
256         return MEDIA_CONTENT_ERROR_NONE;
257 }
258
259 int media_filter_get_offset(filter_h filter, int *offset, int *count)
260 {
261         filter_s *_filter = (filter_s *)filter;
262         content_retvm_if(!_filter || !offset || !count, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid parameter");
263
264         *offset = _filter->offset;
265         *count = _filter->count;
266
267         return MEDIA_CONTENT_ERROR_NONE;
268 }
269
270 int media_filter_get_condition(filter_h filter, char **condition, media_content_collation_e *collate_type)
271 {
272         filter_s *_filter = (filter_s *)filter;
273         content_retvm_if(!_filter, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid _filter");
274         content_retvm_if(!condition, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid condition");
275         content_retvm_if(!collate_type, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid collate_type");
276
277         if (!_filter->is_full_condition)
278                 *condition = g_strdup(_filter->condition);
279
280         *collate_type = _filter->condition_collate_type;
281
282         return MEDIA_CONTENT_ERROR_NONE;
283 }
284
285 int media_filter_get_order(filter_h filter, media_content_order_e *order_type, char **order_keyword, media_content_collation_e *collate_type)
286 {
287         filter_s *_filter = (filter_s *)filter;
288         content_retvm_if(!_filter || !order_type, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid parameter");
289         content_retvm_if(!order_keyword || !collate_type, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid parameter");
290
291         if (!_filter->is_full_order)
292                 *order_keyword = g_strdup(_filter->order_keyword);
293
294         *order_type = _filter->order_type;
295         *collate_type = _filter->order_collate_type;
296
297         return MEDIA_CONTENT_ERROR_NONE;
298 }
299
300 int media_filter_get_storage(filter_h filter, char **storage_id)
301 {
302         content_warn("DEPRECATION WARNING: media_filter_get_storage() is deprecated and will be removed from next release.");
303         filter_s *_filter = (filter_s *)filter;
304         content_retvm_if(!_filter || !storage_id, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid parameter");
305
306         *storage_id = g_strdup(_filter->storage_id);
307
308         return MEDIA_CONTENT_ERROR_NONE;
309 }
310
311 int media_filter_set_condition_v2(filter_h filter, const char *condition)
312 {
313         filter_s *_filter = (filter_s *)filter;
314         content_retvm_if(!_filter || !STRING_VALID(condition), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid parameter");
315
316         _filter->is_full_condition = true;
317         SAFE_FREE(_filter->condition);
318
319         /* FIXME
320                 If an error is occured in _media_content_replace_path_in_condition(),
321                 A suitable return value is 'MEDIA_CONTENT_ERROR_INVALID_OPERATION'.
322                 However, it is not stated in the description of media_filter_set_condition().
323                 Therefore, use 'MEDIA_CONTENT_ERROR_OUT_OF_MEMORY' temporarily.
324                 It will be modified after removing _media_content_replace_path_in_condition() function.
325         */
326         _filter->condition = _media_content_replace_path_in_condition(condition);
327         content_retvm_if(_filter->condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "Fail to set condition");
328
329         content_sec_debug("Condition string : %s", _filter->condition);
330
331         return MEDIA_CONTENT_ERROR_NONE;
332 }
333
334 int media_filter_get_condition_v2(filter_h filter, char **condition)
335 {
336         filter_s *_filter = (filter_s *)filter;
337         content_retvm_if(!_filter || !condition, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid parameter");
338
339         if (_filter->is_full_condition)
340                 *condition = g_strdup(_filter->condition);
341
342         return MEDIA_CONTENT_ERROR_NONE;
343 }
344
345 int media_filter_set_order_v2(filter_h filter, const char *order)
346 {
347         filter_s *_filter = (filter_s *)filter;
348         content_retvm_if(!_filter || !STRING_VALID(order), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid parameter");
349
350         _filter->is_full_order = true;
351         SAFE_FREE(_filter->order_keyword);
352
353         _filter->order_keyword = g_strdup(order);
354
355         return MEDIA_CONTENT_ERROR_NONE;
356 }
357
358 int media_filter_get_order_v2(filter_h filter, char **order)
359 {
360         filter_s *_filter = (filter_s *)filter;
361         content_retvm_if(!_filter || !order, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid parameter");
362
363         if (_filter->is_full_order)
364                 *order = g_strdup(_filter->order_keyword);
365
366         return MEDIA_CONTENT_ERROR_NONE;
367 }
368