2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <media_info_private.h>
21 static const char *media_token[] = {
36 typedef struct _token_t {
42 #define MAX_LEFT_VALUE 512
45 #define UNKNOWN_TYPE 1000
46 #define STRING_TYPE 100
48 static char *__get_order_str(media_content_order_e order_enum);
49 static char *__get_collate_str(media_content_collation_e collate_type);
50 static void __filter_attribute_free_value(gpointer key, gpointer value, gpointer user_data);
51 static char *__media_filter_replace_attr(attribute_h attr, char *name);
52 static int __tokenize_operator(token_t *token, const char *str, int op_type);
53 static int __tokenize(GList **token_list, const char *str);
55 static bool __is_pinyin_needed(void)
58 const char *china = "zh_CN";
59 const char *hongkong = "zh_HK";
63 bool pinyin_support = FALSE;
64 media_svc_check_pinyin_support(&pinyin_support);
66 /*Check Language Setting*/
67 lang = vconf_get_str(VCONFKEY_LANGSET);
68 media_content_retvm_if(lang == NULL, ret, "Fail to get string of language set");
70 if ((strncmp(china, lang, strlen(china)) == 0) ||
71 (strncmp(hongkong, lang, strlen(hongkong)) == 0)) {
81 static char *__get_order_str(media_content_order_e order_enum)
84 case MEDIA_CONTENT_ORDER_ASC:
86 case MEDIA_CONTENT_ORDER_DESC:
87 return (char *)"DESC";
93 static char *__get_collate_str(media_content_collation_e collate_type)
95 switch (collate_type) {
96 case MEDIA_CONTENT_COLLATE_NOCASE:
97 return (char *)"NOCASE";
98 case MEDIA_CONTENT_COLLATE_RTRIM:
99 return (char *)"RTRIM";
100 case MEDIA_CONTENT_COLLATE_LOCALIZED:
101 if (__is_pinyin_needed())
102 return (char *)"NOCASE";
104 return (char *)"localized";
105 default: return (char *)" ";
109 static void __filter_attribute_free_value(gpointer key, gpointer value, gpointer user_data)
115 static char *__media_filter_replace_attr(attribute_h attr, char *name)
117 char *key_temp = NULL;
118 char *generated_value = NULL;
119 attribute_s *_attr = (attribute_s *)attr;
121 if (!g_hash_table_lookup_extended(_attr->attr_map, name, (gpointer)&key_temp, (gpointer)&generated_value)) {
122 /* can't find the value */
123 /* media_content_error("NOT_FOUND_VALUE(%s)", name); */
127 if (STRING_VALID(generated_value))
128 return strdup(generated_value);
130 media_content_error("__media_filter_replace_attr fail");
135 static int __tokenize_operator(token_t *token, const char *str, int op_type)
138 const char *tmp = str;
140 if (token != NULL && STRING_VALID(tmp)) {
141 token->type = op_type;
142 int token_size = strlen(media_token[op_type]);
143 media_content_retvm_if(token_size == 0, -1, "Invalid token_size. op_type[%d]", op_type);
145 token->str = (char*)calloc(token_size+1, sizeof(char));
146 media_content_retvm_if(token->str == NULL, -1, "OUT_OF_MEMORY");
148 strncpy(token->str, tmp, token_size);
149 /*media_content_debug("type : [%d] str : [%s]", token->type, token->str);*/
158 static int __tokenize_string(token_t *token, const char *str, int size)
161 const char *tmp = str;
163 if (token != NULL && STRING_VALID(tmp) && size > 0) {
164 token->str = (char*)calloc(size+1, sizeof(char));
165 media_content_retvm_if(token->str == NULL, -1, "OUT_OF_MEMORY");
167 token->type = UNKNOWN_TYPE;
168 strncpy(token->str, tmp, size);
169 /*media_content_debug("type : [%d] str : [%s]", token->type, token->str);*/
177 static int __tokenize_attribute(GList **token_list, const char *str)
182 media_content_retvm_if(!STRING_VALID(str), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Parameter string is invalid");
184 const char *tmp = str;
185 const char *dst_ptr = str + strlen(str);
187 for (idx = 0; (*(tmp+idx)) && (tmp < dst_ptr); idx++) {
188 /*media_content_debug("[%d] '%c'", idx, tmp[idx]);*/
189 if (tmp[idx] == ' ') { /* " " */
190 if (idx == 0) { /* ignore the space. */
195 token_t *token = (token_t*)calloc(1, sizeof(token_t));
196 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
198 token->type = UNKNOWN_TYPE;
199 token->str = (char*)calloc(idx+1, sizeof(char));
200 if (token->str == NULL) {
202 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
205 strncpy(token->str, tmp, idx);
206 /*media_content_debug("type : [%d] str : [%s]", token->type, token->str);*/
207 *token_list = g_list_append(*token_list, token);
208 tmp = tmp +idx + strlen(media_token[0]);
210 } else if (tmp[idx] == ',') { /* " , " */
212 token_t *token = (token_t*)calloc(1, sizeof(token_t));
213 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
215 ret = __tokenize_string(token, tmp, idx);
218 media_content_error("tokenize error occured");
221 *token_list = g_list_append(*token_list, token);
226 token_t *token = (token_t*)calloc(1, sizeof(token_t));
227 int size = __tokenize_operator(token, tmp, 3);
229 if (token != NULL && STRING_VALID(token->str)) {
230 *token_list = g_list_append(*token_list, token);
235 media_content_error("tokenize error occured");
241 if (*tmp) { /* remained string */
242 token_t *token = (token_t*)calloc(1, sizeof(token_t));
243 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
245 ret = __tokenize_string(token, tmp, idx);
248 media_content_error("tokenize error occured");
252 if (token != NULL && STRING_VALID(token->str)) {
253 *token_list = g_list_append(*token_list, token);
256 media_content_error("tokenize error occured");
261 return MEDIA_CONTENT_ERROR_NONE;
264 static int __tokenize(GList **token_list, const char *str)
269 media_content_retvm_if(!STRING_VALID(str), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Parameter string is invalid");
271 const char *tmp = str;
272 const char *dst_ptr = str + strlen(str);
274 for (idx = 0; (*(tmp+idx)) && (tmp < dst_ptr); idx++) {
275 /* media_content_debug("[%d] '%c'", idx, tmp[idx]); */
276 if (tmp[idx] == media_token[0][0]) { /* " " */
277 if (idx == 0) { /* ignore the space. */
283 token_t *token = (token_t*)calloc(1, sizeof(token_t));
284 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
286 token->type = UNKNOWN_TYPE;
287 token->str = (char *)calloc(idx+1, sizeof(char));
288 if (token->str == NULL) {
290 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
293 strncpy(token->str, tmp, idx);
294 /* media_content_debug("type : [%d] str : [%s]", token->type, token->str); */
295 *token_list = g_list_append(*token_list, token);
296 tmp = tmp +idx + strlen(media_token[0]);
298 } else if (tmp[idx] == media_token[1][0]) { /* " \" " */
301 for (j = idx+1; tmp[j]; j++) { /* find next quotation */
302 if (tmp[j] == media_token[1][0] && tmp[j+1] == media_token[1][0]) {
306 if (tmp[j] == media_token[1][0]) {
307 token_t *token = (token_t*)calloc(1, sizeof(token_t));
308 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
310 token->str = (char*) calloc(j+1+1, sizeof(char));
311 if (token->str == NULL) {
313 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
316 token->type = STRING_TYPE;
317 strncpy(token->str, tmp, j+1);
318 /* media_content_debug("type : [%d] str : [%s], j : %d", token->type, token->str, j); */
319 *token_list = g_list_append(*token_list, token);
320 tmp = tmp + strlen(token->str);
327 if (!flag && *tmp != '\0' && tmp[j] == '\0') {
328 token_t *token = (token_t*)calloc(1, sizeof(token_t));
329 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
331 token->str = (char *) calloc(j+1, sizeof(char));
332 if (token->str == NULL) {
334 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
337 token->type = UNKNOWN_TYPE;
338 strncpy(token->str, tmp, j);
339 /* media_content_debug("type : [%d] str : [%s]", token->type, token->str);*/
340 *token_list = g_list_append(*token_list, token);
341 tmp = tmp +strlen(token->str);
344 } else if (tmp[idx] == media_token[2][0]) { /* " \' "*/
347 for (j = idx+1; tmp[j]; j++) {
348 if (tmp[j] == media_token[2][0] && tmp[j+1] == media_token[2][0]) {
352 if (tmp[j] == media_token[2][0]) {
353 token_t *token = (token_t*)calloc(1, sizeof(token_t));
354 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
356 token->str = (char *) calloc(j+1+1, sizeof(char));
357 if (token->str == NULL) {
359 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
362 token->type = STRING_TYPE;
363 strncpy(token->str, tmp, j+1);
364 /* media_content_debug("type : [%d] str : [%s]", token->type, token->str); */
365 *token_list = g_list_append(*token_list, token);
366 tmp = tmp + strlen(token->str);
373 if (!flag && *tmp != '\0' && tmp[j] == '\0') {
374 token_t *token = (token_t*)calloc(1, sizeof(token_t));
375 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
377 token->str = (char*) calloc(j+1, sizeof(char));
378 if (token->str == NULL) {
380 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
383 token->type = UNKNOWN_TYPE;
384 strncpy(token->str, tmp, j);
385 /* media_content_debug("type : [%d] str : [%s]", token->type, token->str); */
386 *token_list = g_list_append(*token_list, token);
387 tmp = tmp + strlen(token->str);
390 } else if (tmp[idx] == media_token[3][0]) { /* "(" */
392 token_t *token = (token_t*)calloc(1, sizeof(token_t));
393 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
395 ret = __tokenize_string(token, tmp, idx);
398 media_content_error("tokenize error occured");
401 *token_list = g_list_append(*token_list, token);
405 token_t *token = (token_t*)calloc(1, sizeof(token_t));
406 int size = __tokenize_operator(token, tmp, 3);
408 if (token != NULL && STRING_VALID(token->str)) {
409 *token_list = g_list_append(*token_list, token);
414 media_content_error("tokenize error occured");
418 } else if (tmp[idx] == media_token[4][0]) { /* ")" */
420 token_t *token = (token_t*)calloc(1, sizeof(token_t));
421 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
423 ret = __tokenize_string(token, tmp, idx);
426 media_content_error("tokenize error occured");
429 *token_list = g_list_append(*token_list, token);
433 token_t *token = (token_t*)calloc(1, sizeof(token_t));
434 int size = __tokenize_operator(token, tmp, 4);
436 if (token != NULL && STRING_VALID(token->str)) {
437 *token_list = g_list_append(*token_list, token);
442 media_content_error("tokenize error occured");
446 } else if (tmp[idx] == media_token[5][0]) { /* "=" */
448 token_t *token = (token_t*)calloc(1, sizeof(token_t));
449 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
451 ret = __tokenize_string(token, tmp, idx);
454 media_content_error("tokenize error occured");
457 *token_list = g_list_append(*token_list, token);
461 token_t *token = (token_t*)calloc(1, sizeof(token_t));
462 int size = __tokenize_operator(token, tmp, 5);
464 if (token != NULL && STRING_VALID(token->str)) {
465 *token_list = g_list_append(*token_list, token);
470 media_content_error("tokenize error occured");
474 } else if (tmp[idx] == media_token[6][0] && tmp[idx+1] == media_token[6][1]) { /* "<=", */
476 token_t *token = (token_t*)calloc(1, sizeof(token_t));
477 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
479 ret = __tokenize_string(token, tmp, idx);
482 media_content_error("tokenize error occured");
485 *token_list = g_list_append(*token_list, token);
489 token_t *token = (token_t*)calloc(1, sizeof(token_t));
490 int size = __tokenize_operator(token, tmp, 6);
492 if (token != NULL && STRING_VALID(token->str)) {
493 *token_list = g_list_append(*token_list, token);
498 media_content_error("tokenize error occured");
502 } else if (tmp[idx] == media_token[7][0]) { /* "<", */
504 token_t *token = (token_t*)calloc(1, sizeof(token_t));
505 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
507 ret = __tokenize_string(token, tmp, idx);
510 media_content_error("tokenize error occured");
513 *token_list = g_list_append(*token_list, token);
517 token_t *token = (token_t*)calloc(1, sizeof(token_t));
518 int size = __tokenize_operator(token, tmp, 7);
520 if (token != NULL && STRING_VALID(token->str)) {
521 *token_list = g_list_append(*token_list, token);
526 media_content_error("tokenize error occured");
530 } else if (tmp[idx] == media_token[8][0] && tmp[idx+1] == media_token[8][1]) { /* ">=", */
532 token_t *token = (token_t*)calloc(1, sizeof(token_t));
533 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
535 ret = __tokenize_string(token, tmp, idx);
538 media_content_error("tokenize error occured");
541 *token_list = g_list_append(*token_list, token);
545 token_t *token = (token_t*)calloc(1, sizeof(token_t));
546 int size = __tokenize_operator(token, tmp, 8);
548 if (token != NULL && STRING_VALID(token->str)) {
549 *token_list = g_list_append(*token_list, token);
554 media_content_error("tokenize error occured");
558 } else if (tmp[idx] == media_token[9][0]) { /* ">", */
560 token_t *token = (token_t*)calloc(1, sizeof(token_t));
561 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
563 ret = __tokenize_string(token, tmp, idx);
566 media_content_error("tokenize error occured");
569 *token_list = g_list_append(*token_list, token);
573 token_t *token = (token_t*)calloc(1, sizeof(token_t));
574 int size = __tokenize_operator(token, tmp, 9);
576 if (token != NULL && STRING_VALID(token->str)) {
577 *token_list = g_list_append(*token_list, token);
582 media_content_error("tokenize error occured");
585 } else if (tmp[idx] == media_token[10][0] && tmp[idx+1] == media_token[10][1]) { /* "!=", */
587 token_t *token = (token_t*)calloc(1, sizeof(token_t));
588 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
590 ret = __tokenize_string(token, tmp, idx);
593 media_content_error("tokenize error occured");
596 *token_list = g_list_append(*token_list, token);
600 token_t *token = (token_t*)calloc(1, sizeof(token_t));
601 int size = __tokenize_operator(token, tmp, 10);
603 if (token != NULL && STRING_VALID(token->str)) {
604 *token_list = g_list_append(*token_list, token);
609 media_content_error("tokenize error occured");
615 if (*tmp) { /* remained string */
616 token_t *token = (token_t*)calloc(1, sizeof(token_t));
617 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
619 ret = __tokenize_string(token, tmp, idx);
622 media_content_error("tokenize error occured");
626 if (token != NULL && STRING_VALID(token->str)) {
627 *token_list = g_list_append(*token_list, token);
630 media_content_error("tokenize error occured");
635 return MEDIA_CONTENT_ERROR_NONE;
638 int _media_filter_attribute_create(attribute_h *attr)
640 int ret = MEDIA_CONTENT_ERROR_NONE;
642 media_content_retvm_if(attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
644 attribute_s *_attr = (attribute_s*)calloc(1, sizeof(attribute_s));
645 media_content_retvm_if(_attr == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
647 _attr->attr_map = g_hash_table_new(g_str_hash, g_str_equal);
648 *attr = (attribute_h)_attr;
653 int _media_filter_attribute_add(attribute_h attr, const char *user_attr, const char *platform_attr)
655 int ret = MEDIA_CONTENT_ERROR_NONE;
657 char *_platform = NULL;
658 attribute_s *_attr = (attribute_s*)attr;
660 media_content_retvm_if(_attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
662 if (STRING_VALID(user_attr) && STRING_VALID(platform_attr)) {
663 _user = strdup(user_attr);
664 media_content_retvm_if(_user == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
666 _platform = strdup(platform_attr);
667 if (_platform == NULL) {
669 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
670 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
673 g_hash_table_insert(_attr->attr_map, _user, _platform);
675 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
676 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
682 int _media_filter_attribute_destory(attribute_h attr)
684 int ret = MEDIA_CONTENT_ERROR_NONE;
685 attribute_s *_attr = (attribute_s*)attr;
687 media_content_retvm_if(_attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
689 if (_attr->attr_map != NULL) {
690 g_hash_table_foreach(_attr->attr_map, __filter_attribute_free_value, NULL);
691 g_hash_table_destroy(_attr->attr_map);
699 int _media_filter_attribute_generate(attribute_h attr, char *condition, media_content_collation_e collate_type, char **generated_condition)
701 unsigned int idx = 0;
702 GList *token_list = NULL;
704 int ret = MEDIA_CONTENT_ERROR_NONE;
705 int total_str_size = 0;
708 media_content_retvm_if(condition == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid condition");
709 media_content_retvm_if(generated_condition == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid generated_condition");
710 media_content_retvm_if(attr == NULL, MEDIA_CONTENT_ERROR_DB_FAILED, "DB field mapping table doesn't exist. Check db connection");
712 if (__tokenize(&token_list, condition) < 0) {
713 media_content_error("INVALID_PARAMETER(0x%08x):Invalid the condition", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
714 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
717 for (idx = 0; idx < g_list_length(token_list); idx++) {
718 token = (token_t*)g_list_nth_data(token_list, idx);
720 if (token->type == UNKNOWN_TYPE) {
721 char *replace_str = __media_filter_replace_attr(attr, token->str);
722 if (STRING_VALID(replace_str)) {
723 SAFE_FREE(token->str);
724 token->str = replace_str;
728 total_str_size += strlen(token->str)+1;
729 /* media_content_debug("[%d][type:%d]:%s", idx, token->type, token->str); */
732 /* make the statment */
733 size = total_str_size + COLLATE_STR_SIZE + 1;
734 *generated_condition = (char*)calloc(size, sizeof(char));
736 for (idx = 0; idx < g_list_length(token_list); idx++) {
737 token = (token_t*)g_list_nth_data(token_list, idx);
739 if ((token != NULL) && STRING_VALID(token->str)) {
740 SAFE_STRLCAT(*generated_condition, token->str, size);
741 SAFE_STRLCAT(*generated_condition, SPACE, size);
743 SAFE_FREE(token->str);
748 if (collate_type == MEDIA_CONTENT_COLLATE_NOCASE || collate_type == MEDIA_CONTENT_COLLATE_RTRIM || collate_type == MEDIA_CONTENT_COLLATE_LOCALIZED) {
749 SAFE_STRLCAT(*generated_condition, "COLLATE ", size);
750 SAFE_STRLCAT(*generated_condition, __get_collate_str(collate_type), size);
751 SAFE_STRLCAT(*generated_condition, SPACE, size);
754 /* media_content_debug("statement : %s(%d) (total:%d)", *generated_condition, strlen(*generated_condition), total_str_size); */
755 media_content_sec_debug("Condition : %s", *generated_condition);
757 /* if(*generated_condition != NULL)
760 if (token_list != NULL)
761 g_list_free(token_list);
766 int _media_filter_attribute_option_generate(attribute_h attr, filter_h filter, char **generated_option)
768 int ret = MEDIA_CONTENT_ERROR_NONE;
769 filter_s *_filter = NULL;
770 char option_query[DEFAULT_QUERY_SIZE] = {0, };
771 char condition[DEFAULT_QUERY_SIZE] = {0, };
774 media_content_retvm_if(attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
775 media_content_retvm_if(filter == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid filter");
777 _filter = (filter_s*)filter;
779 memset(option_query, 0x00, sizeof(option_query));
782 if (STRING_VALID(_filter->order_keyword) && ((_filter->order_type == MEDIA_CONTENT_ORDER_ASC) || (_filter->order_type == MEDIA_CONTENT_ORDER_DESC))) {
783 unsigned int idx = 0;
784 int total_str_size = 0;
785 GList *token_list = NULL;
789 if (__tokenize_attribute(&token_list, _filter->order_keyword) < 0) {
790 media_content_error("INVALID_PARAMETER(0x%08x):Invalid the condition", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
791 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
794 for (idx = 0; idx < g_list_length(token_list); idx++) {
795 token = (token_t*)g_list_nth_data(token_list, idx);
797 if (token->type == UNKNOWN_TYPE) {
798 char *replace_str = __media_filter_replace_attr(attr, token->str);
799 if (STRING_VALID(replace_str)) {
800 attr_str = (char*)calloc(strlen(replace_str) + COLLATE_STR_SIZE + 1, sizeof(char));
801 if (attr_str == NULL) {
802 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
803 SAFE_FREE(replace_str);
807 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)
808 snprintf(attr_str, strlen(replace_str) + COLLATE_STR_SIZE + 1, "%s COLLATE %s %s", replace_str, __get_collate_str(_filter->order_collate_type), __get_order_str(_filter->order_type));
810 snprintf(attr_str, strlen(replace_str) + COLLATE_STR_SIZE + 1, "%s %s", replace_str, __get_order_str(_filter->order_type));
812 SAFE_FREE(token->str);
813 token->str = attr_str;
814 SAFE_FREE(replace_str);
816 media_content_error("There is no matched db field for %s", token->str);
820 total_str_size += strlen(token->str) + 1;
821 /* media_content_debug("[%d][type:%d]:%s", idx, token->type, token->str); */
824 /* make the statment */
825 char *generated_condition = NULL;
826 size = total_str_size + COLLATE_STR_SIZE + 1;
827 generated_condition = (char*)calloc(size, sizeof(char));
829 for (idx = 0; idx < g_list_length(token_list); idx++) {
830 token = (token_t*)g_list_nth_data(token_list, idx);
832 if ((token != NULL) && STRING_VALID(token->str)) {
833 /* media_content_debug("[%d] %s", idx, token->str); */
834 SAFE_STRLCAT(generated_condition, token->str, size);
835 SAFE_STRLCAT(generated_condition, SPACE, size);
837 SAFE_FREE(token->str);
842 snprintf(condition, sizeof(condition), "ORDER BY %s", generated_condition);
843 SAFE_STRLCAT(option_query, condition, sizeof(option_query));
845 if (token_list != NULL)
846 g_list_free(token_list);
848 SAFE_FREE(generated_condition);
852 SAFE_STRLCAT(option_query, SPACE, sizeof(option_query));
853 snprintf(condition, sizeof(condition), "LIMIT %d, %d", _filter->offset, _filter->count);
854 SAFE_STRLCAT(option_query, condition, sizeof(option_query));
856 if (STRING_VALID(option_query))
857 *generated_option = strdup(option_query);
859 *generated_option = NULL;
864 int media_filter_create(filter_h *filter)
866 int ret = MEDIA_CONTENT_ERROR_NONE;
868 media_content_retvm_if(filter == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid filter");
870 filter_s *_filter = (filter_s*)calloc(1, sizeof(filter_s));
871 media_content_retvm_if(_filter == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
873 _filter->storage_id = NULL;
874 _filter->condition = NULL;
875 _filter->order_keyword = NULL;
876 _filter->order_type = -1;
877 _filter->condition_collate_type = MEDIA_CONTENT_COLLATE_DEFAULT;
878 _filter->order_collate_type = MEDIA_CONTENT_COLLATE_DEFAULT;
879 _filter->offset = -1;
882 *filter = (filter_h)_filter;
887 int media_filter_destroy(filter_h filter)
889 int ret = MEDIA_CONTENT_ERROR_NONE;
890 filter_s *_filter = (filter_s*)filter;
893 SAFE_FREE(_filter->storage_id);
894 SAFE_FREE(_filter->condition);
895 SAFE_FREE(_filter->order_keyword);
898 ret = MEDIA_CONTENT_ERROR_NONE;
900 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
901 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
907 int media_filter_set_offset(filter_h filter, int offset, int count)
909 int ret = MEDIA_CONTENT_ERROR_NONE;
910 filter_s *_filter = (filter_s*)filter;
912 if (_filter != NULL) {
913 _filter->offset = offset;
914 _filter->count = count;
916 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
917 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
923 int media_filter_set_condition(filter_h filter, const char *condition, media_content_collation_e collate_type)
925 int ret = MEDIA_CONTENT_ERROR_NONE;
926 filter_s *_filter = (filter_s*)filter;
928 if ((_filter != NULL) && STRING_VALID(condition)
929 && ((collate_type >= MEDIA_CONTENT_COLLATE_DEFAULT) && (collate_type <= MEDIA_CONTENT_COLLATE_LOCALIZED))) {
930 if (STRING_VALID(_filter->condition))
931 SAFE_FREE(_filter->condition);
933 _filter->condition = strdup(condition);
934 media_content_retvm_if(_filter->condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
936 media_content_sec_debug("Condition string : %s", _filter->condition);
938 _filter->condition_collate_type = collate_type;
940 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
941 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
947 int media_filter_set_order(filter_h filter, media_content_order_e order_type, const char *order_keyword, media_content_collation_e collate_type)
949 int ret = MEDIA_CONTENT_ERROR_NONE;
950 filter_s *_filter = (filter_s*)filter;
952 if ((_filter != NULL) && STRING_VALID(order_keyword)
953 && ((order_type == MEDIA_CONTENT_ORDER_ASC) || (order_type == MEDIA_CONTENT_ORDER_DESC))
954 && ((collate_type >= MEDIA_CONTENT_COLLATE_DEFAULT) && (collate_type <= MEDIA_CONTENT_COLLATE_LOCALIZED))) {
955 SAFE_FREE(_filter->order_keyword);
957 if (STRING_VALID(order_keyword)) {
958 _filter->order_keyword = strdup(order_keyword);
959 media_content_retvm_if(_filter->order_keyword == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
961 _filter->order_type = order_type;
962 _filter->order_collate_type = collate_type;
964 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
965 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
968 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
969 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
975 int media_filter_set_storage(filter_h filter, const char *storage_id)
977 int ret = MEDIA_CONTENT_ERROR_NONE;
978 filter_s *_filter = (filter_s*)filter;
980 if ((_filter != NULL) && STRING_VALID(storage_id)) {
981 if (STRING_VALID(_filter->storage_id))
982 SAFE_FREE(_filter->storage_id);
984 _filter->storage_id = strdup(storage_id);
985 media_content_retvm_if(_filter->storage_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
987 media_content_sec_debug("storage_id : %s", _filter->storage_id);
989 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
990 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
996 int media_filter_get_offset(filter_h filter, int *offset, int *count)
998 int ret = MEDIA_CONTENT_ERROR_NONE;
999 filter_s *_filter = (filter_s*)filter;
1002 *offset = _filter->offset;
1003 *count = _filter->count;
1005 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1006 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1012 int media_filter_get_condition(filter_h filter, char **condition, media_content_collation_e *collate_type)
1014 int ret = MEDIA_CONTENT_ERROR_NONE;
1015 filter_s *_filter = (filter_s*)filter;
1018 if (STRING_VALID(_filter->condition)) {
1019 *condition = strdup(_filter->condition);
1020 media_content_retvm_if(*condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1025 *collate_type = _filter->condition_collate_type;
1027 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1028 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1034 int media_filter_get_order(filter_h filter, media_content_order_e* order_type, char **order_keyword, media_content_collation_e *collate_type)
1036 int ret = MEDIA_CONTENT_ERROR_NONE;
1037 filter_s *_filter = (filter_s*)filter;
1040 if (STRING_VALID(_filter->order_keyword)) {
1041 *order_keyword = strdup(_filter->order_keyword);
1042 media_content_retvm_if(*order_keyword == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1044 *order_keyword = NULL;
1047 *order_type = _filter->order_type;
1048 *collate_type = _filter->order_collate_type;
1050 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1051 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1057 int media_filter_get_storage(filter_h filter, char **storage_id)
1059 int ret = MEDIA_CONTENT_ERROR_NONE;
1060 filter_s *_filter = (filter_s*)filter;
1063 if (STRING_VALID(_filter->storage_id)) {
1064 *storage_id = strdup(_filter->storage_id);
1065 media_content_retvm_if(*storage_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1070 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1071 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;