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>
19 #include <media_util_private.h>
22 static const char *media_token[] = {
37 typedef struct _token_t {
43 #define MAX_LEFT_VALUE 512
46 #define UNKNOWN_TYPE 1000
47 #define STRING_TYPE 100
49 static char *__get_order_str(media_content_order_e order_enum);
50 static char *__get_collate_str(media_content_collation_e collate_type);
51 static void __filter_attribute_free_value(gpointer key, gpointer value, gpointer user_data);
52 static char *__media_filter_replace_attr(attribute_h attr, char *name);
53 static int __tokenize_operator(token_t *token, const char *str, int op_type);
54 static int __tokenize(GList **token_list, const char *str);
56 static bool __is_pinyin_needed(void)
59 const char *china = "zh_CN";
60 const char *hongkong = "zh_HK";
64 bool pinyin_support = FALSE;
65 media_svc_check_pinyin_support(&pinyin_support);
67 /*Check Language Setting*/
68 lang = vconf_get_str(VCONFKEY_LANGSET);
69 media_content_retvm_if(lang == NULL, ret, "Fail to get string of language set");
71 if ((strncmp(china, lang, strlen(china)) == 0) ||
72 (strncmp(hongkong, lang, strlen(hongkong)) == 0)) {
82 static char *__get_order_str(media_content_order_e order_enum)
85 case MEDIA_CONTENT_ORDER_ASC:
87 case MEDIA_CONTENT_ORDER_DESC:
88 return (char *)"DESC";
94 static char *__get_collate_str(media_content_collation_e collate_type)
96 switch (collate_type) {
97 case MEDIA_CONTENT_COLLATE_NOCASE:
98 return (char *)"NOCASE";
99 case MEDIA_CONTENT_COLLATE_RTRIM:
100 return (char *)"RTRIM";
101 case MEDIA_CONTENT_COLLATE_LOCALIZED:
102 if (__is_pinyin_needed())
103 return (char *)"NOCASE";
105 return (char *)"localized";
106 default: return (char *)" ";
110 static void __filter_attribute_free_value(gpointer key, gpointer value, gpointer user_data)
116 static char *__media_filter_replace_attr(attribute_h attr, char *name)
118 char *key_temp = NULL;
119 char *generated_value = NULL;
120 attribute_s *_attr = (attribute_s *)attr;
122 if (!g_hash_table_lookup_extended(_attr->attr_map, name, (gpointer)&key_temp, (gpointer)&generated_value)) {
123 /* can't find the value */
124 /* media_content_error("NOT_FOUND_VALUE(%s)", name); */
128 if (STRING_VALID(generated_value))
129 return strdup(generated_value);
131 media_content_error("__media_filter_replace_attr fail");
136 static int __tokenize_operator(token_t *token, const char *str, int op_type)
139 const char *tmp = str;
141 if (token != NULL && STRING_VALID(tmp)) {
142 token->type = op_type;
143 int token_size = strlen(media_token[op_type]);
144 media_content_retvm_if(token_size == 0, -1, "Invalid token_size. op_type[%d]", op_type);
146 token->str = (char*)calloc(token_size+1, sizeof(char));
147 media_content_retvm_if(token->str == NULL, -1, "OUT_OF_MEMORY");
149 strncpy(token->str, tmp, token_size);
150 /*media_content_debug("type : [%d] str : [%s]", token->type, token->str);*/
159 static int __tokenize_string(token_t *token, const char *str, int size)
162 const char *tmp = str;
164 if (token != NULL && STRING_VALID(tmp) && size > 0) {
165 token->str = (char*)calloc(size+1, sizeof(char));
166 media_content_retvm_if(token->str == NULL, -1, "OUT_OF_MEMORY");
168 token->type = UNKNOWN_TYPE;
169 strncpy(token->str, tmp, size);
170 /*media_content_debug("type : [%d] str : [%s]", token->type, token->str);*/
178 static int __tokenize_attribute(GList **token_list, const char *str)
183 media_content_retvm_if(!STRING_VALID(str), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Parameter string is invalid");
185 const char *tmp = str;
186 const char *dst_ptr = str + strlen(str);
188 for (idx = 0; (*(tmp+idx)) && (tmp < dst_ptr); idx++) {
189 /*media_content_debug("[%d] '%c'", idx, tmp[idx]);*/
190 if (tmp[idx] == ' ') { /* " " */
191 if (idx == 0) { /* ignore the space. */
196 token_t *token = (token_t*)calloc(1, sizeof(token_t));
197 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
199 token->type = UNKNOWN_TYPE;
200 token->str = (char*)calloc(idx+1, sizeof(char));
201 if (token->str == NULL) {
203 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
206 strncpy(token->str, tmp, idx);
207 /*media_content_debug("type : [%d] str : [%s]", token->type, token->str);*/
208 *token_list = g_list_append(*token_list, token);
209 tmp = tmp +idx + strlen(media_token[0]);
211 } else if (tmp[idx] == ',') { /* " , " */
213 token_t *token = (token_t*)calloc(1, sizeof(token_t));
214 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
216 ret = __tokenize_string(token, tmp, idx);
219 media_content_error("tokenize error occured");
222 *token_list = g_list_append(*token_list, token);
227 token_t *token = (token_t*)calloc(1, sizeof(token_t));
228 int size = __tokenize_operator(token, tmp, 3);
230 if (token != NULL && STRING_VALID(token->str)) {
231 *token_list = g_list_append(*token_list, token);
236 media_content_error("tokenize error occured");
242 if (*tmp) { /* remained string */
243 token_t *token = (token_t*)calloc(1, sizeof(token_t));
244 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
246 ret = __tokenize_string(token, tmp, idx);
249 media_content_error("tokenize error occured");
253 if (token != NULL && STRING_VALID(token->str)) {
254 *token_list = g_list_append(*token_list, token);
257 media_content_error("tokenize error occured");
262 return MEDIA_CONTENT_ERROR_NONE;
265 static int __tokenize(GList **token_list, const char *str)
270 media_content_retvm_if(!STRING_VALID(str), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Parameter string is invalid");
272 const char *tmp = str;
273 const char *dst_ptr = str + strlen(str);
275 for (idx = 0; (*(tmp+idx)) && (tmp < dst_ptr); idx++) {
276 /* media_content_debug("[%d] '%c'", idx, tmp[idx]); */
277 if (tmp[idx] == media_token[0][0]) { /* " " */
278 if (idx == 0) { /* ignore the space. */
284 token_t *token = (token_t*)calloc(1, sizeof(token_t));
285 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
287 token->type = UNKNOWN_TYPE;
288 token->str = (char *)calloc(idx+1, sizeof(char));
289 if (token->str == NULL) {
291 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
294 strncpy(token->str, tmp, idx);
295 /* media_content_debug("type : [%d] str : [%s]", token->type, token->str); */
296 *token_list = g_list_append(*token_list, token);
297 tmp = tmp +idx + strlen(media_token[0]);
299 } else if (tmp[idx] == media_token[1][0]) { /* " \" " */
302 for (j = idx+1; tmp[j]; j++) { /* find next quotation */
303 if (tmp[j] == media_token[1][0] && tmp[j+1] == media_token[1][0]) {
307 if (tmp[j] == media_token[1][0]) {
308 token_t *token = (token_t*)calloc(1, sizeof(token_t));
309 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
311 token->str = (char*) calloc(j+1+1, sizeof(char));
312 if (token->str == NULL) {
314 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
317 token->type = STRING_TYPE;
318 strncpy(token->str, tmp, j+1);
319 /* media_content_debug("type : [%d] str : [%s], j : %d", token->type, token->str, j); */
320 *token_list = g_list_append(*token_list, token);
321 tmp = tmp + strlen(token->str);
328 if (!flag && *tmp != '\0' && tmp[j] == '\0') {
329 token_t *token = (token_t*)calloc(1, sizeof(token_t));
330 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
332 token->str = (char *) calloc(j+1, sizeof(char));
333 if (token->str == NULL) {
335 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
338 token->type = UNKNOWN_TYPE;
339 strncpy(token->str, tmp, j);
340 /* media_content_debug("type : [%d] str : [%s]", token->type, token->str);*/
341 *token_list = g_list_append(*token_list, token);
342 tmp = tmp +strlen(token->str);
345 } else if (tmp[idx] == media_token[2][0]) { /* " \' "*/
348 for (j = idx+1; tmp[j]; j++) {
349 if (tmp[j] == media_token[2][0] && tmp[j+1] == media_token[2][0]) {
353 if (tmp[j] == media_token[2][0]) {
354 token_t *token = (token_t*)calloc(1, sizeof(token_t));
355 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
357 token->str = (char *) calloc(j+1+1, sizeof(char));
358 if (token->str == NULL) {
360 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
363 token->type = STRING_TYPE;
364 strncpy(token->str, tmp, j+1);
365 /* media_content_debug("type : [%d] str : [%s]", token->type, token->str); */
366 *token_list = g_list_append(*token_list, token);
367 tmp = tmp + strlen(token->str);
374 if (!flag && *tmp != '\0' && tmp[j] == '\0') {
375 token_t *token = (token_t*)calloc(1, sizeof(token_t));
376 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
378 token->str = (char*) calloc(j+1, sizeof(char));
379 if (token->str == NULL) {
381 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
384 token->type = UNKNOWN_TYPE;
385 strncpy(token->str, tmp, j);
386 /* media_content_debug("type : [%d] str : [%s]", token->type, token->str); */
387 *token_list = g_list_append(*token_list, token);
388 tmp = tmp + strlen(token->str);
391 } else if (tmp[idx] == media_token[3][0]) { /* "(" */
393 token_t *token = (token_t*)calloc(1, sizeof(token_t));
394 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
396 ret = __tokenize_string(token, tmp, idx);
399 media_content_error("tokenize error occured");
402 *token_list = g_list_append(*token_list, token);
406 token_t *token = (token_t*)calloc(1, sizeof(token_t));
407 int size = __tokenize_operator(token, tmp, 3);
409 if (token != NULL && STRING_VALID(token->str)) {
410 *token_list = g_list_append(*token_list, token);
415 media_content_error("tokenize error occured");
419 } else if (tmp[idx] == media_token[4][0]) { /* ")" */
421 token_t *token = (token_t*)calloc(1, sizeof(token_t));
422 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
424 ret = __tokenize_string(token, tmp, idx);
427 media_content_error("tokenize error occured");
430 *token_list = g_list_append(*token_list, token);
434 token_t *token = (token_t*)calloc(1, sizeof(token_t));
435 int size = __tokenize_operator(token, tmp, 4);
437 if (token != NULL && STRING_VALID(token->str)) {
438 *token_list = g_list_append(*token_list, token);
443 media_content_error("tokenize error occured");
447 } else if (tmp[idx] == media_token[5][0]) { /* "=" */
449 token_t *token = (token_t*)calloc(1, sizeof(token_t));
450 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
452 ret = __tokenize_string(token, tmp, idx);
455 media_content_error("tokenize error occured");
458 *token_list = g_list_append(*token_list, token);
462 token_t *token = (token_t*)calloc(1, sizeof(token_t));
463 int size = __tokenize_operator(token, tmp, 5);
465 if (token != NULL && STRING_VALID(token->str)) {
466 *token_list = g_list_append(*token_list, token);
471 media_content_error("tokenize error occured");
475 } else if (tmp[idx] == media_token[6][0] && tmp[idx+1] == media_token[6][1]) { /* "<=", */
477 token_t *token = (token_t*)calloc(1, sizeof(token_t));
478 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
480 ret = __tokenize_string(token, tmp, idx);
483 media_content_error("tokenize error occured");
486 *token_list = g_list_append(*token_list, token);
490 token_t *token = (token_t*)calloc(1, sizeof(token_t));
491 int size = __tokenize_operator(token, tmp, 6);
493 if (token != NULL && STRING_VALID(token->str)) {
494 *token_list = g_list_append(*token_list, token);
499 media_content_error("tokenize error occured");
503 } else if (tmp[idx] == media_token[7][0]) { /* "<", */
505 token_t *token = (token_t*)calloc(1, sizeof(token_t));
506 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
508 ret = __tokenize_string(token, tmp, idx);
511 media_content_error("tokenize error occured");
514 *token_list = g_list_append(*token_list, token);
518 token_t *token = (token_t*)calloc(1, sizeof(token_t));
519 int size = __tokenize_operator(token, tmp, 7);
521 if (token != NULL && STRING_VALID(token->str)) {
522 *token_list = g_list_append(*token_list, token);
527 media_content_error("tokenize error occured");
531 } else if (tmp[idx] == media_token[8][0] && tmp[idx+1] == media_token[8][1]) { /* ">=", */
533 token_t *token = (token_t*)calloc(1, sizeof(token_t));
534 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
536 ret = __tokenize_string(token, tmp, idx);
539 media_content_error("tokenize error occured");
542 *token_list = g_list_append(*token_list, token);
546 token_t *token = (token_t*)calloc(1, sizeof(token_t));
547 int size = __tokenize_operator(token, tmp, 8);
549 if (token != NULL && STRING_VALID(token->str)) {
550 *token_list = g_list_append(*token_list, token);
555 media_content_error("tokenize error occured");
559 } else if (tmp[idx] == media_token[9][0]) { /* ">", */
561 token_t *token = (token_t*)calloc(1, sizeof(token_t));
562 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
564 ret = __tokenize_string(token, tmp, idx);
567 media_content_error("tokenize error occured");
570 *token_list = g_list_append(*token_list, token);
574 token_t *token = (token_t*)calloc(1, sizeof(token_t));
575 int size = __tokenize_operator(token, tmp, 9);
577 if (token != NULL && STRING_VALID(token->str)) {
578 *token_list = g_list_append(*token_list, token);
583 media_content_error("tokenize error occured");
586 } else if (tmp[idx] == media_token[10][0] && tmp[idx+1] == media_token[10][1]) { /* "!=", */
588 token_t *token = (token_t*)calloc(1, sizeof(token_t));
589 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
591 ret = __tokenize_string(token, tmp, idx);
594 media_content_error("tokenize error occured");
597 *token_list = g_list_append(*token_list, token);
601 token_t *token = (token_t*)calloc(1, sizeof(token_t));
602 int size = __tokenize_operator(token, tmp, 10);
604 if (token != NULL && STRING_VALID(token->str)) {
605 *token_list = g_list_append(*token_list, token);
610 media_content_error("tokenize error occured");
616 if (*tmp) { /* remained string */
617 token_t *token = (token_t*)calloc(1, sizeof(token_t));
618 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
620 ret = __tokenize_string(token, tmp, idx);
623 media_content_error("tokenize error occured");
627 if (token != NULL && STRING_VALID(token->str)) {
628 *token_list = g_list_append(*token_list, token);
631 media_content_error("tokenize error occured");
636 return MEDIA_CONTENT_ERROR_NONE;
639 int _media_filter_attribute_create(attribute_h *attr)
641 int ret = MEDIA_CONTENT_ERROR_NONE;
643 media_content_retvm_if(attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
645 attribute_s *_attr = (attribute_s*)calloc(1, sizeof(attribute_s));
646 media_content_retvm_if(_attr == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
648 _attr->attr_map = g_hash_table_new(g_str_hash, g_str_equal);
649 *attr = (attribute_h)_attr;
654 int _media_filter_attribute_add(attribute_h attr, const char *user_attr, const char *platform_attr)
656 int ret = MEDIA_CONTENT_ERROR_NONE;
658 char *_platform = NULL;
659 attribute_s *_attr = (attribute_s*)attr;
661 media_content_retvm_if(_attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
663 if (STRING_VALID(user_attr) && STRING_VALID(platform_attr)) {
664 _user = g_strdup(user_attr);
665 media_content_retvm_if(_user == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
667 _platform = g_strdup(platform_attr);
668 if (_platform == NULL) {
670 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
671 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
674 g_hash_table_insert(_attr->attr_map, _user, _platform);
676 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
677 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
683 int _media_filter_attribute_destory(attribute_h attr)
685 int ret = MEDIA_CONTENT_ERROR_NONE;
686 attribute_s *_attr = (attribute_s*)attr;
688 media_content_retvm_if(_attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
690 if (_attr->attr_map != NULL) {
691 g_hash_table_foreach(_attr->attr_map, __filter_attribute_free_value, NULL);
692 g_hash_table_destroy(_attr->attr_map);
700 int _media_filter_attribute_generate(attribute_h attr, char *condition, media_content_collation_e collate_type, char **generated_condition)
702 unsigned int idx = 0;
703 GList *token_list = NULL;
705 int ret = MEDIA_CONTENT_ERROR_NONE;
706 int total_str_size = 0;
709 media_content_retvm_if(condition == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid condition");
710 media_content_retvm_if(generated_condition == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid generated_condition");
711 media_content_retvm_if(attr == NULL, MEDIA_CONTENT_ERROR_DB_FAILED, "DB field mapping table doesn't exist. Check db connection");
713 if (__tokenize(&token_list, condition) < 0) {
714 media_content_error("INVALID_PARAMETER(0x%08x):Invalid the condition", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
715 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
718 for (idx = 0; idx < g_list_length(token_list); idx++) {
719 token = (token_t*)g_list_nth_data(token_list, idx);
721 if (token->type == UNKNOWN_TYPE) {
722 char *replace_str = __media_filter_replace_attr(attr, token->str);
723 if (STRING_VALID(replace_str)) {
724 SAFE_FREE(token->str);
725 token->str = replace_str;
729 total_str_size += strlen(token->str)+1;
730 /* media_content_debug("[%d][type:%d]:%s", idx, token->type, token->str); */
733 /* make the statment */
734 size = total_str_size + COLLATE_STR_SIZE + 1;
735 *generated_condition = (char*)calloc(size, sizeof(char));
737 for (idx = 0; idx < g_list_length(token_list); idx++) {
738 token = (token_t*)g_list_nth_data(token_list, idx);
740 if ((token != NULL) && STRING_VALID(token->str)) {
741 SAFE_STRLCAT(*generated_condition, token->str, size);
742 SAFE_STRLCAT(*generated_condition, SPACE, size);
744 SAFE_FREE(token->str);
749 if (collate_type == MEDIA_CONTENT_COLLATE_NOCASE || collate_type == MEDIA_CONTENT_COLLATE_RTRIM || collate_type == MEDIA_CONTENT_COLLATE_LOCALIZED) {
750 SAFE_STRLCAT(*generated_condition, "COLLATE ", size);
751 SAFE_STRLCAT(*generated_condition, __get_collate_str(collate_type), size);
752 SAFE_STRLCAT(*generated_condition, SPACE, size);
755 /* Always close in here (condition collate option issue)*/
756 SAFE_STRLCAT(*generated_condition, QUERY_KEYWORD_BRACKET, size);
758 /* media_content_debug("statement : %s(%d) (total:%d)", *generated_condition, strlen(*generated_condition), total_str_size); */
759 media_content_sec_debug("Condition : %s", *generated_condition);
761 /* if(*generated_condition != NULL)
764 if (token_list != NULL)
765 g_list_free(token_list);
770 int _media_filter_attribute_option_generate(attribute_h attr, filter_h filter, char **generated_option)
772 int ret = MEDIA_CONTENT_ERROR_NONE;
773 filter_s *_filter = NULL;
774 char option_query[DEFAULT_QUERY_SIZE] = {0, };
775 char condition[DEFAULT_QUERY_SIZE] = {0, };
778 media_content_retvm_if(attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
779 media_content_retvm_if(filter == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid filter");
781 _filter = (filter_s*)filter;
783 memset(option_query, 0x00, sizeof(option_query));
786 if (STRING_VALID(_filter->order_keyword)) {
787 if ((_filter->order_type == MEDIA_CONTENT_ORDER_ASC) || (_filter->order_type == MEDIA_CONTENT_ORDER_DESC)) {
788 unsigned int idx = 0;
789 int total_str_size = 0;
790 GList *token_list = NULL;
794 if (__tokenize_attribute(&token_list, _filter->order_keyword) < 0) {
795 media_content_error("INVALID_PARAMETER(0x%08x):Invalid the condition", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
796 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
799 for (idx = 0; idx < g_list_length(token_list); idx++) {
800 token = (token_t*)g_list_nth_data(token_list, idx);
802 if (token->type == UNKNOWN_TYPE) {
803 char *replace_str = __media_filter_replace_attr(attr, token->str);
804 if (STRING_VALID(replace_str)) {
805 attr_str = (char*)calloc(strlen(replace_str) + COLLATE_STR_SIZE + 1, sizeof(char));
806 if (attr_str == NULL) {
807 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
808 SAFE_FREE(replace_str);
812 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)
813 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));
815 snprintf(attr_str, strlen(replace_str) + COLLATE_STR_SIZE + 1, "%s %s", replace_str, __get_order_str(_filter->order_type));
817 SAFE_FREE(token->str);
818 token->str = attr_str;
819 SAFE_FREE(replace_str);
821 media_content_error("There is no matched db field for %s", token->str);
825 total_str_size += strlen(token->str) + 1;
826 /* media_content_debug("[%d][type:%d]:%s", idx, token->type, token->str); */
829 /* make the statment */
830 char *generated_condition = NULL;
831 size = total_str_size + COLLATE_STR_SIZE + 1;
832 generated_condition = (char*)calloc(size, sizeof(char));
834 for (idx = 0; idx < g_list_length(token_list); idx++) {
835 token = (token_t*)g_list_nth_data(token_list, idx);
837 if ((token != NULL) && STRING_VALID(token->str)) {
838 /* media_content_debug("[%d] %s", idx, token->str); */
839 SAFE_STRLCAT(generated_condition, token->str, size);
840 SAFE_STRLCAT(generated_condition, SPACE, size);
842 SAFE_FREE(token->str);
847 snprintf(condition, sizeof(condition), "ORDER BY %s", generated_condition);
848 SAFE_STRLCAT(option_query, condition, sizeof(option_query));
850 if (token_list != NULL)
851 g_list_free(token_list);
853 SAFE_FREE(generated_condition);
855 SAFE_STRLCAT(option_query, _filter->order_keyword, sizeof(option_query));
856 media_content_error("option_query [%s]", option_query);
861 SAFE_STRLCAT(option_query, SPACE, sizeof(option_query));
862 snprintf(condition, sizeof(condition), "LIMIT %d, %d", _filter->offset, _filter->count);
863 SAFE_STRLCAT(option_query, condition, sizeof(option_query));
865 if (STRING_VALID(option_query))
866 *generated_option = strdup(option_query);
868 *generated_option = NULL;
873 int media_filter_create(filter_h *filter)
875 int ret = MEDIA_CONTENT_ERROR_NONE;
877 media_content_retvm_if(filter == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid filter");
879 filter_s *_filter = (filter_s*)calloc(1, sizeof(filter_s));
880 media_content_retvm_if(_filter == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
882 _filter->storage_id = NULL;
883 _filter->condition = NULL;
884 _filter->order_keyword = NULL;
885 _filter->order_type = -1;
886 _filter->condition_collate_type = MEDIA_CONTENT_COLLATE_DEFAULT;
887 _filter->order_collate_type = MEDIA_CONTENT_COLLATE_DEFAULT;
888 _filter->offset = -1;
891 *filter = (filter_h)_filter;
896 int media_filter_destroy(filter_h filter)
898 int ret = MEDIA_CONTENT_ERROR_NONE;
899 filter_s *_filter = (filter_s*)filter;
902 SAFE_FREE(_filter->storage_id);
903 SAFE_FREE(_filter->condition);
904 SAFE_FREE(_filter->order_keyword);
907 ret = MEDIA_CONTENT_ERROR_NONE;
909 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
910 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
916 int media_filter_set_offset(filter_h filter, int offset, int count)
918 int ret = MEDIA_CONTENT_ERROR_NONE;
919 filter_s *_filter = (filter_s*)filter;
921 if (_filter != NULL) {
922 _filter->offset = offset;
923 _filter->count = count;
925 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
926 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
932 int media_filter_set_condition(filter_h filter, const char *condition, media_content_collation_e collate_type)
934 int ret = MEDIA_CONTENT_ERROR_NONE;
935 filter_s *_filter = (filter_s*)filter;
937 if ((_filter != NULL) && STRING_VALID(condition)
938 && ((collate_type >= MEDIA_CONTENT_COLLATE_DEFAULT) && (collate_type <= MEDIA_CONTENT_COLLATE_LOCALIZED))) {
939 if (STRING_VALID(_filter->condition))
940 SAFE_FREE(_filter->condition);
942 char new_condition[MAX_QUERY_SIZE] = {0, };
943 memset(new_condition, 0, sizeof(new_condition));
944 ret = _media_content_replace_path_in_condition(condition, new_condition, TRUE);
945 media_content_retvm_if(!STRING_VALID(new_condition), MEDIA_CONTENT_ERROR_INVALID_OPERATION, "path replacement failed");
947 _filter->condition = strdup(new_condition);
948 media_content_retvm_if(_filter->condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
950 media_content_sec_debug("Condition string : %s", _filter->condition);
952 _filter->condition_collate_type = collate_type;
954 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
955 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
961 int media_filter_set_order(filter_h filter, media_content_order_e order_type, const char *order_keyword, media_content_collation_e collate_type)
963 int ret = MEDIA_CONTENT_ERROR_NONE;
964 filter_s *_filter = (filter_s*)filter;
966 if ((_filter != NULL) && STRING_VALID(order_keyword)
967 && ((order_type == MEDIA_CONTENT_ORDER_ASC) || (order_type == MEDIA_CONTENT_ORDER_DESC))
968 && ((collate_type >= MEDIA_CONTENT_COLLATE_DEFAULT) && (collate_type <= MEDIA_CONTENT_COLLATE_LOCALIZED))) {
969 SAFE_FREE(_filter->order_keyword);
971 if (STRING_VALID(order_keyword)) {
972 _filter->order_keyword = strdup(order_keyword);
973 media_content_retvm_if(_filter->order_keyword == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
975 _filter->order_type = order_type;
976 _filter->order_collate_type = collate_type;
978 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
979 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
982 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
983 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
989 int media_filter_set_storage(filter_h filter, const char *storage_id)
991 int ret = MEDIA_CONTENT_ERROR_NONE;
992 filter_s *_filter = (filter_s*)filter;
994 if ((_filter != NULL) && STRING_VALID(storage_id)) {
995 if (STRING_VALID(_filter->storage_id))
996 SAFE_FREE(_filter->storage_id);
998 _filter->storage_id = strdup(storage_id);
999 media_content_retvm_if(_filter->storage_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1001 media_content_sec_debug("storage_id : %s", _filter->storage_id);
1003 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1004 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1010 int media_filter_get_offset(filter_h filter, int *offset, int *count)
1012 int ret = MEDIA_CONTENT_ERROR_NONE;
1013 filter_s *_filter = (filter_s*)filter;
1016 *offset = _filter->offset;
1017 *count = _filter->count;
1019 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1020 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1026 int media_filter_get_condition(filter_h filter, char **condition, media_content_collation_e *collate_type)
1028 int ret = MEDIA_CONTENT_ERROR_NONE;
1029 filter_s *_filter = (filter_s*)filter;
1032 if (STRING_VALID(_filter->condition)) {
1033 char new_condition[MAX_QUERY_SIZE] = {0, };
1034 memset(new_condition, 0, sizeof(new_condition));
1035 ret = _media_content_replace_path_in_condition(_filter->condition, new_condition, FALSE);
1036 media_content_retvm_if(!STRING_VALID(new_condition), MEDIA_CONTENT_ERROR_INVALID_OPERATION, "path replacement failed");
1038 *condition = strdup(new_condition);
1039 media_content_retvm_if(*condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1044 *collate_type = _filter->condition_collate_type;
1046 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1047 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1053 int media_filter_get_order(filter_h filter, media_content_order_e* order_type, char **order_keyword, media_content_collation_e *collate_type)
1055 int ret = MEDIA_CONTENT_ERROR_NONE;
1056 filter_s *_filter = (filter_s*)filter;
1059 if (STRING_VALID(_filter->order_keyword)) {
1060 *order_keyword = strdup(_filter->order_keyword);
1061 media_content_retvm_if(*order_keyword == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1063 *order_keyword = NULL;
1066 *order_type = _filter->order_type;
1067 *collate_type = _filter->order_collate_type;
1069 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1070 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1076 int media_filter_get_storage(filter_h filter, char **storage_id)
1078 int ret = MEDIA_CONTENT_ERROR_NONE;
1079 filter_s *_filter = (filter_s*)filter;
1082 if (STRING_VALID(_filter->storage_id)) {
1083 *storage_id = strdup(_filter->storage_id);
1084 media_content_retvm_if(*storage_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1089 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1090 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;