Improve readability of media_bookmark
[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
213                 /* FIXME
214                         If an error is occured in _media_content_replace_path_in_condition(),
215                         A suitable return value is 'MEDIA_CONTENT_ERROR_INVALID_OPERATION'.
216                         However, it is not stated in the description of media_filter_set_condition().
217                         Therefore, use 'MEDIA_CONTENT_ERROR_OUT_OF_MEMORY' temporarily.
218                         It will be modified after removing _media_content_replace_path_in_condition() function.
219                 */
220                 media_content_retvm_if(_filter->condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "Fail to _media_content_replace_path_in_condition");
221
222                 media_content_sec_debug("Condition string : %s", _filter->condition);
223
224                 _filter->condition_collate_type = collate_type;
225         } else {
226                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
227                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
228         }
229
230         return ret;
231 }
232
233 int media_filter_set_order(filter_h filter, media_content_order_e order_type, const char *order_keyword, media_content_collation_e collate_type)
234 {
235         int ret = MEDIA_CONTENT_ERROR_NONE;
236         filter_s *_filter = (filter_s *)filter;
237
238         if ((_filter != NULL) && STRING_VALID(order_keyword)
239                 && ((order_type == MEDIA_CONTENT_ORDER_ASC) || (order_type == MEDIA_CONTENT_ORDER_DESC))
240                 && ((collate_type >= MEDIA_CONTENT_COLLATE_DEFAULT) && (collate_type <= MEDIA_CONTENT_COLLATE_LOCALIZED))) {
241
242                 _filter->is_full_order = false;
243
244                 SAFE_FREE(_filter->order_keyword);
245
246                 _filter->order_keyword = strdup(order_keyword);
247                 media_content_retvm_if(_filter->order_keyword == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
248
249                 _filter->order_type = order_type;
250                 _filter->order_collate_type = collate_type;
251         } else {
252                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
253                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
254         }
255
256         return ret;
257 }
258
259 int media_filter_set_storage(filter_h filter, const char *storage_id)
260 {
261         int ret = MEDIA_CONTENT_ERROR_NONE;
262         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.");
263         filter_s *_filter = (filter_s *)filter;
264
265         if ((_filter != NULL) && STRING_VALID(storage_id)) {
266                 if (STRING_VALID(_filter->storage_id))
267                         SAFE_FREE(_filter->storage_id);
268
269                 _filter->storage_id = strdup(storage_id);
270                 media_content_retvm_if(_filter->storage_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
271
272                 media_content_sec_debug("storage_id : %s", _filter->storage_id);
273         } else {
274                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
275                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
276         }
277
278         return ret;
279 }
280
281 int media_filter_get_offset(filter_h filter, int *offset, int *count)
282 {
283         int ret = MEDIA_CONTENT_ERROR_NONE;
284         filter_s *_filter = (filter_s *)filter;
285
286         if (_filter) {
287                 *offset = _filter->offset;
288                 *count = _filter->count;
289         } else {
290                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
291                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
292         }
293
294         return ret;
295 }
296
297 int media_filter_get_condition(filter_h filter, char **condition, media_content_collation_e *collate_type)
298 {
299         int ret = MEDIA_CONTENT_ERROR_NONE;
300         filter_s *_filter = (filter_s *)filter;
301
302         if (_filter) {
303                 if (STRING_VALID(_filter->condition) && _filter->is_full_condition == false) {
304                         *condition = strdup(_filter->condition);
305                         media_content_retvm_if(*condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
306                 } else {
307                         *condition = NULL;
308                 }
309
310                 *collate_type = _filter->condition_collate_type;
311         } else {
312                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
313                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
314         }
315
316         return ret;
317 }
318
319 int media_filter_get_order(filter_h filter, media_content_order_e* order_type, char **order_keyword, media_content_collation_e *collate_type)
320 {
321         int ret = MEDIA_CONTENT_ERROR_NONE;
322         filter_s *_filter = (filter_s *)filter;
323
324         if (_filter) {
325                 if (STRING_VALID(_filter->order_keyword) && _filter->is_full_order == false) {
326                         *order_keyword = strdup(_filter->order_keyword);
327                         media_content_retvm_if(*order_keyword == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
328                 } else {
329                         *order_keyword = NULL;
330                 }
331
332                 *order_type = _filter->order_type;
333                 *collate_type = _filter->order_collate_type;
334         } else {
335                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
336                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
337         }
338
339         return ret;
340 }
341
342 int media_filter_get_storage(filter_h filter, char **storage_id)
343 {
344         int ret = MEDIA_CONTENT_ERROR_NONE;
345         media_content_warn("DEPRECATION WARNING: media_filter_get_storage() is deprecated and will be removed from next release.");
346         filter_s *_filter = (filter_s *)filter;
347
348         if (_filter) {
349                 if (STRING_VALID(_filter->storage_id)) {
350                         *storage_id = strdup(_filter->storage_id);
351                         media_content_retvm_if(*storage_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
352                 } else {
353                         *storage_id = NULL;
354                 }
355         } else {
356                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
357                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
358         }
359
360         return ret;
361 }
362
363 int media_filter_set_condition_v2(filter_h filter, const char *condition)
364 {
365         int ret = MEDIA_CONTENT_ERROR_NONE;
366         filter_s *_filter = (filter_s *)filter;
367
368         if ((_filter != NULL) && STRING_VALID(condition)) {
369                 _filter->is_full_condition = true;
370
371                 if (STRING_VALID(_filter->condition))
372                         SAFE_FREE(_filter->condition);
373
374                 /* FIXME
375                         If an error is occured in _media_content_replace_path_in_condition(),
376                         A suitable return value is 'MEDIA_CONTENT_ERROR_INVALID_OPERATION'.
377                         However, it is not stated in the description of media_filter_set_condition().
378                         Therefore, use 'MEDIA_CONTENT_ERROR_OUT_OF_MEMORY' temporarily.
379                         It will be modified after removing _media_content_replace_path_in_condition() function.
380                 */
381                 _filter->condition = _media_content_replace_path_in_condition(condition);
382                 media_content_retvm_if(_filter->condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "Fail to _media_content_replace_path_in_condition");
383
384                 media_content_sec_debug("Condition string : %s", _filter->condition);
385         } else {
386                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
387                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
388         }
389
390         return ret;
391 }
392
393 int media_filter_get_condition_v2(filter_h filter, char **condition)
394 {
395         int ret = MEDIA_CONTENT_ERROR_NONE;
396         filter_s *_filter = (filter_s *)filter;
397
398         if (_filter) {
399                 if (STRING_VALID(_filter->condition) && _filter->is_full_condition == true) {
400                         *condition = strdup(_filter->condition);
401                         media_content_retvm_if(*condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
402                 } else {
403                         *condition = NULL;
404                 }
405         } else {
406                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
407                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
408         }
409
410         return ret;
411 }
412
413 int media_filter_set_order_v2(filter_h filter, const char *order)
414 {
415         int ret = MEDIA_CONTENT_ERROR_NONE;
416         filter_s *_filter = (filter_s *)filter;
417
418         if ((_filter != NULL) && STRING_VALID(order)) {
419                 _filter->is_full_order = true;
420
421                 SAFE_FREE(_filter->order_keyword);
422
423                 _filter->order_keyword = strdup(order);
424                 media_content_retvm_if(_filter->order_keyword == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
425         } else {
426                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
427                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
428         }
429
430         return ret;
431 }
432
433 int media_filter_get_order_v2(filter_h filter, char **order)
434 {
435         int ret = MEDIA_CONTENT_ERROR_NONE;
436         filter_s *_filter = (filter_s *)filter;
437
438         if (_filter) {
439                 if (STRING_VALID(_filter->order_keyword) && _filter->is_full_order == true) {
440                         *order = strdup(_filter->order_keyword);
441                         media_content_retvm_if(*order == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
442                 } else {
443                         *order = NULL;
444                 }
445         } else {
446                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
447                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
448         }
449
450         return ret;
451 }
452