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>
20 static const char *media_token[] =
35 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)
59 const char *china = "zh_CN";
60 const char *hongkong = "zh_HK";
65 bool pinyin_support = FALSE;
66 media_svc_check_pinyin_support(&pinyin_support);
69 /*Check Language Setting*/
70 lang = vconf_get_str(VCONFKEY_LANGSET);
71 media_content_retvm_if(lang == NULL, ret, "Fail to get string of language set");
73 if((strncmp(china, lang, strlen(china)) == 0) ||
74 (strncmp(hongkong, lang, strlen(hongkong)) == 0))
85 static char *__get_order_str(media_content_order_e order_enum)
88 case MEDIA_CONTENT_ORDER_ASC:
90 case MEDIA_CONTENT_ORDER_DESC:
91 return (char *)"DESC";
97 static char *__get_collate_str(media_content_collation_e collate_type)
99 switch(collate_type) {
100 case MEDIA_CONTENT_COLLATE_NOCASE:
101 return (char *)"NOCASE";
102 case MEDIA_CONTENT_COLLATE_RTRIM:
103 return (char *)"RTRIM";
104 case MEDIA_CONTENT_COLLATE_LOCALIZED:
105 if(__is_pinyin_needed())
106 return (char *)"NOCASE";
108 return (char *)"localized";
109 default: return (char *)" ";
113 static void __filter_attribute_free_value(gpointer key, gpointer value, gpointer user_data)
119 static char *__media_filter_replace_attr(attribute_h attr, char *name)
121 char *key_temp = NULL;
122 char *generated_value = NULL;
123 attribute_s *_attr = (attribute_s *)attr;
125 if(!g_hash_table_lookup_extended(_attr->attr_map,
127 (gpointer)&key_temp, (gpointer)&generated_value))
129 //can't find the value
130 //media_content_error("NOT_FOUND_VALUE(%s)", name);
134 if(STRING_VALID(generated_value))
136 return strdup(generated_value);
139 media_content_error("__media_filter_replace_attr fail");
144 static int __tokenize_operator(token_t *token, const char *str, int op_type)
147 const char *tmp = str;
149 if(token != NULL && STRING_VALID(tmp))
151 token->type = op_type;
152 int token_size = strlen(media_token[op_type]);
153 media_content_retvm_if(token_size == 0, -1, "Invalid token_size. op_type[%d]", op_type);
155 token->str = (char*)calloc(token_size+1, sizeof(char));
156 media_content_retvm_if(token->str == NULL, -1, "OUT_OF_MEMORY");
158 strncpy(token->str, tmp, token_size);
159 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
170 static int __tokenize_string(token_t *token, const char *str, int size)
173 const char *tmp = str;
175 if(token != NULL && STRING_VALID(tmp) && size > 0)
177 token->str = (char*)calloc(size+1, sizeof(char));
178 media_content_retvm_if(token->str == NULL, -1, "OUT_OF_MEMORY");
180 token->type = UNKNOWN_TYPE;
181 strncpy(token->str, tmp, size);
182 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
192 static int __tokenize_attribute(GList **token_list, const char *str)
197 media_content_retvm_if(!STRING_VALID(str), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Parameter string is invalid");
199 const char *tmp = str;
200 const char *dst_ptr = str + strlen(str);
202 for (idx = 0; (*(tmp+idx)) && (tmp < dst_ptr); idx++)
204 //media_content_debug("[%d] '%c'", idx, tmp[idx]);
205 if(tmp[idx] == ' ') //" "
207 if(idx == 0) // ignore the space.
213 token_t *token = (token_t*)calloc(1, sizeof(token_t));
214 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
216 token->type = UNKNOWN_TYPE;
217 token->str = (char*)calloc(idx+1, sizeof(char));
218 if(token->str == NULL)
221 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
224 strncpy(token->str, tmp, idx);
225 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
226 *token_list = g_list_append(*token_list, token);
227 tmp = tmp +idx + strlen(media_token[0]);
230 else if(tmp[idx] == ',') // " , "
234 token_t *token = (token_t*)calloc(1, sizeof(token_t));
235 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
237 ret = __tokenize_string(token, tmp, idx);
241 media_content_error("tokenize error occured");
246 *token_list = g_list_append(*token_list, token);
251 token_t *token = (token_t*)calloc(1, sizeof(token_t));
252 int size = __tokenize_operator(token, tmp,3);
254 if(token != NULL && STRING_VALID(token->str))
256 *token_list = g_list_append(*token_list, token);
263 media_content_error("tokenize error occured");
269 if(*tmp) //remained string
271 token_t *token = (token_t*)calloc(1, sizeof(token_t));
272 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
274 ret = __tokenize_string(token, tmp, idx);
278 media_content_error("tokenize error occured");
282 if(token != NULL && STRING_VALID(token->str))
284 *token_list = g_list_append(*token_list, token);
289 media_content_error("tokenize error occured");
294 return MEDIA_CONTENT_ERROR_NONE;
297 static int __tokenize(GList **token_list, const char *str)
302 media_content_retvm_if(!STRING_VALID(str), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Parameter string is invalid");
304 const char *tmp = str;
305 const char *dst_ptr = str + strlen(str);
307 for (idx = 0; (*(tmp+idx)) && (tmp < dst_ptr); idx++)
309 //media_content_debug("[%d] '%c'", idx, tmp[idx]);
310 if(tmp[idx] == media_token[0][0]) //" "
312 if(idx == 0) // ignore the space.
319 token_t *token = (token_t*)calloc(1, sizeof(token_t));
320 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
322 token->type = UNKNOWN_TYPE;
323 token->str = (char*)calloc(idx+1, sizeof(char));
324 if(token->str == NULL)
327 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
330 strncpy(token->str, tmp, idx);
331 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
332 *token_list = g_list_append(*token_list, token);
333 tmp = tmp +idx + strlen(media_token[0]);
336 else if(tmp[idx] == media_token[1][0]) // " \" "
340 for(j = idx+1; tmp[j]; j++) //find next quotation
342 if(tmp[j] == media_token[1][0] && tmp[j+1] == media_token[1][0])
347 if(tmp[j] == media_token[1][0])
349 token_t *token = (token_t*)calloc(1, sizeof(token_t));
350 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
352 token->str = (char*) calloc(j+1+1, sizeof(char));
353 if(token->str == NULL)
356 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
359 token->type = STRING_TYPE;
360 strncpy(token->str, tmp, j+1);
361 //media_content_debug("type : [%d] str : [%s], j : %d", token->type, token->str, j);
362 *token_list = g_list_append(*token_list, token);
363 tmp = tmp + strlen(token->str);
370 if(!flag && *tmp != '\0' && tmp[j]=='\0')
372 token_t *token = (token_t*)calloc(1, sizeof(token_t));
373 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
375 token->str = (char*) calloc(j+1,sizeof(char));
376 if(token->str == NULL)
379 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
382 token->type = UNKNOWN_TYPE;
383 strncpy(token->str, tmp,j);
384 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
385 *token_list = g_list_append(*token_list, token);
386 tmp = tmp +strlen(token->str);
390 else if(tmp[idx] == media_token[2][0]) // " \' "
394 for(j = idx+1; tmp[j]; j++)
396 if(tmp[j] == media_token[2][0] && tmp[j+1] == media_token[2][0])
401 if(tmp[j] == media_token[2][0])
403 token_t *token = (token_t*)calloc(1, sizeof(token_t));
404 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
406 token->str = (char*) calloc(j+1+1, sizeof(char));
407 if(token->str == NULL)
410 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
413 token->type = STRING_TYPE;
414 strncpy(token->str, tmp, j+1);
415 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
416 *token_list = g_list_append(*token_list, token);
417 tmp = tmp + strlen(token->str);
424 if(!flag && *tmp != '\0' && tmp[j]=='\0')
426 token_t *token = (token_t*)calloc(1, sizeof(token_t));
427 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
429 token->str = (char*) calloc(j+1,sizeof(char));
430 if(token->str == NULL)
433 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
436 token->type = UNKNOWN_TYPE;
437 strncpy(token->str, tmp,j);
438 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
439 *token_list = g_list_append(*token_list, token);
440 tmp = tmp + strlen(token->str);
444 else if(tmp[idx] == media_token[3][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);
455 media_content_error("tokenize error occured");
460 *token_list = g_list_append(*token_list, token);
464 token_t *token = (token_t*)calloc(1, sizeof(token_t));
465 int size = __tokenize_operator(token, tmp,3);
467 if(token != NULL && STRING_VALID(token->str))
469 *token_list = g_list_append(*token_list, token);
476 media_content_error("tokenize error occured");
481 else if(tmp[idx] == media_token[4][0]) //")"
485 token_t *token = (token_t*)calloc(1, sizeof(token_t));
486 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
488 ret = __tokenize_string(token, tmp, idx);
492 media_content_error("tokenize error occured");
497 *token_list = g_list_append(*token_list, token);
501 token_t *token = (token_t*)calloc(1, sizeof(token_t));
502 int size = __tokenize_operator(token, tmp,4);
504 if(token != NULL && STRING_VALID(token->str))
506 *token_list = g_list_append(*token_list, token);
513 media_content_error("tokenize error occured");
518 else if(tmp[idx] == media_token[5][0]) //"="
522 token_t *token = (token_t*)calloc(1, sizeof(token_t));
523 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
525 ret = __tokenize_string(token, tmp, idx);
529 media_content_error("tokenize error occured");
534 *token_list = g_list_append(*token_list, token);
538 token_t *token = (token_t*)calloc(1, sizeof(token_t));
539 int size = __tokenize_operator(token, tmp,5);
541 if(token != NULL && STRING_VALID(token->str))
543 *token_list = g_list_append(*token_list, token);
550 media_content_error("tokenize error occured");
555 else if(tmp[idx] == media_token[6][0] && tmp[idx+1] == media_token[6][1]) //"<=",
559 token_t *token = (token_t*)calloc(1, sizeof(token_t));
560 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
562 ret = __tokenize_string(token, tmp, idx);
566 media_content_error("tokenize error occured");
571 *token_list = g_list_append(*token_list, token);
575 token_t *token = (token_t*)calloc(1, sizeof(token_t));
576 int size = __tokenize_operator(token, tmp,6);
578 if(token != NULL && STRING_VALID(token->str))
580 *token_list = g_list_append(*token_list, token);
587 media_content_error("tokenize error occured");
592 else if(tmp[idx] == media_token[7][0]) //"<",
596 token_t *token = (token_t*)calloc(1, sizeof(token_t));
597 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
599 ret = __tokenize_string(token, tmp, idx);
603 media_content_error("tokenize error occured");
608 *token_list = g_list_append(*token_list, token);
612 token_t *token = (token_t*)calloc(1, sizeof(token_t));
613 int size = __tokenize_operator(token, tmp,7);
615 if(token != NULL && STRING_VALID(token->str))
617 *token_list = g_list_append(*token_list, token);
624 media_content_error("tokenize error occured");
629 else if(tmp[idx] == media_token[8][0] && tmp[idx+1] == media_token[8][1]) //">=",
633 token_t *token = (token_t*)calloc(1, sizeof(token_t));
634 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
636 ret = __tokenize_string(token, tmp, idx);
640 media_content_error("tokenize error occured");
645 *token_list = g_list_append(*token_list, token);
649 token_t *token = (token_t*)calloc(1, sizeof(token_t));
650 int size = __tokenize_operator(token, tmp,8);
652 if(token != NULL && STRING_VALID(token->str))
654 *token_list = g_list_append(*token_list, token);
661 media_content_error("tokenize error occured");
666 else if(tmp[idx] == media_token[9][0]) //">",
670 token_t *token = (token_t*)calloc(1, sizeof(token_t));
671 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
673 ret = __tokenize_string(token, tmp, idx);
677 media_content_error("tokenize error occured");
682 *token_list = g_list_append(*token_list, token);
686 token_t *token = (token_t*)calloc(1, sizeof(token_t));
687 int size = __tokenize_operator(token, tmp,9);
689 if(token != NULL && STRING_VALID(token->str))
691 *token_list = g_list_append(*token_list, token);
698 media_content_error("tokenize error occured");
705 if(*tmp) //remained string
707 token_t *token = (token_t*)calloc(1, sizeof(token_t));
708 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
710 ret = __tokenize_string(token, tmp, idx);
714 media_content_error("tokenize error occured");
718 if(token != NULL && STRING_VALID(token->str))
720 *token_list = g_list_append(*token_list, token);
725 media_content_error("tokenize error occured");
730 return MEDIA_CONTENT_ERROR_NONE;
733 int _media_filter_attribute_create(attribute_h *attr)
735 int ret = MEDIA_CONTENT_ERROR_NONE;
737 media_content_retvm_if(attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
739 attribute_s *_attr = (attribute_s*)calloc(1, sizeof(attribute_s));
740 media_content_retvm_if(_attr == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
742 _attr->attr_map = g_hash_table_new (g_str_hash, g_str_equal);
743 *attr = (attribute_h)_attr;
748 int _media_filter_attribute_add(attribute_h attr, const char *user_attr, const char *platform_attr)
750 int ret = MEDIA_CONTENT_ERROR_NONE;
752 char *_platform = NULL;
753 attribute_s *_attr = (attribute_s*)attr;
755 media_content_retvm_if(_attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
757 if(STRING_VALID(user_attr) && STRING_VALID(platform_attr))
759 _user = strdup(user_attr);
760 media_content_retvm_if(_user == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
762 _platform = strdup(platform_attr);
763 if(_platform == NULL)
766 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
767 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
770 g_hash_table_insert (_attr->attr_map, _user, _platform);
774 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
775 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
781 int _media_filter_attribute_destory(attribute_h attr)
783 int ret = MEDIA_CONTENT_ERROR_NONE;
784 attribute_s *_attr = (attribute_s*)attr;
786 media_content_retvm_if(_attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
788 if(_attr->attr_map != NULL)
790 g_hash_table_foreach(_attr->attr_map, __filter_attribute_free_value, NULL);
791 g_hash_table_destroy(_attr->attr_map);
799 int _media_filter_attribute_generate(attribute_h attr, char *condition, media_content_collation_e collate_type, char **generated_condition)
801 unsigned int idx = 0;
802 GList *token_list = NULL;
804 int ret = MEDIA_CONTENT_ERROR_NONE;
805 int total_str_size = 0;
808 media_content_retvm_if(condition == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid condition");
809 media_content_retvm_if(generated_condition == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid generated_condition");
810 media_content_retvm_if(attr == NULL, MEDIA_CONTENT_ERROR_DB_FAILED, "DB field mapping table doesn't exist. Check db connection");
812 if(__tokenize(&token_list, condition) < 0)
814 media_content_error("INVALID_PARAMETER(0x%08x):Invalid the condition", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
815 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
818 for(idx = 0; idx < g_list_length(token_list); idx++)
820 token = (token_t*)g_list_nth_data(token_list, idx);
822 if(token->type == UNKNOWN_TYPE)
824 char *replace_str = __media_filter_replace_attr(attr, token->str);
825 if(STRING_VALID(replace_str))
827 SAFE_FREE(token->str);
828 token->str = replace_str;
832 total_str_size += strlen(token->str)+1;
833 //media_content_debug("[%d][type:%d]:%s", idx, token->type, token->str);
837 size = total_str_size + COLLATE_STR_SIZE + 1;
838 * generated_condition = (char*)calloc(size, sizeof(char));
840 for(idx = 0; idx < g_list_length(token_list); idx++)
842 token = (token_t*)g_list_nth_data(token_list, idx);
844 if((token != NULL) && STRING_VALID(token->str))
846 SAFE_STRLCAT(*generated_condition, token->str, size);
847 SAFE_STRLCAT(*generated_condition, SPACE, size);
849 SAFE_FREE(token->str);
854 if(collate_type == MEDIA_CONTENT_COLLATE_NOCASE || collate_type == MEDIA_CONTENT_COLLATE_RTRIM ||collate_type == MEDIA_CONTENT_COLLATE_LOCALIZED) {
855 SAFE_STRLCAT(*generated_condition, "COLLATE ", size);
856 SAFE_STRLCAT(*generated_condition, __get_collate_str(collate_type), size);
857 SAFE_STRLCAT(*generated_condition, SPACE, size);
860 //media_content_debug("statement : %s(%d) (total:%d)", *generated_condition, strlen(*generated_condition), total_str_size);
861 media_content_sec_debug("Condition : %s", *generated_condition);
863 //if(*generated_condition != NULL)
866 if(token_list != NULL)
867 g_list_free(token_list);
872 int _media_filter_attribute_option_generate(attribute_h attr, filter_h filter, char **generated_option)
874 int ret = MEDIA_CONTENT_ERROR_NONE;
875 filter_s *_filter = NULL;
876 char option_query[DEFAULT_QUERY_SIZE] = {0, };
877 char condition[DEFAULT_QUERY_SIZE] = {0, };
879 //bool order_by = true;
881 media_content_retvm_if(attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
882 media_content_retvm_if(filter == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid filter");
884 _filter = (filter_s*)filter;
886 memset(option_query, 0x00, sizeof(option_query));
889 if(STRING_VALID(_filter->order_keyword) && ((_filter->order_type == MEDIA_CONTENT_ORDER_ASC) ||(_filter->order_type == MEDIA_CONTENT_ORDER_DESC)))
891 unsigned int idx = 0;
892 int total_str_size = 0;
893 GList *token_list = NULL;
897 if(__tokenize_attribute(&token_list, _filter->order_keyword) < 0)
899 media_content_error("INVALID_PARAMETER(0x%08x):Invalid the condition", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
900 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
903 for(idx = 0; idx < g_list_length(token_list); idx++)
905 token = (token_t*)g_list_nth_data(token_list, idx);
907 if(token->type == UNKNOWN_TYPE)
909 char *replace_str = __media_filter_replace_attr(attr, token->str);
910 if(STRING_VALID(replace_str))
912 attr_str = (char*)calloc(strlen(replace_str) + COLLATE_STR_SIZE + 1, sizeof(char));
915 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
916 SAFE_FREE(replace_str);
920 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) {
921 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));
923 snprintf(attr_str, strlen(replace_str) + COLLATE_STR_SIZE + 1, "%s %s", replace_str, __get_order_str(_filter->order_type));
926 SAFE_FREE(token->str);
927 token->str = attr_str;
928 SAFE_FREE(replace_str);
932 media_content_error("There is no matched db field for %s", token->str);
936 total_str_size += strlen(token->str) + 1;
937 //media_content_debug("[%d][type:%d]:%s", idx, token->type, token->str);
941 char *generated_condition = NULL;
942 size = total_str_size + COLLATE_STR_SIZE + 1;
943 generated_condition = (char*)calloc(size, sizeof(char));
945 for(idx = 0; idx < g_list_length(token_list); idx++)
947 token = (token_t*)g_list_nth_data(token_list, idx);
949 if((token != NULL) && STRING_VALID(token->str))
951 //media_content_debug("[%d] %s", idx, token->str);
952 SAFE_STRLCAT(generated_condition, token->str, size);
953 SAFE_STRLCAT(generated_condition, SPACE, size);
955 SAFE_FREE(token->str);
960 snprintf(condition, sizeof(condition), "ORDER BY %s", generated_condition);
961 SAFE_STRLCAT(option_query, condition, sizeof(option_query));
963 if(token_list != NULL)
964 g_list_free(token_list);
966 SAFE_FREE(generated_condition);
970 SAFE_STRLCAT(option_query, SPACE, sizeof(option_query));
971 snprintf(condition, sizeof(condition), "LIMIT %d, %d", _filter->offset, _filter->count);
972 SAFE_STRLCAT(option_query, condition, sizeof(option_query));
974 if(STRING_VALID(option_query))
976 *generated_option = strdup(option_query);
980 *generated_option = NULL;
986 int media_filter_create(filter_h *filter)
988 int ret = MEDIA_CONTENT_ERROR_NONE;
990 media_content_retvm_if(filter == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid filter");
992 filter_s *_filter = (filter_s*)calloc(1, sizeof(filter_s));
993 media_content_retvm_if(_filter == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
995 _filter->storage_id = NULL;
996 _filter->condition = NULL;
997 _filter->order_keyword = NULL;
998 _filter->order_type = -1;
999 _filter->condition_collate_type = MEDIA_CONTENT_COLLATE_DEFAULT;
1000 _filter->order_collate_type = MEDIA_CONTENT_COLLATE_DEFAULT;
1001 _filter->offset = -1;
1002 _filter->count = -1;
1004 *filter = (filter_h)_filter;
1009 int media_filter_destroy(filter_h filter)
1011 int ret = MEDIA_CONTENT_ERROR_NONE;
1012 filter_s *_filter = (filter_s*)filter;
1016 SAFE_FREE(_filter->storage_id);
1017 SAFE_FREE(_filter->condition);
1018 SAFE_FREE(_filter->order_keyword);
1021 ret = MEDIA_CONTENT_ERROR_NONE;
1025 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1026 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1032 int media_filter_set_offset(filter_h filter, int offset, int count)
1034 int ret = MEDIA_CONTENT_ERROR_NONE;
1035 filter_s *_filter = (filter_s*)filter;
1039 _filter->offset = offset;
1040 _filter->count = count;
1044 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1045 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1051 int media_filter_set_condition(filter_h filter, const char *condition, media_content_collation_e collate_type)
1053 int ret = MEDIA_CONTENT_ERROR_NONE;
1054 filter_s *_filter = (filter_s*)filter;
1056 if((_filter != NULL) && STRING_VALID(condition)
1057 && ((collate_type >= MEDIA_CONTENT_COLLATE_DEFAULT) && (collate_type <= MEDIA_CONTENT_COLLATE_LOCALIZED)))
1059 if(STRING_VALID(_filter->condition))
1061 SAFE_FREE(_filter->condition);
1064 _filter->condition = strdup(condition);
1065 media_content_retvm_if(_filter->condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1067 media_content_sec_debug("Condition string : %s", _filter->condition);
1069 _filter->condition_collate_type = collate_type;
1073 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1074 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1080 int media_filter_set_order(filter_h filter, media_content_order_e order_type, const char *order_keyword, media_content_collation_e collate_type)
1082 int ret = MEDIA_CONTENT_ERROR_NONE;
1083 filter_s *_filter = (filter_s*)filter;
1085 if((_filter != NULL) && STRING_VALID(order_keyword)
1086 && ((order_type == MEDIA_CONTENT_ORDER_ASC) ||(order_type == MEDIA_CONTENT_ORDER_DESC))
1087 && ((collate_type >= MEDIA_CONTENT_COLLATE_DEFAULT) && (collate_type <= MEDIA_CONTENT_COLLATE_LOCALIZED)))
1089 SAFE_FREE(_filter->order_keyword);
1091 if(STRING_VALID(order_keyword))
1093 _filter->order_keyword = strdup(order_keyword);
1094 media_content_retvm_if(_filter->order_keyword == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1096 _filter->order_type = order_type;
1097 _filter->order_collate_type = collate_type;
1101 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1102 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1107 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1108 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1114 int media_filter_set_storage(filter_h filter, const char *storage_id)
1116 int ret = MEDIA_CONTENT_ERROR_NONE;
1117 filter_s *_filter = (filter_s*)filter;
1119 if((_filter != NULL) && STRING_VALID(storage_id))
1121 if(STRING_VALID(_filter->storage_id))
1123 SAFE_FREE(_filter->storage_id);
1126 _filter->storage_id = strdup(storage_id);
1127 media_content_retvm_if(_filter->storage_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1129 media_content_sec_debug("storage_id : %s", _filter->storage_id);
1133 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1134 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1140 int media_filter_get_offset(filter_h filter, int *offset, int *count)
1142 int ret = MEDIA_CONTENT_ERROR_NONE;
1143 filter_s *_filter = (filter_s*)filter;
1147 *offset = _filter->offset;
1148 *count = _filter->count;
1152 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1153 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1159 int media_filter_get_condition(filter_h filter, char **condition, media_content_collation_e *collate_type)
1161 int ret = MEDIA_CONTENT_ERROR_NONE;
1162 filter_s *_filter = (filter_s*)filter;
1166 if(STRING_VALID(_filter->condition))
1168 *condition = strdup(_filter->condition);
1169 media_content_retvm_if(*condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1176 *collate_type = _filter->condition_collate_type;
1180 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1181 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1187 int media_filter_get_order(filter_h filter, media_content_order_e* order_type, char **order_keyword, media_content_collation_e *collate_type)
1189 int ret = MEDIA_CONTENT_ERROR_NONE;
1190 filter_s *_filter = (filter_s*)filter;
1194 if(STRING_VALID(_filter->order_keyword))
1196 *order_keyword = strdup(_filter->order_keyword);
1197 media_content_retvm_if(*order_keyword == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1201 *order_keyword = NULL;
1204 *order_type = _filter->order_type;
1205 *collate_type = _filter->order_collate_type;
1209 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1210 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1216 int media_filter_get_storage(filter_h filter, char **storage_id)
1218 int ret = MEDIA_CONTENT_ERROR_NONE;
1219 filter_s *_filter = (filter_s*)filter;
1223 if(STRING_VALID(_filter->storage_id))
1225 *storage_id = strdup(_filter->storage_id);
1226 media_content_retvm_if(*storage_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1235 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1236 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;