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 {
42 #define UNKNOWN_TYPE 1000
43 #define STRING_TYPE 100
45 static char *__get_order_str(media_content_order_e order_enum);
46 static char *__get_collate_str(media_content_collation_e collate_type);
47 static void __filter_attribute_free_value(gpointer key, gpointer value, gpointer user_data);
48 static char *__media_filter_replace_attr(attribute_h attr, char *name);
49 static int __tokenize_operator(token_t *token, const char *str, int op_type);
50 static int __tokenize(GList **token_list, const char *str);
52 static bool __is_pinyin_needed(void)
55 const char *china = "zh_CN";
56 const char *hongkong = "zh_HK";
60 bool pinyin_support = FALSE;
61 media_svc_check_pinyin_support(&pinyin_support);
63 /*Check Language Setting*/
64 lang = vconf_get_str(VCONFKEY_LANGSET);
65 media_content_retvm_if(lang == NULL, ret, "Fail to get string of language set");
67 if ((strncmp(china, lang, strlen(china)) == 0) ||
68 (strncmp(hongkong, lang, strlen(hongkong)) == 0)) {
78 static char *__get_order_str(media_content_order_e order_enum)
81 case MEDIA_CONTENT_ORDER_ASC:
83 case MEDIA_CONTENT_ORDER_DESC:
84 return (char *)"DESC";
90 static char *__get_collate_str(media_content_collation_e collate_type)
92 switch (collate_type) {
93 case MEDIA_CONTENT_COLLATE_NOCASE:
94 return (char *)"NOCASE";
95 case MEDIA_CONTENT_COLLATE_RTRIM:
96 return (char *)"RTRIM";
97 case MEDIA_CONTENT_COLLATE_LOCALIZED:
98 if (__is_pinyin_needed())
99 return (char *)"NOCASE";
101 return (char *)"localized";
102 default: return (char *)" ";
106 static void __filter_attribute_free_value(gpointer key, gpointer value, gpointer user_data)
112 static char *__media_filter_replace_attr(attribute_h attr, char *name)
114 char *key_temp = NULL;
115 char *generated_value = NULL;
116 attribute_s *_attr = (attribute_s *)attr;
118 if (!g_hash_table_lookup_extended(_attr->attr_map, name, (gpointer)&key_temp, (gpointer)&generated_value)) {
119 /* can't find the value */
120 /* media_content_error("NOT_FOUND_VALUE(%s)", name); */
124 if (STRING_VALID(generated_value))
125 return strdup(generated_value);
127 media_content_error("__media_filter_replace_attr fail");
132 static int __tokenize_operator(token_t *token, const char *str, int op_type)
135 const char *tmp = str;
137 if (token != NULL && STRING_VALID(tmp)) {
138 token->type = op_type;
139 int token_size = strlen(media_token[op_type]);
140 media_content_retvm_if(token_size == 0, -1, "Invalid token_size. op_type[%d]", op_type);
142 token->str = (char*)calloc(token_size+1, sizeof(char));
143 media_content_retvm_if(token->str == NULL, -1, "OUT_OF_MEMORY");
145 strncpy(token->str, tmp, token_size);
146 /*media_content_debug("type : [%d] str : [%s]", token->type, token->str);*/
155 static int __tokenize_string(token_t *token, const char *str, int size)
158 const char *tmp = str;
160 if (token != NULL && STRING_VALID(tmp) && size > 0) {
161 token->str = (char*)calloc(size+1, sizeof(char));
162 media_content_retvm_if(token->str == NULL, -1, "OUT_OF_MEMORY");
164 token->type = UNKNOWN_TYPE;
165 strncpy(token->str, tmp, size);
166 /*media_content_debug("type : [%d] str : [%s]", token->type, token->str);*/
174 static int __tokenize_attribute(GList **token_list, const char *str)
179 media_content_retvm_if(!STRING_VALID(str), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Parameter string is invalid");
181 const char *tmp = str;
182 const char *dst_ptr = str + strlen(str);
184 for (idx = 0; (*(tmp+idx)) && (tmp < dst_ptr); idx++) {
185 /*media_content_debug("[%d] '%c'", idx, tmp[idx]);*/
186 if (tmp[idx] == ' ') { /* " " */
187 if (idx == 0) { /* ignore the space. */
192 token_t *token = (token_t*)calloc(1, sizeof(token_t));
193 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
195 token->type = UNKNOWN_TYPE;
196 token->str = (char*)calloc(idx+1, sizeof(char));
197 if (token->str == NULL) {
199 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
202 strncpy(token->str, tmp, idx);
203 /*media_content_debug("type : [%d] str : [%s]", token->type, token->str);*/
204 *token_list = g_list_append(*token_list, token);
205 tmp = tmp +idx + strlen(media_token[0]);
207 } else if (tmp[idx] == ',') { /* " , " */
209 token_t *token = (token_t*)calloc(1, sizeof(token_t));
210 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
212 ret = __tokenize_string(token, tmp, idx);
215 media_content_error("tokenize error occured");
218 *token_list = g_list_append(*token_list, token);
223 token_t *token = (token_t*)calloc(1, sizeof(token_t));
224 int size = __tokenize_operator(token, tmp, 3);
226 if (token != NULL && STRING_VALID(token->str)) {
227 *token_list = g_list_append(*token_list, token);
232 media_content_error("tokenize error occured");
238 if (*tmp) { /* remained string */
239 token_t *token = (token_t*)calloc(1, sizeof(token_t));
240 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
242 ret = __tokenize_string(token, tmp, idx);
245 media_content_error("tokenize error occured");
249 if (token != NULL && STRING_VALID(token->str)) {
250 *token_list = g_list_append(*token_list, token);
253 media_content_error("tokenize error occured");
258 return MEDIA_CONTENT_ERROR_NONE;
261 static int __tokenize(GList **token_list, const char *str)
266 media_content_retvm_if(!STRING_VALID(str), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Parameter string is invalid");
268 const char *tmp = str;
269 const char *dst_ptr = str + strlen(str);
271 for (idx = 0; (*(tmp+idx)) && (tmp < dst_ptr); idx++) {
272 /* media_content_debug("[%d] '%c'", idx, tmp[idx]); */
273 if (tmp[idx] == media_token[0][0]) { /* " " */
274 if (idx == 0) { /* ignore the space. */
280 token_t *token = (token_t*)calloc(1, sizeof(token_t));
281 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
283 token->type = UNKNOWN_TYPE;
284 token->str = (char *)calloc(idx+1, sizeof(char));
285 if (token->str == NULL) {
287 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
290 strncpy(token->str, tmp, idx);
291 /* media_content_debug("type : [%d] str : [%s]", token->type, token->str); */
292 *token_list = g_list_append(*token_list, token);
293 tmp = tmp +idx + strlen(media_token[0]);
295 } else if (tmp[idx] == media_token[1][0]) { /* " \" " */
298 for (j = idx+1; tmp[j]; j++) { /* find next quotation */
299 if (tmp[j] == media_token[1][0] && tmp[j+1] == media_token[1][0]) {
303 if (tmp[j] == media_token[1][0]) {
304 token_t *token = (token_t*)calloc(1, sizeof(token_t));
305 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
307 token->str = (char*) calloc(j+1+1, sizeof(char));
308 if (token->str == NULL) {
310 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
313 token->type = STRING_TYPE;
314 strncpy(token->str, tmp, j+1);
315 /* media_content_debug("type : [%d] str : [%s], j : %d", token->type, token->str, j); */
316 *token_list = g_list_append(*token_list, token);
317 tmp = tmp + strlen(token->str);
324 if (!flag && *tmp != '\0' && tmp[j] == '\0') {
325 token_t *token = (token_t*)calloc(1, sizeof(token_t));
326 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
328 token->str = (char *) calloc(j+1, sizeof(char));
329 if (token->str == NULL) {
331 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
334 token->type = UNKNOWN_TYPE;
335 strncpy(token->str, tmp, j);
336 /* media_content_debug("type : [%d] str : [%s]", token->type, token->str);*/
337 *token_list = g_list_append(*token_list, token);
338 tmp = tmp +strlen(token->str);
341 } else if (tmp[idx] == media_token[2][0]) { /* " \' "*/
344 for (j = idx+1; tmp[j]; j++) {
345 if (tmp[j] == media_token[2][0] && tmp[j+1] == media_token[2][0]) {
349 if (tmp[j] == media_token[2][0]) {
350 token_t *token = (token_t*)calloc(1, sizeof(token_t));
351 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
353 token->str = (char *) calloc(j+1+1, sizeof(char));
354 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]", token->type, token->str); */
362 *token_list = g_list_append(*token_list, token);
363 tmp = tmp + strlen(token->str);
370 if (!flag && *tmp != '\0' && tmp[j] == '\0') {
371 token_t *token = (token_t*)calloc(1, sizeof(token_t));
372 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
374 token->str = (char*) calloc(j+1, sizeof(char));
375 if (token->str == NULL) {
377 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
380 token->type = UNKNOWN_TYPE;
381 strncpy(token->str, tmp, j);
382 /* media_content_debug("type : [%d] str : [%s]", token->type, token->str); */
383 *token_list = g_list_append(*token_list, token);
384 tmp = tmp + strlen(token->str);
387 } else if (tmp[idx] == media_token[3][0]) { /* "(" */
389 token_t *token = (token_t*)calloc(1, sizeof(token_t));
390 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
392 ret = __tokenize_string(token, tmp, idx);
395 media_content_error("tokenize error occured");
398 *token_list = g_list_append(*token_list, token);
402 token_t *token = (token_t*)calloc(1, sizeof(token_t));
403 int size = __tokenize_operator(token, tmp, 3);
405 if (token != NULL && STRING_VALID(token->str)) {
406 *token_list = g_list_append(*token_list, token);
411 media_content_error("tokenize error occured");
415 } else if (tmp[idx] == media_token[4][0]) { /* ")" */
417 token_t *token = (token_t*)calloc(1, sizeof(token_t));
418 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
420 ret = __tokenize_string(token, tmp, idx);
423 media_content_error("tokenize error occured");
426 *token_list = g_list_append(*token_list, token);
430 token_t *token = (token_t*)calloc(1, sizeof(token_t));
431 int size = __tokenize_operator(token, tmp, 4);
433 if (token != NULL && STRING_VALID(token->str)) {
434 *token_list = g_list_append(*token_list, token);
439 media_content_error("tokenize error occured");
443 } else if (tmp[idx] == media_token[5][0]) { /* "=" */
445 token_t *token = (token_t*)calloc(1, sizeof(token_t));
446 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
448 ret = __tokenize_string(token, tmp, idx);
451 media_content_error("tokenize error occured");
454 *token_list = g_list_append(*token_list, token);
458 token_t *token = (token_t*)calloc(1, sizeof(token_t));
459 int size = __tokenize_operator(token, tmp, 5);
461 if (token != NULL && STRING_VALID(token->str)) {
462 *token_list = g_list_append(*token_list, token);
467 media_content_error("tokenize error occured");
471 } else if (tmp[idx] == media_token[6][0] && tmp[idx+1] == media_token[6][1]) { /* "<=", */
473 token_t *token = (token_t*)calloc(1, sizeof(token_t));
474 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
476 ret = __tokenize_string(token, tmp, idx);
479 media_content_error("tokenize error occured");
482 *token_list = g_list_append(*token_list, token);
486 token_t *token = (token_t*)calloc(1, sizeof(token_t));
487 int size = __tokenize_operator(token, tmp, 6);
489 if (token != NULL && STRING_VALID(token->str)) {
490 *token_list = g_list_append(*token_list, token);
495 media_content_error("tokenize error occured");
499 } else if (tmp[idx] == media_token[7][0]) { /* "<", */
501 token_t *token = (token_t*)calloc(1, sizeof(token_t));
502 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
504 ret = __tokenize_string(token, tmp, idx);
507 media_content_error("tokenize error occured");
510 *token_list = g_list_append(*token_list, token);
514 token_t *token = (token_t*)calloc(1, sizeof(token_t));
515 int size = __tokenize_operator(token, tmp, 7);
517 if (token != NULL && STRING_VALID(token->str)) {
518 *token_list = g_list_append(*token_list, token);
523 media_content_error("tokenize error occured");
527 } else if (tmp[idx] == media_token[8][0] && tmp[idx+1] == media_token[8][1]) { /* ">=", */
529 token_t *token = (token_t*)calloc(1, sizeof(token_t));
530 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
532 ret = __tokenize_string(token, tmp, idx);
535 media_content_error("tokenize error occured");
538 *token_list = g_list_append(*token_list, token);
542 token_t *token = (token_t*)calloc(1, sizeof(token_t));
543 int size = __tokenize_operator(token, tmp, 8);
545 if (token != NULL && STRING_VALID(token->str)) {
546 *token_list = g_list_append(*token_list, token);
551 media_content_error("tokenize error occured");
555 } else if (tmp[idx] == media_token[9][0]) { /* ">", */
557 token_t *token = (token_t*)calloc(1, sizeof(token_t));
558 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
560 ret = __tokenize_string(token, tmp, idx);
563 media_content_error("tokenize error occured");
566 *token_list = g_list_append(*token_list, token);
570 token_t *token = (token_t*)calloc(1, sizeof(token_t));
571 int size = __tokenize_operator(token, tmp, 9);
573 if (token != NULL && STRING_VALID(token->str)) {
574 *token_list = g_list_append(*token_list, token);
579 media_content_error("tokenize error occured");
582 } else if (tmp[idx] == media_token[10][0] && tmp[idx+1] == media_token[10][1]) { /* "!=", */
584 token_t *token = (token_t*)calloc(1, sizeof(token_t));
585 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
587 ret = __tokenize_string(token, tmp, idx);
590 media_content_error("tokenize error occured");
593 *token_list = g_list_append(*token_list, token);
597 token_t *token = (token_t*)calloc(1, sizeof(token_t));
598 int size = __tokenize_operator(token, tmp, 10);
600 if (token != NULL && STRING_VALID(token->str)) {
601 *token_list = g_list_append(*token_list, token);
606 media_content_error("tokenize error occured");
612 if (*tmp) { /* remained string */
613 token_t *token = (token_t*)calloc(1, sizeof(token_t));
614 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
616 ret = __tokenize_string(token, tmp, idx);
619 media_content_error("tokenize error occured");
623 if (token != NULL && STRING_VALID(token->str)) {
624 *token_list = g_list_append(*token_list, token);
627 media_content_error("tokenize error occured");
632 return MEDIA_CONTENT_ERROR_NONE;
635 int _media_filter_attribute_create(attribute_h *attr)
637 int ret = MEDIA_CONTENT_ERROR_NONE;
639 media_content_retvm_if(attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
641 attribute_s *_attr = (attribute_s*)calloc(1, sizeof(attribute_s));
642 media_content_retvm_if(_attr == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
644 _attr->attr_map = g_hash_table_new(g_str_hash, g_str_equal);
645 *attr = (attribute_h)_attr;
650 int _media_filter_attribute_add(attribute_h attr, const char *user_attr, const char *platform_attr)
652 int ret = MEDIA_CONTENT_ERROR_NONE;
654 char *_platform = NULL;
655 attribute_s *_attr = (attribute_s*)attr;
657 media_content_retvm_if(_attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
659 if (STRING_VALID(user_attr) && STRING_VALID(platform_attr)) {
660 _user = g_strdup(user_attr);
661 media_content_retvm_if(_user == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
663 _platform = g_strdup(platform_attr);
664 if (_platform == NULL) {
666 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
667 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
670 g_hash_table_insert(_attr->attr_map, _user, _platform);
672 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
673 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
679 int _media_filter_attribute_destory(attribute_h attr)
681 int ret = MEDIA_CONTENT_ERROR_NONE;
682 attribute_s *_attr = (attribute_s*)attr;
684 media_content_retvm_if(_attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
686 if (_attr->attr_map != NULL) {
687 g_hash_table_foreach(_attr->attr_map, __filter_attribute_free_value, NULL);
688 g_hash_table_destroy(_attr->attr_map);
696 int _media_filter_attribute_generate(attribute_h attr, filter_h filter, char **generated_condition)
698 unsigned int idx = 0;
699 GList *token_list = NULL;
700 int ret = MEDIA_CONTENT_ERROR_NONE;
702 filter_s *_filter = NULL;
703 char tmp_condition[MAX_QUERY_SIZE] = {0, };
705 media_content_retvm_if(attr == NULL, MEDIA_CONTENT_ERROR_DB_FAILED, "DB field mapping table doesn't exist. Check db connection");
706 media_content_retvm_if(filter == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid filter");
707 media_content_retvm_if(generated_condition == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid generated_condition");
709 _filter = (filter_s*)filter;
711 media_content_retvm_if(_filter->condition == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid condition");
712 media_content_retvm_if(_filter->condition_collate_type < MEDIA_CONTENT_COLLATE_DEFAULT ||
713 _filter->condition_collate_type > MEDIA_CONTENT_COLLATE_LOCALIZED, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid condition collate");
715 if (__tokenize(&token_list, _filter->condition) < 0) {
716 media_content_error("INVALID_PARAMETER(0x%08x):Invalid the condition", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
717 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
720 for (idx = 0; idx < g_list_length(token_list); idx++) {
721 token = (token_t*)g_list_nth_data(token_list, idx);
723 if (token->type == UNKNOWN_TYPE) {
724 char *replace_str = __media_filter_replace_attr(attr, token->str);
725 if (STRING_VALID(replace_str)) {
726 SAFE_FREE(token->str);
727 token->str = replace_str;
732 /* make the statment */
733 memset(tmp_condition, 0, sizeof(tmp_condition));
734 SAFE_STRLCAT(tmp_condition, QUERY_KEYWORD_OPEN_BRACKET, sizeof(tmp_condition));
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(tmp_condition, token->str, sizeof(tmp_condition));
741 SAFE_STRLCAT(tmp_condition, QUERY_KEYWORD_SPACE, sizeof(tmp_condition));
743 SAFE_FREE(token->str);
748 /* Process for filter v1 */
749 if (_filter->is_full_condition == false && _filter->condition_collate_type != MEDIA_CONTENT_COLLATE_DEFAULT) {
750 SAFE_STRLCAT(tmp_condition, QUERY_KEYWORD_COLLATE, sizeof(tmp_condition));
751 SAFE_STRLCAT(tmp_condition, __get_collate_str(_filter->condition_collate_type), sizeof(tmp_condition));
752 SAFE_STRLCAT(tmp_condition, QUERY_KEYWORD_SPACE, sizeof(tmp_condition));
755 SAFE_STRLCAT(tmp_condition, QUERY_KEYWORD_BRACKET, sizeof(tmp_condition));
757 if (STRING_VALID(tmp_condition))
758 *generated_condition = g_strdup(tmp_condition);
760 *generated_condition = NULL;
762 media_content_sec_debug("Condition : %s", *generated_condition);
764 if (token_list != NULL) {
765 g_list_free(token_list);
772 int _media_filter_attribute_option_generate(attribute_h attr, filter_h filter, char **generated_option)
774 int ret = MEDIA_CONTENT_ERROR_NONE;
775 filter_s *_filter = NULL;
776 char query[DEFAULT_QUERY_SIZE] = {0, };
777 char option[DEFAULT_QUERY_SIZE] = {0, };
779 media_content_retvm_if(attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
780 media_content_retvm_if(filter == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid filter");
782 _filter = (filter_s*)filter;
784 memset(query, 0x00, sizeof(query));
787 if (STRING_VALID(_filter->order_keyword)) {
788 if ((_filter->order_type == MEDIA_CONTENT_ORDER_ASC) || (_filter->order_type == MEDIA_CONTENT_ORDER_DESC)) {
789 unsigned int idx = 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);
826 /* make the statment */
827 SAFE_STRLCAT(query, QUERY_KEYWORD_ORDER_BY, sizeof(query));
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 SAFE_STRLCAT(query, token->str, sizeof(query));
834 SAFE_STRLCAT(query, QUERY_KEYWORD_SPACE, sizeof(query));
836 SAFE_FREE(token->str);
841 if (token_list != NULL) {
842 g_list_free(token_list);
846 SAFE_STRLCAT(query, _filter->order_keyword, sizeof(query));
851 SAFE_STRLCAT(query, QUERY_KEYWORD_SPACE, sizeof(query));
853 memset(option, 0, sizeof(option));
854 snprintf(option, sizeof(option), "%s %d, %d", QUERY_KEYWORD_LIMIT, _filter->offset, _filter->count);
856 SAFE_STRLCAT(query, option, sizeof(query));
858 if (STRING_VALID(query)) {
859 *generated_option = g_strdup(query);
860 media_content_sec_debug("Option : %s", *generated_option);
862 *generated_option = NULL;
868 int _media_filter_attribute_option_generate_with_full_query(attribute_h attr, filter_h filter, char **generated_option)
870 unsigned int idx = 0;
871 GList *token_list = NULL;
872 int ret = MEDIA_CONTENT_ERROR_NONE;
874 filter_s * _filter = NULL;
875 char query[DEFAULT_QUERY_SIZE] = {0, };
876 char option[DEFAULT_QUERY_SIZE] = {0, };
878 media_content_retvm_if(attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
879 media_content_retvm_if(filter == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid filter");
880 media_content_retvm_if(generated_option == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid generated_option");
882 _filter = (filter_s*)filter;
884 memset(query, 0, sizeof(query));
887 if (STRING_VALID(_filter->order_keyword)) {
888 if (__tokenize_attribute(&token_list, _filter->order_keyword) < 0) {
889 media_content_error("INVALID_PARAMETER(0x%08x):Invalid the order", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
890 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
893 for (idx = 0; idx < g_list_length(token_list); idx++) {
894 token = (token_t*)g_list_nth_data(token_list, idx);
896 if (token->type == UNKNOWN_TYPE) {
897 char *replace_str = __media_filter_replace_attr(attr, token->str);
898 if (STRING_VALID(replace_str)) {
899 SAFE_FREE(token->str);
900 token->str = replace_str;
905 /* make the statment */
906 SAFE_STRLCAT(query, QUERY_KEYWORD_ORDER_BY, sizeof(query));
908 for (idx = 0; idx < g_list_length(token_list); idx++) {
909 token = (token_t*)g_list_nth_data(token_list, idx);
911 if ((token != NULL) && STRING_VALID(token->str)) {
912 SAFE_STRLCAT(query, token->str, sizeof(query));
913 SAFE_STRLCAT(query, QUERY_KEYWORD_SPACE, sizeof(query));
915 SAFE_FREE(token->str);
920 if (token_list != NULL) {
921 g_list_free(token_list);
927 SAFE_STRLCAT(query, QUERY_KEYWORD_SPACE, sizeof(query));
929 memset(option, 0, sizeof(option));
930 snprintf(option, sizeof(option), "%s %d, %d", QUERY_KEYWORD_LIMIT, _filter->offset, _filter->count);
931 SAFE_STRLCAT(query, option, sizeof(query));
933 if (STRING_VALID(query)) {
934 *generated_option = g_strdup(query);
935 media_content_sec_debug("Option : %s", *generated_option);
937 *generated_option = NULL;
944 int media_filter_create(filter_h *filter)
946 int ret = MEDIA_CONTENT_ERROR_NONE;
948 media_content_retvm_if(filter == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid filter");
950 filter_s *_filter = (filter_s*)calloc(1, sizeof(filter_s));
951 media_content_retvm_if(_filter == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
953 _filter->storage_id = NULL;
954 _filter->condition = NULL;
955 _filter->order_keyword = NULL;
956 _filter->order_type = -1;
957 _filter->condition_collate_type = MEDIA_CONTENT_COLLATE_DEFAULT;
958 _filter->order_collate_type = MEDIA_CONTENT_COLLATE_DEFAULT;
959 _filter->offset = -1;
961 _filter->is_full_condition = false;
962 _filter->is_full_order = false;
964 *filter = (filter_h)_filter;
969 int media_filter_destroy(filter_h filter)
971 int ret = MEDIA_CONTENT_ERROR_NONE;
972 filter_s *_filter = (filter_s*)filter;
975 SAFE_FREE(_filter->storage_id);
976 SAFE_FREE(_filter->condition);
977 SAFE_FREE(_filter->order_keyword);
980 ret = MEDIA_CONTENT_ERROR_NONE;
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_offset(filter_h filter, int offset, int count)
991 int ret = MEDIA_CONTENT_ERROR_NONE;
992 filter_s *_filter = (filter_s*)filter;
994 if (_filter != NULL) {
995 _filter->offset = offset;
996 _filter->count = count;
998 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
999 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1005 int media_filter_set_condition(filter_h filter, const char *condition, media_content_collation_e collate_type)
1007 int ret = MEDIA_CONTENT_ERROR_NONE;
1008 filter_s *_filter = (filter_s*)filter;
1010 if ((_filter != NULL) && STRING_VALID(condition)
1011 && ((collate_type >= MEDIA_CONTENT_COLLATE_DEFAULT) && (collate_type <= MEDIA_CONTENT_COLLATE_LOCALIZED))) {
1013 _filter->is_full_condition = false;
1015 if (STRING_VALID(_filter->condition))
1016 SAFE_FREE(_filter->condition);
1018 char new_condition[MAX_QUERY_SIZE] = {0, };
1019 memset(new_condition, 0, sizeof(new_condition));
1020 ret = _media_content_replace_path_in_condition(condition, new_condition, TRUE);
1021 media_content_retvm_if(!STRING_VALID(new_condition), MEDIA_CONTENT_ERROR_INVALID_OPERATION, "path replacement failed");
1023 _filter->condition = strdup(new_condition);
1024 media_content_retvm_if(_filter->condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1026 media_content_sec_debug("Condition string : %s", _filter->condition);
1028 _filter->condition_collate_type = collate_type;
1030 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1031 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1037 int media_filter_set_order(filter_h filter, media_content_order_e order_type, const char *order_keyword, media_content_collation_e collate_type)
1039 int ret = MEDIA_CONTENT_ERROR_NONE;
1040 filter_s *_filter = (filter_s*)filter;
1042 if ((_filter != NULL) && STRING_VALID(order_keyword)
1043 && ((order_type == MEDIA_CONTENT_ORDER_ASC) || (order_type == MEDIA_CONTENT_ORDER_DESC))
1044 && ((collate_type >= MEDIA_CONTENT_COLLATE_DEFAULT) && (collate_type <= MEDIA_CONTENT_COLLATE_LOCALIZED))) {
1046 _filter->is_full_order = false;
1048 SAFE_FREE(_filter->order_keyword);
1050 _filter->order_keyword = strdup(order_keyword);
1051 media_content_retvm_if(_filter->order_keyword == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1053 _filter->order_type = order_type;
1054 _filter->order_collate_type = collate_type;
1056 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1057 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1063 int media_filter_set_storage(filter_h filter, const char *storage_id)
1065 int ret = MEDIA_CONTENT_ERROR_NONE;
1066 filter_s *_filter = (filter_s*)filter;
1068 if ((_filter != NULL) && STRING_VALID(storage_id)) {
1069 if (STRING_VALID(_filter->storage_id))
1070 SAFE_FREE(_filter->storage_id);
1072 _filter->storage_id = strdup(storage_id);
1073 media_content_retvm_if(_filter->storage_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1075 media_content_sec_debug("storage_id : %s", _filter->storage_id);
1077 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1078 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1084 int media_filter_get_offset(filter_h filter, int *offset, int *count)
1086 int ret = MEDIA_CONTENT_ERROR_NONE;
1087 filter_s *_filter = (filter_s*)filter;
1090 *offset = _filter->offset;
1091 *count = _filter->count;
1093 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1094 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1100 int media_filter_get_condition(filter_h filter, char **condition, media_content_collation_e *collate_type)
1102 int ret = MEDIA_CONTENT_ERROR_NONE;
1103 filter_s *_filter = (filter_s*)filter;
1106 if (STRING_VALID(_filter->condition) && _filter->is_full_condition == false) {
1107 char new_condition[MAX_QUERY_SIZE] = {0, };
1108 memset(new_condition, 0, sizeof(new_condition));
1109 ret = _media_content_replace_path_in_condition(_filter->condition, new_condition, FALSE);
1110 media_content_retvm_if(!STRING_VALID(new_condition), MEDIA_CONTENT_ERROR_INVALID_OPERATION, "path replacement failed");
1112 *condition = strdup(new_condition);
1113 media_content_retvm_if(*condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1118 *collate_type = _filter->condition_collate_type;
1120 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1121 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1127 int media_filter_get_order(filter_h filter, media_content_order_e* order_type, char **order_keyword, media_content_collation_e *collate_type)
1129 int ret = MEDIA_CONTENT_ERROR_NONE;
1130 filter_s *_filter = (filter_s*)filter;
1133 if (STRING_VALID(_filter->order_keyword) && _filter->is_full_order == false) {
1134 *order_keyword = strdup(_filter->order_keyword);
1135 media_content_retvm_if(*order_keyword == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1137 *order_keyword = NULL;
1140 *order_type = _filter->order_type;
1141 *collate_type = _filter->order_collate_type;
1143 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1144 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1150 int media_filter_get_storage(filter_h filter, char **storage_id)
1152 int ret = MEDIA_CONTENT_ERROR_NONE;
1153 filter_s *_filter = (filter_s*)filter;
1156 if (STRING_VALID(_filter->storage_id)) {
1157 *storage_id = strdup(_filter->storage_id);
1158 media_content_retvm_if(*storage_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1163 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1164 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1170 int media_filter_set_condition_v2(filter_h filter, const char *condition)
1172 int ret = MEDIA_CONTENT_ERROR_NONE;
1173 filter_s *_filter = (filter_s*)filter;
1175 if ((_filter != NULL) && STRING_VALID(condition)) {
1176 _filter->is_full_condition = true;
1178 if (STRING_VALID(_filter->condition))
1179 SAFE_FREE(_filter->condition);
1181 char new_condition[MAX_QUERY_SIZE] = {0, };
1182 memset(new_condition, 0, sizeof(new_condition));
1183 ret = _media_content_replace_path_in_condition(condition, new_condition, TRUE);
1184 media_content_retvm_if(!STRING_VALID(new_condition), MEDIA_CONTENT_ERROR_INVALID_OPERATION, "path replacement failed");
1186 _filter->condition = strdup(new_condition);
1187 media_content_retvm_if(_filter->condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1189 media_content_sec_debug("Condition string : %s", _filter->condition);
1191 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1192 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1198 int media_filter_get_condition_v2(filter_h filter, char **condition)
1200 int ret = MEDIA_CONTENT_ERROR_NONE;
1201 filter_s *_filter = (filter_s*)filter;
1204 if (STRING_VALID(_filter->condition) && _filter->is_full_condition == true) {
1205 char new_condition[MAX_QUERY_SIZE] = {0, };
1206 memset(new_condition, 0, sizeof(new_condition));
1207 ret = _media_content_replace_path_in_condition(_filter->condition, new_condition, FALSE);
1208 media_content_retvm_if(!STRING_VALID(new_condition), MEDIA_CONTENT_ERROR_INVALID_OPERATION, "path replacement failed");
1210 *condition = strdup(new_condition);
1211 media_content_retvm_if(*condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1216 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1217 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1223 int media_filter_set_order_v2(filter_h filter, const char *order)
1225 int ret = MEDIA_CONTENT_ERROR_NONE;
1226 filter_s *_filter = (filter_s*)filter;
1228 if ((_filter != NULL) && STRING_VALID(order)) {
1229 _filter->is_full_order = true;
1231 SAFE_FREE(_filter->order_keyword);
1233 _filter->order_keyword = strdup(order);
1234 media_content_retvm_if(_filter->order_keyword == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1236 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1237 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1243 int media_filter_get_order_v2(filter_h filter, char **order)
1245 int ret = MEDIA_CONTENT_ERROR_NONE;
1246 filter_s *_filter = (filter_s*)filter;
1249 if (STRING_VALID(_filter->order_keyword) && _filter->is_full_order == true) {
1250 *order = strdup(_filter->order_keyword);
1251 media_content_retvm_if(*order == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1256 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1257 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;