Resurrect removed APIs
[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 char *__get_order_str(media_content_order_e order_enum)
49 {
50         switch (order_enum) {
51         case MEDIA_CONTENT_ORDER_ASC:
52                 return (char *)"ASC ";
53         case MEDIA_CONTENT_ORDER_DESC:
54                 return (char *)"DESC ";
55         default:
56                 return (char *)" ";
57         }
58 }
59
60 static char *__get_collate_str(media_content_collation_e collate_type)
61 {
62         switch (collate_type) {
63         case MEDIA_CONTENT_COLLATE_NOCASE:
64                 return (char *)"NOCASE ";
65         case MEDIA_CONTENT_COLLATE_RTRIM:
66                 return (char *)"RTRIM ";
67         case MEDIA_CONTENT_COLLATE_LOCALIZED:
68                 if (__is_pinyin_needed())
69                         return (char *)"NOCASE ";
70                 else
71                         return (char *)"localized ";
72         default: return (char *)" ";
73         }
74 }
75
76 int _media_filter_attribute_generate(filter_h filter, char **generated_condition)
77 {
78         int ret = MEDIA_CONTENT_ERROR_NONE;
79         filter_s *_filter = NULL;
80         char tmp_condition[MAX_QUERY_SIZE] = {0, };
81
82         media_content_retvm_if(filter == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid filter");
83         media_content_retvm_if(generated_condition == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid generated_condition");
84
85         _filter = (filter_s*)filter;
86
87         media_content_retvm_if(_filter->condition == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid condition");
88         media_content_retvm_if(_filter->condition_collate_type < MEDIA_CONTENT_COLLATE_DEFAULT ||
89                 _filter->condition_collate_type > MEDIA_CONTENT_COLLATE_LOCALIZED, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid condition collate");
90
91         /* make the statment */
92         memset(tmp_condition, 0, sizeof(tmp_condition));
93         SAFE_STRLCAT(tmp_condition, QUERY_KEYWORD_OPEN_BRACKET, sizeof(tmp_condition));
94         SAFE_STRLCAT(tmp_condition, _filter->condition, sizeof(tmp_condition));
95
96         /* Process for filter v1 */
97         if (_filter->is_full_condition == false && _filter->condition_collate_type != MEDIA_CONTENT_COLLATE_DEFAULT) {
98                 SAFE_STRLCAT(tmp_condition, QUERY_KEYWORD_COLLATE, sizeof(tmp_condition));
99                 SAFE_STRLCAT(tmp_condition, __get_collate_str(_filter->condition_collate_type), sizeof(tmp_condition));
100         }
101
102         SAFE_STRLCAT(tmp_condition, QUERY_KEYWORD_BRACKET, sizeof(tmp_condition));
103
104         if (STRING_VALID(tmp_condition))
105                 *generated_condition = g_strdup(tmp_condition);
106         else
107                 *generated_condition = NULL;
108
109         media_content_sec_debug("Condition : %s", *generated_condition);
110
111         return ret;
112 }
113
114 int _media_filter_attribute_option_generate(filter_h filter, char **generated_option)
115 {
116         int ret = MEDIA_CONTENT_ERROR_NONE;
117         filter_s *_filter = NULL;
118         char query[DEFAULT_QUERY_SIZE] = {0, };
119         char option[DEFAULT_QUERY_SIZE] = {0, };
120
121         media_content_retvm_if(filter == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid filter");
122
123         _filter = (filter_s*)filter;
124
125         memset(query, 0x00, sizeof(query));
126
127         /* Order by*/
128         if (STRING_VALID(_filter->order_keyword)) {
129                 if ((_filter->order_type == MEDIA_CONTENT_ORDER_ASC) || (_filter->order_type == MEDIA_CONTENT_ORDER_DESC)) {
130                         SAFE_STRLCAT(query, QUERY_KEYWORD_ORDER_BY, sizeof(query));
131                         SAFE_STRLCAT(query, _filter->order_keyword, sizeof(query));
132
133                         if (_filter->order_collate_type == MEDIA_CONTENT_COLLATE_NOCASE || _filter->order_collate_type == MEDIA_CONTENT_COLLATE_RTRIM || _filter->order_collate_type == MEDIA_CONTENT_COLLATE_LOCALIZED) {
134                                 SAFE_STRLCAT(query, QUERY_KEYWORD_COLLATE, sizeof(query));
135                                 SAFE_STRLCAT(query, __get_collate_str(_filter->order_collate_type), sizeof(query));
136                         } else {
137                                 SAFE_STRLCAT(query, QUERY_KEYWORD_SPACE, sizeof(query));
138                         }
139                         SAFE_STRLCAT(query, __get_order_str(_filter->order_type), sizeof(query));
140                 } else {
141                         SAFE_STRLCAT(query, _filter->order_keyword, sizeof(query));
142                 }
143         }
144
145         /* offset */
146         SAFE_STRLCAT(query, QUERY_KEYWORD_SPACE, sizeof(query));
147
148         memset(option, 0, sizeof(option));
149         snprintf(option, sizeof(option), "%s %d, %d", QUERY_KEYWORD_LIMIT, _filter->offset, _filter->count);
150
151         SAFE_STRLCAT(query, option, sizeof(query));
152
153         if (STRING_VALID(query)) {
154                 *generated_option = g_strdup(query);
155                 media_content_sec_debug("Option : %s", *generated_option);
156         } else {
157                 *generated_option = NULL;
158         }
159
160         return ret;
161 }
162
163 int _media_filter_attribute_option_generate_with_full_query(filter_h filter, char **generated_option)
164 {
165         int ret = MEDIA_CONTENT_ERROR_NONE;
166         filter_s * _filter = NULL;
167         char query[DEFAULT_QUERY_SIZE] = {0, };
168         char option[DEFAULT_QUERY_SIZE] = {0, };
169
170         media_content_retvm_if(filter == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid filter");
171         media_content_retvm_if(generated_option == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid generated_option");
172
173         _filter = (filter_s*)filter;
174
175         memset(query, 0, sizeof(query));
176
177         /* Order by*/
178         if (STRING_VALID(_filter->order_keyword)) {
179                 SAFE_STRLCAT(query, QUERY_KEYWORD_ORDER_BY, sizeof(query));
180                 SAFE_STRLCAT(query, _filter->order_keyword, sizeof(query));
181         }
182
183         /* offset */
184         SAFE_STRLCAT(query, QUERY_KEYWORD_SPACE, sizeof(query));
185
186         memset(option, 0, sizeof(option));
187         snprintf(option, sizeof(option), "%s %d, %d", QUERY_KEYWORD_LIMIT, _filter->offset, _filter->count);
188         SAFE_STRLCAT(query, option, sizeof(query));
189
190         if (STRING_VALID(query)) {
191                 *generated_option = g_strdup(query);
192                 media_content_sec_debug("Option : %s", *generated_option);
193         } else {
194                 *generated_option = NULL;
195         }
196
197         return ret;
198 }
199
200
201 int media_filter_create(filter_h *filter)
202 {
203         int ret = MEDIA_CONTENT_ERROR_NONE;
204
205         media_content_retvm_if(filter == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid filter");
206
207         filter_s *_filter = (filter_s*)calloc(1, sizeof(filter_s));
208         media_content_retvm_if(_filter == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
209
210         _filter->storage_id = NULL;
211         _filter->condition = NULL;
212         _filter->order_keyword = NULL;
213         _filter->order_type = -1;
214         _filter->condition_collate_type = MEDIA_CONTENT_COLLATE_DEFAULT;
215         _filter->order_collate_type = MEDIA_CONTENT_COLLATE_DEFAULT;
216         _filter->offset = -1;
217         _filter->count = -1;
218         _filter->is_full_condition = false;
219         _filter->is_full_order = false;
220
221         *filter = (filter_h)_filter;
222
223         return ret;
224 }
225
226 int media_filter_destroy(filter_h filter)
227 {
228         int ret = MEDIA_CONTENT_ERROR_NONE;
229         filter_s *_filter = (filter_s*)filter;
230
231         if (_filter) {
232                 SAFE_FREE(_filter->storage_id);
233                 SAFE_FREE(_filter->condition);
234                 SAFE_FREE(_filter->order_keyword);
235                 SAFE_FREE(_filter);
236
237                 ret = MEDIA_CONTENT_ERROR_NONE;
238         } else {
239                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
240                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
241         }
242
243         return ret;
244 }
245
246 int media_filter_set_offset(filter_h filter, int offset, int count)
247 {
248         int ret = MEDIA_CONTENT_ERROR_NONE;
249         filter_s *_filter = (filter_s*)filter;
250
251         if (_filter != NULL) {
252                 _filter->offset = offset;
253                 _filter->count = count;
254         } else {
255                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
256                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
257         }
258
259         return ret;
260 }
261
262 int media_filter_set_condition(filter_h filter, const char *condition, media_content_collation_e collate_type)
263 {
264         int ret = MEDIA_CONTENT_ERROR_NONE;
265         filter_s *_filter = (filter_s*)filter;
266
267         if ((_filter != NULL) && STRING_VALID(condition)
268                 && ((collate_type >= MEDIA_CONTENT_COLLATE_DEFAULT) && (collate_type <= MEDIA_CONTENT_COLLATE_LOCALIZED))) {
269
270                 _filter->is_full_condition = false;
271
272                 if (STRING_VALID(_filter->condition))
273                         SAFE_FREE(_filter->condition);
274
275                 char new_condition[MAX_QUERY_SIZE] = {0, };
276                 memset(new_condition, 0, sizeof(new_condition));
277                 ret = _media_content_replace_path_in_condition(condition, new_condition, TRUE);
278                 media_content_retvm_if(!STRING_VALID(new_condition), MEDIA_CONTENT_ERROR_INVALID_OPERATION, "path replacement failed");
279
280                 _filter->condition = strdup(new_condition);
281                 media_content_retvm_if(_filter->condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
282
283                 media_content_sec_debug("Condition string : %s", _filter->condition);
284
285                 _filter->condition_collate_type = collate_type;
286         } else {
287                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
288                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
289         }
290
291         return ret;
292 }
293
294 int media_filter_set_order(filter_h filter, media_content_order_e order_type, const char *order_keyword, media_content_collation_e collate_type)
295 {
296         int ret = MEDIA_CONTENT_ERROR_NONE;
297         filter_s *_filter = (filter_s*)filter;
298
299         if ((_filter != NULL) && STRING_VALID(order_keyword)
300                 && ((order_type == MEDIA_CONTENT_ORDER_ASC) || (order_type == MEDIA_CONTENT_ORDER_DESC))
301                 && ((collate_type >= MEDIA_CONTENT_COLLATE_DEFAULT) && (collate_type <= MEDIA_CONTENT_COLLATE_LOCALIZED))) {
302
303                 _filter->is_full_order = false;
304
305                 SAFE_FREE(_filter->order_keyword);
306
307                 _filter->order_keyword = strdup(order_keyword);
308                 media_content_retvm_if(_filter->order_keyword == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
309
310                 _filter->order_type = order_type;
311                 _filter->order_collate_type = collate_type;
312         } else {
313                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
314                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
315         }
316
317         return ret;
318 }
319
320 int media_filter_set_storage(filter_h filter, const char *storage_id)
321 {
322         int ret = MEDIA_CONTENT_ERROR_NONE;
323         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.");
324         filter_s *_filter = (filter_s*)filter;
325
326         if ((_filter != NULL) && STRING_VALID(storage_id)) {
327                 if (STRING_VALID(_filter->storage_id))
328                         SAFE_FREE(_filter->storage_id);
329
330                 _filter->storage_id = strdup(storage_id);
331                 media_content_retvm_if(_filter->storage_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
332
333                 media_content_sec_debug("storage_id : %s", _filter->storage_id);
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_offset(filter_h filter, int *offset, int *count)
343 {
344         int ret = MEDIA_CONTENT_ERROR_NONE;
345         filter_s *_filter = (filter_s*)filter;
346
347         if (_filter) {
348                 *offset = _filter->offset;
349                 *count = _filter->count;
350         } else {
351                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
352                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
353         }
354
355         return ret;
356 }
357
358 int media_filter_get_condition(filter_h filter, char **condition, media_content_collation_e *collate_type)
359 {
360         int ret = MEDIA_CONTENT_ERROR_NONE;
361         filter_s *_filter = (filter_s*)filter;
362
363         if (_filter) {
364                 if (STRING_VALID(_filter->condition) && _filter->is_full_condition == false) {
365                         char new_condition[MAX_QUERY_SIZE] = {0, };
366                         memset(new_condition, 0, sizeof(new_condition));
367                         ret = _media_content_replace_path_in_condition(_filter->condition, new_condition, FALSE);
368                         media_content_retvm_if(!STRING_VALID(new_condition), MEDIA_CONTENT_ERROR_INVALID_OPERATION, "path replacement failed");
369
370                         *condition = strdup(new_condition);
371                         media_content_retvm_if(*condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
372                 } else {
373                         *condition = NULL;
374                 }
375
376                 *collate_type = _filter->condition_collate_type;
377         } else {
378                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
379                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
380         }
381
382         return ret;
383 }
384
385 int media_filter_get_order(filter_h filter, media_content_order_e* order_type, char **order_keyword, media_content_collation_e *collate_type)
386 {
387         int ret = MEDIA_CONTENT_ERROR_NONE;
388         filter_s *_filter = (filter_s*)filter;
389
390         if (_filter) {
391                 if (STRING_VALID(_filter->order_keyword) && _filter->is_full_order == false) {
392                         *order_keyword = strdup(_filter->order_keyword);
393                         media_content_retvm_if(*order_keyword == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
394                 } else {
395                         *order_keyword = NULL;
396                 }
397
398                 *order_type = _filter->order_type;
399                 *collate_type = _filter->order_collate_type;
400         } else {
401                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
402                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
403         }
404
405         return ret;
406 }
407
408 int media_filter_get_storage(filter_h filter, char **storage_id)
409 {
410         int ret = MEDIA_CONTENT_ERROR_NONE;
411         media_content_warn("DEPRECATION WARNING: media_filter_get_storage() is deprecated and will be removed from next release.");
412         filter_s *_filter = (filter_s*)filter;
413
414         if (_filter) {
415                 if (STRING_VALID(_filter->storage_id)) {
416                         *storage_id = strdup(_filter->storage_id);
417                         media_content_retvm_if(*storage_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
418                 } else {
419                         *storage_id = NULL;
420                 }
421         } else {
422                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
423                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
424         }
425
426         return ret;
427 }
428
429 int media_filter_set_condition_v2(filter_h filter, const char *condition)
430 {
431         int ret = MEDIA_CONTENT_ERROR_NONE;
432         filter_s *_filter = (filter_s*)filter;
433
434         if ((_filter != NULL) && STRING_VALID(condition)) {
435                 _filter->is_full_condition = true;
436
437                 if (STRING_VALID(_filter->condition))
438                         SAFE_FREE(_filter->condition);
439
440                 char new_condition[MAX_QUERY_SIZE] = {0, };
441                 memset(new_condition, 0, sizeof(new_condition));
442                 ret = _media_content_replace_path_in_condition(condition, new_condition, TRUE);
443                 media_content_retvm_if(!STRING_VALID(new_condition), MEDIA_CONTENT_ERROR_INVALID_OPERATION, "path replacement failed");
444
445                 _filter->condition = strdup(new_condition);
446                 media_content_retvm_if(_filter->condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
447
448                 media_content_sec_debug("Condition string : %s", _filter->condition);
449         } else {
450                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
451                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
452         }
453
454         return ret;
455 }
456
457 int media_filter_get_condition_v2(filter_h filter, char **condition)
458 {
459         int ret = MEDIA_CONTENT_ERROR_NONE;
460         filter_s *_filter = (filter_s*)filter;
461
462         if (_filter) {
463                 if (STRING_VALID(_filter->condition) && _filter->is_full_condition == true) {
464                         char new_condition[MAX_QUERY_SIZE] = {0, };
465                         memset(new_condition, 0, sizeof(new_condition));
466                         ret = _media_content_replace_path_in_condition(_filter->condition, new_condition, FALSE);
467                         media_content_retvm_if(!STRING_VALID(new_condition), MEDIA_CONTENT_ERROR_INVALID_OPERATION, "path replacement failed");
468
469                         *condition = strdup(new_condition);
470                         media_content_retvm_if(*condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
471                 } else {
472                         *condition = NULL;
473                 }
474         } else {
475                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
476                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
477         }
478
479         return ret;
480 }
481
482 int media_filter_set_order_v2(filter_h filter, const char *order)
483 {
484         int ret = MEDIA_CONTENT_ERROR_NONE;
485         filter_s *_filter = (filter_s*)filter;
486
487         if ((_filter != NULL) && STRING_VALID(order)) {
488                 _filter->is_full_order = true;
489
490                 SAFE_FREE(_filter->order_keyword);
491
492                 _filter->order_keyword = strdup(order);
493                 media_content_retvm_if(_filter->order_keyword == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
494         } else {
495                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
496                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
497         }
498
499         return ret;
500 }
501
502 int media_filter_get_order_v2(filter_h filter, char **order)
503 {
504         int ret = MEDIA_CONTENT_ERROR_NONE;
505         filter_s *_filter = (filter_s*)filter;
506
507         if (_filter) {
508                 if (STRING_VALID(_filter->order_keyword) && _filter->is_full_order == true) {
509                         *order = strdup(_filter->order_keyword);
510                         media_content_retvm_if(*order == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
511                 } else {
512                         *order = NULL;
513                 }
514         } else {
515                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
516                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
517         }
518
519         return ret;
520 }
521