8ca675f10f03759eae7d4c7eda462bc25df10790
[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                 media_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 int _media_filter_build_condition(bool is_full, const char *condition, media_content_collation_e collate_type, char **result)
91 {
92         media_content_retvm_if(result == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid out param");
93         media_content_retvm_if(!STRING_VALID(condition), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid condition");
94         media_content_retvm_if(!__check_collate_type(collate_type), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid condition collate");
95
96         if (is_full)
97                 *result = g_strdup_printf("(%s)", condition);
98         else
99                 *result = g_strdup_printf("(%s%s)", condition, __get_collate_str(collate_type));
100
101         media_content_sec_debug("Condition : %s", *result);
102
103         return MEDIA_CONTENT_ERROR_NONE;
104 }
105
106 int _media_filter_build_option(filter_h filter, char **result)
107 {
108         filter_s *_filter = (filter_s *)filter;
109
110         media_content_retvm_if(!_filter, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid filter");
111         media_content_retvm_if(!result, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid out param");
112
113         if (!STRING_VALID(_filter->order_keyword)) {
114                 *result = g_strdup_printf(" LIMIT %d, %d", _filter->offset, _filter->count);
115                 return MEDIA_CONTENT_ERROR_NONE;
116         }
117
118         if (_filter->is_full_order) {
119                 *result = g_strdup_printf("ORDER BY %s LIMIT %d, %d", _filter->order_keyword, _filter->offset, _filter->count);
120                 return MEDIA_CONTENT_ERROR_NONE;
121         }
122
123         if (_filter->order_type == MEDIA_CONTENT_ORDER_ASC || _filter->order_type == MEDIA_CONTENT_ORDER_DESC) {
124                 *result = g_strdup_printf("ORDER BY %s%s%s LIMIT %d, %d",
125                         _filter->order_keyword,
126                         __get_collate_str(_filter->order_collate_type),
127                         __get_order_str(_filter->order_type),
128                         _filter->offset,
129                         _filter->count);
130         } else {
131                 *result = g_strdup_printf("%s LIMIT %d, %d", _filter->order_keyword, _filter->offset, _filter->count);
132         }
133
134         return MEDIA_CONTENT_ERROR_NONE;
135 }
136
137 int media_filter_create(filter_h *filter)
138 {
139         int ret = MEDIA_CONTENT_ERROR_NONE;
140
141         media_content_retvm_if(filter == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid filter");
142
143         filter_s *_filter = (filter_s *)calloc(1, sizeof(filter_s));
144         media_content_retvm_if(_filter == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
145
146         _filter->storage_id = NULL;
147         _filter->condition = NULL;
148         _filter->order_keyword = NULL;
149         _filter->order_type = -1;
150         _filter->condition_collate_type = MEDIA_CONTENT_COLLATE_DEFAULT;
151         _filter->order_collate_type = MEDIA_CONTENT_COLLATE_DEFAULT;
152         _filter->offset = -1;
153         _filter->count = -1;
154         _filter->is_full_condition = false;
155         _filter->is_full_order = false;
156
157         *filter = (filter_h)_filter;
158
159         return ret;
160 }
161
162 int media_filter_destroy(filter_h filter)
163 {
164         int ret = MEDIA_CONTENT_ERROR_NONE;
165         filter_s *_filter = (filter_s *)filter;
166
167         if (_filter) {
168                 SAFE_FREE(_filter->storage_id);
169                 SAFE_FREE(_filter->condition);
170                 SAFE_FREE(_filter->order_keyword);
171                 SAFE_FREE(_filter);
172
173                 ret = MEDIA_CONTENT_ERROR_NONE;
174         } else {
175                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
176                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
177         }
178
179         return ret;
180 }
181
182 int media_filter_set_offset(filter_h filter, int offset, int count)
183 {
184         int ret = MEDIA_CONTENT_ERROR_NONE;
185         filter_s *_filter = (filter_s *)filter;
186
187         if (_filter != NULL) {
188                 _filter->offset = offset;
189                 _filter->count = count;
190         } else {
191                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
192                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
193         }
194
195         return ret;
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
203         if ((_filter != NULL) && STRING_VALID(condition)
204                 && ((collate_type >= MEDIA_CONTENT_COLLATE_DEFAULT) && (collate_type <= MEDIA_CONTENT_COLLATE_LOCALIZED))) {
205
206                 _filter->is_full_condition = false;
207
208                 if (STRING_VALID(_filter->condition))
209                         SAFE_FREE(_filter->condition);
210
211                 _filter->condition = _media_content_replace_path_in_condition(condition);
212                 media_content_retvm_if(_filter->condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "Fail to _media_content_replace_path_in_condition");
213
214                 media_content_sec_debug("Condition string : %s", _filter->condition);
215
216                 _filter->condition_collate_type = collate_type;
217         } else {
218                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
219                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
220         }
221
222         return ret;
223 }
224
225 int media_filter_set_order(filter_h filter, media_content_order_e order_type, const char *order_keyword, media_content_collation_e collate_type)
226 {
227         int ret = MEDIA_CONTENT_ERROR_NONE;
228         filter_s *_filter = (filter_s *)filter;
229
230         if ((_filter != NULL) && STRING_VALID(order_keyword)
231                 && ((order_type == MEDIA_CONTENT_ORDER_ASC) || (order_type == MEDIA_CONTENT_ORDER_DESC))
232                 && ((collate_type >= MEDIA_CONTENT_COLLATE_DEFAULT) && (collate_type <= MEDIA_CONTENT_COLLATE_LOCALIZED))) {
233
234                 _filter->is_full_order = false;
235
236                 SAFE_FREE(_filter->order_keyword);
237
238                 _filter->order_keyword = strdup(order_keyword);
239                 media_content_retvm_if(_filter->order_keyword == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
240
241                 _filter->order_type = order_type;
242                 _filter->order_collate_type = collate_type;
243         } else {
244                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
245                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
246         }
247
248         return ret;
249 }
250
251 int media_filter_set_storage(filter_h filter, const char *storage_id)
252 {
253         int ret = MEDIA_CONTENT_ERROR_NONE;
254         media_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.");
255         filter_s *_filter = (filter_s *)filter;
256
257         if ((_filter != NULL) && STRING_VALID(storage_id)) {
258                 if (STRING_VALID(_filter->storage_id))
259                         SAFE_FREE(_filter->storage_id);
260
261                 _filter->storage_id = strdup(storage_id);
262                 media_content_retvm_if(_filter->storage_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
263
264                 media_content_sec_debug("storage_id : %s", _filter->storage_id);
265         } else {
266                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
267                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
268         }
269
270         return ret;
271 }
272
273 int media_filter_get_offset(filter_h filter, int *offset, int *count)
274 {
275         int ret = MEDIA_CONTENT_ERROR_NONE;
276         filter_s *_filter = (filter_s *)filter;
277
278         if (_filter) {
279                 *offset = _filter->offset;
280                 *count = _filter->count;
281         } else {
282                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
283                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
284         }
285
286         return ret;
287 }
288
289 int media_filter_get_condition(filter_h filter, char **condition, media_content_collation_e *collate_type)
290 {
291         int ret = MEDIA_CONTENT_ERROR_NONE;
292         filter_s *_filter = (filter_s *)filter;
293
294         if (_filter) {
295                 if (STRING_VALID(_filter->condition) && _filter->is_full_condition == false) {
296                         *condition = strdup(_filter->condition);
297                         media_content_retvm_if(*condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
298                 } else {
299                         *condition = NULL;
300                 }
301
302                 *collate_type = _filter->condition_collate_type;
303         } else {
304                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
305                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
306         }
307
308         return ret;
309 }
310
311 int media_filter_get_order(filter_h filter, media_content_order_e* order_type, char **order_keyword, media_content_collation_e *collate_type)
312 {
313         int ret = MEDIA_CONTENT_ERROR_NONE;
314         filter_s *_filter = (filter_s *)filter;
315
316         if (_filter) {
317                 if (STRING_VALID(_filter->order_keyword) && _filter->is_full_order == false) {
318                         *order_keyword = strdup(_filter->order_keyword);
319                         media_content_retvm_if(*order_keyword == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
320                 } else {
321                         *order_keyword = NULL;
322                 }
323
324                 *order_type = _filter->order_type;
325                 *collate_type = _filter->order_collate_type;
326         } else {
327                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
328                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
329         }
330
331         return ret;
332 }
333
334 int media_filter_get_storage(filter_h filter, char **storage_id)
335 {
336         int ret = MEDIA_CONTENT_ERROR_NONE;
337         media_content_warn("DEPRECATION WARNING: media_filter_get_storage() is deprecated and will be removed from next release.");
338         filter_s *_filter = (filter_s *)filter;
339
340         if (_filter) {
341                 if (STRING_VALID(_filter->storage_id)) {
342                         *storage_id = strdup(_filter->storage_id);
343                         media_content_retvm_if(*storage_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
344                 } else {
345                         *storage_id = NULL;
346                 }
347         } else {
348                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
349                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
350         }
351
352         return ret;
353 }
354
355 int media_filter_set_condition_v2(filter_h filter, const char *condition)
356 {
357         int ret = MEDIA_CONTENT_ERROR_NONE;
358         filter_s *_filter = (filter_s *)filter;
359
360         if ((_filter != NULL) && STRING_VALID(condition)) {
361                 _filter->is_full_condition = true;
362
363                 if (STRING_VALID(_filter->condition))
364                         SAFE_FREE(_filter->condition);
365
366                 _filter->condition = _media_content_replace_path_in_condition(condition);
367                 media_content_retvm_if(_filter->condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "Fail to _media_content_replace_path_in_condition");
368
369                 media_content_sec_debug("Condition string : %s", _filter->condition);
370         } else {
371                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
372                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
373         }
374
375         return ret;
376 }
377
378 int media_filter_get_condition_v2(filter_h filter, char **condition)
379 {
380         int ret = MEDIA_CONTENT_ERROR_NONE;
381         filter_s *_filter = (filter_s *)filter;
382
383         if (_filter) {
384                 if (STRING_VALID(_filter->condition) && _filter->is_full_condition == true) {
385                         *condition = strdup(_filter->condition);
386                         media_content_retvm_if(*condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
387                 } else {
388                         *condition = NULL;
389                 }
390         } else {
391                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
392                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
393         }
394
395         return ret;
396 }
397
398 int media_filter_set_order_v2(filter_h filter, const char *order)
399 {
400         int ret = MEDIA_CONTENT_ERROR_NONE;
401         filter_s *_filter = (filter_s *)filter;
402
403         if ((_filter != NULL) && STRING_VALID(order)) {
404                 _filter->is_full_order = true;
405
406                 SAFE_FREE(_filter->order_keyword);
407
408                 _filter->order_keyword = strdup(order);
409                 media_content_retvm_if(_filter->order_keyword == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
410         } else {
411                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
412                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
413         }
414
415         return ret;
416 }
417
418 int media_filter_get_order_v2(filter_h filter, char **order)
419 {
420         int ret = MEDIA_CONTENT_ERROR_NONE;
421         filter_s *_filter = (filter_s *)filter;
422
423         if (_filter) {
424                 if (STRING_VALID(_filter->order_keyword) && _filter->is_full_order == true) {
425                         *order = strdup(_filter->order_keyword);
426                         media_content_retvm_if(*order == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
427                 } else {
428                         *order = NULL;
429                 }
430         } else {
431                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
432                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
433         }
434
435         return ret;
436 }
437