Activate vconf(langset)
[platform/core/api/media-content.git] / src / media_filter.c
1 /*
2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18 #include <media_info_private.h>
19
20 static const char *media_token[] =
21 {
22         " ",
23         "\"",
24         "'",
25         "(",
26         ")",
27         "=",
28         "<=",
29         "<",
30         ">=",
31         ">",
32         "!=",
33 };
34
35
36 typedef struct _token_t
37 {
38         int type;
39         char *str;
40 }token_t;
41
42
43 #define MAX_LEFT_VALUE 512
44 #define SPACE_LEN 1
45 #define SPACE " "
46 #define UNKNOWN_TYPE 1000
47 #define STRING_TYPE 100
48
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);
55
56 static bool __is_pinyin_needed(void)
57 {
58         char *lang = NULL;
59         const char *china = "zh_CN";
60         const char *hongkong = "zh_HK";
61         int ret = FALSE;
62
63         /*Check CSC first*/
64         bool pinyin_support = FALSE;
65         media_svc_check_pinyin_support(&pinyin_support);
66         if(pinyin_support)
67         {
68                 /*Check Language Setting*/
69                 lang = vconf_get_str(VCONFKEY_LANGSET);
70                 media_content_retvm_if(lang == NULL, ret, "Fail to get string of language set");
71
72                 if((strncmp(china, lang, strlen(china)) == 0) ||
73                         (strncmp(hongkong, lang, strlen(hongkong)) == 0))
74                 {
75                         ret = TRUE;
76                 }
77
78                 SAFE_FREE(lang);
79         }
80
81         return ret;
82 }
83
84 static char *__get_order_str(media_content_order_e order_enum)
85 {
86         switch(order_enum) {
87                 case MEDIA_CONTENT_ORDER_ASC:
88                         return (char *)"ASC";
89                 case MEDIA_CONTENT_ORDER_DESC:
90                         return (char *)"DESC";
91                 default:
92                         return (char *)" ";
93         }
94 }
95
96 static char *__get_collate_str(media_content_collation_e collate_type)
97 {
98         switch(collate_type) {
99                 case MEDIA_CONTENT_COLLATE_NOCASE:
100                         return (char *)"NOCASE";
101                 case MEDIA_CONTENT_COLLATE_RTRIM:
102                         return (char *)"RTRIM";
103                 case MEDIA_CONTENT_COLLATE_LOCALIZED:
104                         if(__is_pinyin_needed())
105                                 return (char *)"NOCASE";
106                         else
107                                 return (char *)"localized";
108                 default: return (char *)" ";
109         }
110 }
111
112 static void __filter_attribute_free_value(gpointer key, gpointer value, gpointer user_data)
113 {
114         SAFE_FREE(key);
115         SAFE_FREE(value);
116 }
117
118 static char *__media_filter_replace_attr(attribute_h attr, char *name)
119 {
120         char *key_temp = NULL;
121         char *generated_value = NULL;
122         attribute_s *_attr = (attribute_s *)attr;
123
124         if(!g_hash_table_lookup_extended(_attr->attr_map,
125                                                                                 name,
126                                                                                 (gpointer)&key_temp, (gpointer)&generated_value))
127         {
128                 //can't find the value
129                 //media_content_error("NOT_FOUND_VALUE(%s)", name);
130                 return NULL;
131         }
132
133         if(STRING_VALID(generated_value))
134         {
135                 return strdup(generated_value);
136         }
137
138         media_content_error("__media_filter_replace_attr fail");
139
140         return NULL;
141 }
142
143 static int __tokenize_operator(token_t *token, const char *str, int op_type)
144 {
145         int ret = 0;
146         const char *tmp = str;
147
148         if(token != NULL && STRING_VALID(tmp))
149         {
150                 token->type = op_type;
151                 int token_size = strlen(media_token[op_type]);
152                 media_content_retvm_if(token_size == 0, -1, "Invalid token_size. op_type[%d]", op_type);
153
154                 token->str = (char*)calloc(token_size+1, sizeof(char));
155                 media_content_retvm_if(token->str == NULL, -1, "OUT_OF_MEMORY");
156
157                 strncpy(token->str, tmp, token_size);
158                 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
159                 ret = token_size;
160         }
161         else
162         {
163                 ret = -1;
164         }
165
166         return ret;
167 }
168
169 static int __tokenize_string(token_t *token, const char *str, int size)
170 {
171         int ret = size;
172         const char *tmp = str;
173
174         if(token != NULL && STRING_VALID(tmp) && size > 0)
175         {
176                 token->str = (char*)calloc(size+1, sizeof(char));
177                 media_content_retvm_if(token->str == NULL, -1, "OUT_OF_MEMORY");
178
179                 token->type = UNKNOWN_TYPE;
180                 strncpy(token->str, tmp, size);
181                 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
182         }
183         else
184         {
185                 ret = -1;
186         }
187
188         return ret;
189 }
190
191 static int __tokenize_attribute(GList **token_list, const char *str)
192 {
193         int ret = 0;
194         int idx = 0;
195
196         media_content_retvm_if(!STRING_VALID(str), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Parameter string is invalid");
197
198         const char *tmp = str;
199         const char *dst_ptr = str + strlen(str);
200
201         for (idx = 0; (*(tmp+idx)) && (tmp < dst_ptr); idx++)
202         {
203                 //media_content_debug("[%d] '%c'", idx, tmp[idx]);
204                 if(tmp[idx] == ' ')             //" "
205                 {
206                         if(idx == 0)            // ignore the space.
207                         {
208                                 tmp++;
209                                 idx = -1;
210                                 continue;
211                         }
212                         token_t *token = (token_t*)calloc(1, sizeof(token_t));
213                         media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
214
215                         token->type = UNKNOWN_TYPE;
216                         token->str = (char*)calloc(idx+1, sizeof(char));
217                         if(token->str == NULL)
218                         {
219                                 SAFE_FREE(token);
220                                 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
221                                 return -1;
222                         }
223                         strncpy(token->str, tmp, idx);
224                         //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
225                         *token_list = g_list_append(*token_list, token);
226                         tmp = tmp +idx + strlen(media_token[0]);
227                         idx = -1;
228                 }
229                 else if(tmp[idx] == ',')        // " , "
230                 {
231                         if(idx != 0)
232                         {
233                                 token_t *token = (token_t*)calloc(1, sizeof(token_t));
234                                 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
235
236                                 ret = __tokenize_string(token, tmp, idx);
237                                 if (ret < 0)
238                                 {
239                                         SAFE_FREE(token);
240                                         media_content_error("tokenize error occured");
241                                         return -1;
242                                 }
243                                 else
244                                 {
245                                         *token_list = g_list_append(*token_list, token);
246                                         tmp = tmp + idx;
247                                 }
248                         }
249
250                         token_t *token = (token_t*)calloc(1, sizeof(token_t));
251                         int size = __tokenize_operator(token, tmp,3);
252
253                         if(token != NULL && STRING_VALID(token->str))
254                         {
255                                 *token_list = g_list_append(*token_list, token);
256                                 tmp += size;
257                                 idx = -1;
258                         }
259                         else
260                         {
261                                 SAFE_FREE(token);
262                                 media_content_error("tokenize error occured");
263                                 return -1;
264                         }
265                 }
266         }
267
268         if(*tmp)                        //remained string
269         {
270                 token_t *token = (token_t*)calloc(1, sizeof(token_t));
271                 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
272
273                 ret = __tokenize_string(token, tmp, idx);
274                 if (ret < 0)
275                 {
276                         SAFE_FREE(token);
277                         media_content_error("tokenize error occured");
278                         return -1;
279                 }
280
281                 if(token != NULL && STRING_VALID(token->str))
282                 {
283                         *token_list = g_list_append(*token_list, token);
284                 }
285                 else
286                 {
287                         SAFE_FREE(token);
288                         media_content_error("tokenize error occured");
289                         return -1;
290                 }
291         }
292
293         return MEDIA_CONTENT_ERROR_NONE;
294 }
295
296 static int __tokenize(GList **token_list, const char *str)
297 {
298         int ret = 0;
299         int idx = 0;
300
301         media_content_retvm_if(!STRING_VALID(str), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Parameter string is invalid");
302
303         const char *tmp = str;
304         const char *dst_ptr = str + strlen(str);
305
306         for (idx = 0; (*(tmp+idx)) && (tmp < dst_ptr); idx++)
307         {
308                 //media_content_debug("[%d] '%c'", idx, tmp[idx]);
309                 if(tmp[idx] == media_token[0][0])               //" "
310                 {
311                         if(idx == 0)            // ignore the space.
312                         {
313                                 tmp++;
314                                 idx = -1;
315                                 continue;
316                         }
317
318                         token_t *token = (token_t*)calloc(1, sizeof(token_t));
319                         media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
320
321                         token->type = UNKNOWN_TYPE;
322                         token->str = (char*)calloc(idx+1, sizeof(char));
323                         if(token->str == NULL)
324                         {
325                                 SAFE_FREE(token);
326                                 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
327                                 return -1;
328                         }
329                         strncpy(token->str, tmp, idx);
330                         //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
331                         *token_list = g_list_append(*token_list, token);
332                         tmp = tmp +idx + strlen(media_token[0]);
333                         idx = -1;
334                 }
335                 else if(tmp[idx] == media_token[1][0])  // " \" "
336                 {
337                         int j;
338                         bool flag = false;
339                         for(j = idx+1; tmp[j]; j++)     //find next quotation
340                         {
341                                 if(tmp[j] == media_token[1][0] && tmp[j+1] == media_token[1][0])
342                                 {
343                                         j += 1;
344                                         continue;
345                                 }
346                                 if(tmp[j] == media_token[1][0])
347                                 {
348                                         token_t *token = (token_t*)calloc(1, sizeof(token_t));
349                                         media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
350
351                                         token->str = (char*) calloc(j+1+1, sizeof(char));
352                                         if(token->str == NULL)
353                                         {
354                                                 SAFE_FREE(token);
355                                                 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
356                                                 return -1;
357                                         }
358                                         token->type = STRING_TYPE;
359                                         strncpy(token->str, tmp, j+1);
360                                         //media_content_debug("type : [%d] str : [%s], j : %d", token->type, token->str, j);
361                                         *token_list = g_list_append(*token_list, token);
362                                         tmp = tmp + strlen(token->str);
363                                         idx = -1;
364                                         flag = true;
365                                         break;
366                                 }
367                         }
368
369                         if(!flag && *tmp != '\0' && tmp[j]=='\0')
370                         {
371                                 token_t *token = (token_t*)calloc(1, sizeof(token_t));
372                                 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
373
374                                 token->str = (char*) calloc(j+1,sizeof(char));
375                                 if(token->str == NULL)
376                                 {
377                                         SAFE_FREE(token);
378                                         media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
379                                         return -1;
380                                 }
381                                 token->type = UNKNOWN_TYPE;
382                                 strncpy(token->str, tmp,j);
383                                 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
384                                 *token_list = g_list_append(*token_list, token);
385                                 tmp = tmp +strlen(token->str);
386                                 idx = -1;
387                         }
388                 }
389                 else if(tmp[idx] == media_token[2][0])  // " \' "
390                 {
391                         int j;
392                         bool flag = false;
393                         for(j = idx+1; tmp[j]; j++)
394                         {
395                                 if(tmp[j] == media_token[2][0] && tmp[j+1] == media_token[2][0])
396                                 {
397                                         j += 1;
398                                         continue;
399                                 }
400                                 if(tmp[j] == media_token[2][0])
401                                 {
402                                         token_t *token = (token_t*)calloc(1, sizeof(token_t));
403                                         media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
404
405                                         token->str = (char*) calloc(j+1+1, sizeof(char));
406                                         if(token->str == NULL)
407                                         {
408                                                 SAFE_FREE(token);
409                                                 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
410                                                 return -1;
411                                         }
412                                         token->type = STRING_TYPE;
413                                         strncpy(token->str, tmp, j+1);
414                                         //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
415                                         *token_list = g_list_append(*token_list, token);
416                                         tmp = tmp + strlen(token->str);
417                                         idx = -1;
418                                         flag = true;
419                                         break;
420                                 }
421                         }
422
423                         if(!flag && *tmp != '\0' && tmp[j]=='\0')
424                         {
425                                 token_t *token = (token_t*)calloc(1, sizeof(token_t));
426                                 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
427
428                                 token->str = (char*) calloc(j+1,sizeof(char));
429                                 if(token->str == NULL)
430                                 {
431                                         SAFE_FREE(token);
432                                         media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
433                                         return -1;
434                                 }
435                                 token->type = UNKNOWN_TYPE;
436                                 strncpy(token->str, tmp,j);
437                                 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
438                                 *token_list = g_list_append(*token_list, token);
439                                 tmp = tmp + strlen(token->str);
440                                 idx = -1;
441                         }
442                 }
443                 else if(tmp[idx] == media_token[3][0])  //"("
444                 {
445                         if(idx != 0)
446                         {
447                                 token_t *token = (token_t*)calloc(1, sizeof(token_t));
448                                 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
449
450                                 ret = __tokenize_string(token, tmp, idx);
451                                 if (ret < 0)
452                                 {
453                                         SAFE_FREE(token);
454                                         media_content_error("tokenize error occured");
455                                         return -1;
456                                 }
457                                 else
458                                 {
459                                         *token_list = g_list_append(*token_list, token);
460                                         tmp = tmp + idx;
461                                 }
462                         }
463                         token_t *token = (token_t*)calloc(1, sizeof(token_t));
464                         int size = __tokenize_operator(token, tmp,3);
465
466                         if(token != NULL && STRING_VALID(token->str))
467                         {
468                                 *token_list = g_list_append(*token_list, token);
469                                 tmp += size;
470                                 idx = -1;
471                         }
472                         else
473                         {
474                                 SAFE_FREE(token);
475                                 media_content_error("tokenize error occured");
476                                 return -1;
477                         }
478
479                 }
480                 else if(tmp[idx] == media_token[4][0])  //")"
481                 {
482                         if(idx != 0)
483                         {
484                                 token_t *token = (token_t*)calloc(1, sizeof(token_t));
485                                 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
486
487                                 ret = __tokenize_string(token, tmp, idx);
488                                 if (ret < 0)
489                                 {
490                                         SAFE_FREE(token);
491                                         media_content_error("tokenize error occured");
492                                         return -1;
493                                 }
494                                 else
495                                 {
496                                         *token_list = g_list_append(*token_list, token);
497                                         tmp = tmp + idx;
498                                 }
499                         }
500                         token_t *token = (token_t*)calloc(1, sizeof(token_t));
501                         int size = __tokenize_operator(token, tmp,4);
502
503                         if(token != NULL && STRING_VALID(token->str))
504                         {
505                                 *token_list = g_list_append(*token_list, token);
506                                 tmp += size;
507                                 idx = -1;
508                         }
509                         else
510                         {
511                                 SAFE_FREE(token);
512                                 media_content_error("tokenize error occured");
513                                 return -1;
514                         }
515
516                 }
517                 else if(tmp[idx] == media_token[5][0])  //"="
518                 {
519                         if(idx != 0)
520                         {
521                                 token_t *token = (token_t*)calloc(1, sizeof(token_t));
522                                 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
523
524                                 ret = __tokenize_string(token, tmp, idx);
525                                 if (ret < 0)
526                                 {
527                                         SAFE_FREE(token);
528                                         media_content_error("tokenize error occured");
529                                         return -1;
530                                 }
531                                 else
532                                 {
533                                         *token_list = g_list_append(*token_list, token);
534                                         tmp = tmp + idx;
535                                 }
536                         }
537                         token_t *token = (token_t*)calloc(1, sizeof(token_t));
538                         int size = __tokenize_operator(token, tmp,5);
539
540                         if(token != NULL && STRING_VALID(token->str))
541                         {
542                                 *token_list = g_list_append(*token_list, token);
543                                 tmp += size;
544                                 idx = -1;
545                         }
546                         else
547                         {
548                                 SAFE_FREE(token);
549                                 media_content_error("tokenize error occured");
550                                 return -1;
551                         }
552
553                 }
554                 else if(tmp[idx] == media_token[6][0] && tmp[idx+1] == media_token[6][1])       //"<=",
555                 {
556                         if(idx != 0)
557                         {
558                                 token_t *token = (token_t*)calloc(1, sizeof(token_t));
559                                 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
560
561                                 ret = __tokenize_string(token, tmp, idx);
562                                 if (ret < 0)
563                                 {
564                                         SAFE_FREE(token);
565                                         media_content_error("tokenize error occured");
566                                         return -1;
567                                 }
568                                 else
569                                 {
570                                         *token_list = g_list_append(*token_list, token);
571                                         tmp = tmp + idx;
572                                 }
573                         }
574                         token_t *token = (token_t*)calloc(1, sizeof(token_t));
575                         int size = __tokenize_operator(token, tmp,6);
576
577                         if(token != NULL && STRING_VALID(token->str))
578                         {
579                                 *token_list = g_list_append(*token_list, token);
580                                 tmp += size;
581                                 idx = -1;
582                         }
583                         else
584                         {
585                                 SAFE_FREE(token);
586                                 media_content_error("tokenize error occured");
587                                 return -1;
588                         }
589
590                 }
591                 else if(tmp[idx] == media_token[7][0])  //"<",
592                 {
593                         if(idx != 0)
594                         {
595                                 token_t *token = (token_t*)calloc(1, sizeof(token_t));
596                                 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
597
598                                 ret = __tokenize_string(token, tmp, idx);
599                                 if (ret < 0)
600                                 {
601                                         SAFE_FREE(token);
602                                         media_content_error("tokenize error occured");
603                                         return -1;
604                                 }
605                                 else
606                                 {
607                                         *token_list = g_list_append(*token_list, token);
608                                         tmp = tmp + idx;
609                                 }
610                         }
611                         token_t *token = (token_t*)calloc(1, sizeof(token_t));
612                         int size = __tokenize_operator(token, tmp,7);
613
614                         if(token != NULL && STRING_VALID(token->str))
615                         {
616                                 *token_list = g_list_append(*token_list, token);
617                                 tmp += size;
618                                 idx = -1;
619                         }
620                         else
621                         {
622                                 SAFE_FREE(token);
623                                 media_content_error("tokenize error occured");
624                                 return -1;
625                         }
626
627                 }
628                 else if(tmp[idx] == media_token[8][0] && tmp[idx+1] == media_token[8][1])       //">=",
629                 {
630                         if(idx != 0)
631                         {
632                                 token_t *token = (token_t*)calloc(1, sizeof(token_t));
633                                 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
634
635                                 ret = __tokenize_string(token, tmp, idx);
636                                 if (ret < 0)
637                                 {
638                                         SAFE_FREE(token);
639                                         media_content_error("tokenize error occured");
640                                         return -1;
641                                 }
642                                 else
643                                 {
644                                         *token_list = g_list_append(*token_list, token);
645                                         tmp = tmp + idx;
646                                 }
647                         }
648                         token_t *token = (token_t*)calloc(1, sizeof(token_t));
649                         int size = __tokenize_operator(token, tmp,8);
650
651                         if(token != NULL && STRING_VALID(token->str))
652                         {
653                                 *token_list = g_list_append(*token_list, token);
654                                 tmp += size;
655                                 idx = -1;
656                         }
657                         else
658                         {
659                                 SAFE_FREE(token);
660                                 media_content_error("tokenize error occured");
661                                 return -1;
662                         }
663
664                 }
665                 else if(tmp[idx] == media_token[9][0])  //">",
666                 {
667                         if(idx != 0)
668                         {
669                                 token_t *token = (token_t*)calloc(1, sizeof(token_t));
670                                 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
671
672                                 ret = __tokenize_string(token, tmp, idx);
673                                 if (ret < 0)
674                                 {
675                                         SAFE_FREE(token);
676                                         media_content_error("tokenize error occured");
677                                         return -1;
678                                 }
679                                 else
680                                 {
681                                         *token_list = g_list_append(*token_list, token);
682                                         tmp = tmp + idx;
683                                 }
684                         }
685                         token_t *token = (token_t*)calloc(1, sizeof(token_t));
686                         int size = __tokenize_operator(token, tmp,9);
687
688                         if(token != NULL && STRING_VALID(token->str))
689                         {
690                                 *token_list = g_list_append(*token_list, token);
691                                 tmp += size;
692                                 idx = -1;
693                         }
694                         else
695                         {
696                                 SAFE_FREE(token);
697                                 media_content_error("tokenize error occured");
698                                 return -1;
699                         }
700                 }
701                 else if(tmp[idx] == media_token[10][0] && tmp[idx+1] == media_token[10][1])     //"!=",
702                 {
703                         if(idx != 0)
704                         {
705                                 token_t *token = (token_t*)calloc(1, sizeof(token_t));
706                                 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
707
708                                 ret = __tokenize_string(token, tmp, idx);
709                                 if (ret < 0)
710                                 {
711                                         SAFE_FREE(token);
712                                         media_content_error("tokenize error occured");
713                                         return -1;
714                                 }
715                                 else
716                                 {
717                                         *token_list = g_list_append(*token_list, token);
718                                         tmp = tmp + idx;
719                                 }
720                         }
721                         token_t *token = (token_t*)calloc(1, sizeof(token_t));
722                         int size = __tokenize_operator(token, tmp, 10);
723
724                         if(token != NULL && STRING_VALID(token->str))
725                         {
726                                 *token_list = g_list_append(*token_list, token);
727                                 tmp += size;
728                                 idx = -1;
729                         }
730                         else
731                         {
732                                 SAFE_FREE(token);
733                                 media_content_error("tokenize error occured");
734                                 return -1;
735                         }
736                 }
737         }
738
739         if(*tmp)                        //remained string
740         {
741                 token_t *token = (token_t*)calloc(1, sizeof(token_t));
742                 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
743
744                 ret = __tokenize_string(token, tmp, idx);
745                 if (ret < 0)
746                 {
747                         SAFE_FREE(token);
748                         media_content_error("tokenize error occured");
749                         return -1;
750                 }
751
752                 if(token != NULL && STRING_VALID(token->str))
753                 {
754                         *token_list = g_list_append(*token_list, token);
755                 }
756                 else
757                 {
758                         SAFE_FREE(token);
759                         media_content_error("tokenize error occured");
760                         return -1;
761                 }
762         }
763
764         return MEDIA_CONTENT_ERROR_NONE;
765 }
766
767 int _media_filter_attribute_create(attribute_h *attr)
768 {
769         int ret = MEDIA_CONTENT_ERROR_NONE;
770
771         media_content_retvm_if(attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
772
773         attribute_s *_attr = (attribute_s*)calloc(1, sizeof(attribute_s));
774         media_content_retvm_if(_attr == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
775
776         _attr->attr_map = g_hash_table_new (g_str_hash, g_str_equal);
777         *attr = (attribute_h)_attr;
778
779         return ret;
780 }
781
782 int _media_filter_attribute_add(attribute_h attr, const char *user_attr, const char *platform_attr)
783 {
784         int ret = MEDIA_CONTENT_ERROR_NONE;
785         char *_user = NULL;
786         char *_platform = NULL;
787         attribute_s *_attr = (attribute_s*)attr;
788
789         media_content_retvm_if(_attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
790
791         if(STRING_VALID(user_attr) && STRING_VALID(platform_attr))
792         {
793                 _user = strdup(user_attr);
794                 media_content_retvm_if(_user == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
795
796                 _platform = strdup(platform_attr);
797                 if(_platform == NULL)
798                 {
799                         SAFE_FREE(_user);
800                         media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
801                         return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
802                 }
803
804                 g_hash_table_insert (_attr->attr_map, _user, _platform);
805         }
806         else
807         {
808                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
809                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
810         }
811
812         return ret;
813 }
814
815 int _media_filter_attribute_destory(attribute_h attr)
816 {
817         int ret = MEDIA_CONTENT_ERROR_NONE;
818         attribute_s *_attr = (attribute_s*)attr;
819
820         media_content_retvm_if(_attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
821
822         if(_attr->attr_map != NULL)
823         {
824                 g_hash_table_foreach(_attr->attr_map, __filter_attribute_free_value, NULL);
825                 g_hash_table_destroy(_attr->attr_map);
826         }
827
828         SAFE_FREE(_attr);
829
830         return ret;
831 }
832
833 int _media_filter_attribute_generate(attribute_h attr, char *condition, media_content_collation_e collate_type, char **generated_condition)
834 {
835         unsigned int idx = 0;
836         GList *token_list = NULL;
837         int size = 0;
838         int ret = MEDIA_CONTENT_ERROR_NONE;
839         int total_str_size = 0;
840         token_t *token;
841
842         media_content_retvm_if(condition == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid condition");
843         media_content_retvm_if(generated_condition == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid generated_condition");
844         media_content_retvm_if(attr == NULL, MEDIA_CONTENT_ERROR_DB_FAILED, "DB field mapping table doesn't exist. Check db connection");
845
846         if(__tokenize(&token_list, condition) < 0)
847         {
848                 media_content_error("INVALID_PARAMETER(0x%08x):Invalid the condition", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
849                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
850         }
851
852         for(idx = 0; idx < g_list_length(token_list); idx++)
853         {
854                 token = (token_t*)g_list_nth_data(token_list, idx);
855
856                 if(token->type == UNKNOWN_TYPE)
857                 {
858                         char *replace_str = __media_filter_replace_attr(attr, token->str);
859                         if(STRING_VALID(replace_str))
860                         {
861                                 SAFE_FREE(token->str);
862                                 token->str = replace_str;
863                         }
864                 }
865
866                 total_str_size += strlen(token->str)+1;
867                 //media_content_debug("[%d][type:%d]:%s", idx, token->type, token->str);
868         }
869
870         //make the statment
871         size = total_str_size + COLLATE_STR_SIZE + 1;
872         * generated_condition = (char*)calloc(size, sizeof(char));
873
874         for(idx = 0; idx < g_list_length(token_list); idx++)
875         {
876                 token = (token_t*)g_list_nth_data(token_list, idx);
877
878                 if((token != NULL) && STRING_VALID(token->str))
879                 {
880                         SAFE_STRLCAT(*generated_condition, token->str, size);
881                         SAFE_STRLCAT(*generated_condition, SPACE, size);
882
883                         SAFE_FREE(token->str);
884                         SAFE_FREE(token);
885                 }
886         }
887
888         if(collate_type == MEDIA_CONTENT_COLLATE_NOCASE || collate_type == MEDIA_CONTENT_COLLATE_RTRIM ||collate_type == MEDIA_CONTENT_COLLATE_LOCALIZED) {
889                 SAFE_STRLCAT(*generated_condition, "COLLATE ", size);
890                 SAFE_STRLCAT(*generated_condition, __get_collate_str(collate_type), size);
891                 SAFE_STRLCAT(*generated_condition, SPACE, size);
892         }
893
894         //media_content_debug("statement : %s(%d) (total:%d)", *generated_condition, strlen(*generated_condition), total_str_size);
895         media_content_sec_debug("Condition : %s", *generated_condition);
896
897         //if(*generated_condition != NULL)
898         //      res = 1;
899
900         if(token_list != NULL)
901                 g_list_free(token_list);
902
903         return ret;
904 }
905
906 int _media_filter_attribute_option_generate(attribute_h attr, filter_h filter, char **generated_option)
907 {
908         int ret = MEDIA_CONTENT_ERROR_NONE;
909         filter_s *_filter = NULL;
910         char option_query[DEFAULT_QUERY_SIZE] = {0, };
911         char condition[DEFAULT_QUERY_SIZE] = {0, };
912         int size = 0;
913         //bool order_by = true;
914
915         media_content_retvm_if(attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
916         media_content_retvm_if(filter == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid filter");
917
918         _filter = (filter_s*)filter;
919
920         memset(option_query, 0x00, sizeof(option_query));
921
922         /* Order by*/
923         if(STRING_VALID(_filter->order_keyword) && ((_filter->order_type == MEDIA_CONTENT_ORDER_ASC) ||(_filter->order_type == MEDIA_CONTENT_ORDER_DESC)))
924         {
925                 unsigned int idx = 0;
926                 int total_str_size = 0;
927                 GList *token_list = NULL;
928                 token_t *token;
929                 char *attr_str;
930
931                 if(__tokenize_attribute(&token_list, _filter->order_keyword) < 0)
932                 {
933                         media_content_error("INVALID_PARAMETER(0x%08x):Invalid the condition", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
934                         return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
935                 }
936
937                 for(idx = 0; idx < g_list_length(token_list); idx++)
938                 {
939                         token = (token_t*)g_list_nth_data(token_list, idx);
940
941                         if(token->type == UNKNOWN_TYPE)
942                         {
943                                 char *replace_str = __media_filter_replace_attr(attr, token->str);
944                                 if(STRING_VALID(replace_str))
945                                 {
946                                         attr_str = (char*)calloc(strlen(replace_str) + COLLATE_STR_SIZE + 1, sizeof(char));
947                                         if(attr_str == NULL)
948                                         {
949                                                 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
950                                                 SAFE_FREE(replace_str);
951                                                 continue;
952                                         }
953
954                                         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) {
955                                                 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));
956                                         } else {
957                                                 snprintf(attr_str, strlen(replace_str) + COLLATE_STR_SIZE + 1, "%s %s", replace_str, __get_order_str(_filter->order_type));
958                                         }
959
960                                         SAFE_FREE(token->str);
961                                         token->str = attr_str;
962                                         SAFE_FREE(replace_str);
963                                 }
964                                 else
965                                 {
966                                         media_content_error("There is no matched db field for %s", token->str);
967                                 }
968                         }
969
970                         total_str_size += strlen(token->str) + 1;
971                         //media_content_debug("[%d][type:%d]:%s", idx, token->type, token->str);
972                 }
973
974                 //make the statment
975                 char *generated_condition = NULL;
976                 size = total_str_size + COLLATE_STR_SIZE + 1;
977                 generated_condition = (char*)calloc(size, sizeof(char));
978
979                 for(idx = 0; idx < g_list_length(token_list); idx++)
980                 {
981                         token = (token_t*)g_list_nth_data(token_list, idx);
982
983                         if((token != NULL) && STRING_VALID(token->str))
984                         {
985                                 //media_content_debug("[%d] %s", idx, token->str);
986                                 SAFE_STRLCAT(generated_condition, token->str, size);
987                                 SAFE_STRLCAT(generated_condition, SPACE, size);
988
989                                 SAFE_FREE(token->str);
990                                 SAFE_FREE(token);
991                         }
992                 }
993
994                 snprintf(condition, sizeof(condition), "ORDER BY %s", generated_condition);
995                 SAFE_STRLCAT(option_query, condition, sizeof(option_query));
996
997                 if(token_list != NULL)
998                         g_list_free(token_list);
999
1000                 SAFE_FREE(generated_condition);
1001         }
1002
1003         /* offset */
1004         SAFE_STRLCAT(option_query, SPACE, sizeof(option_query));
1005         snprintf(condition, sizeof(condition), "LIMIT %d, %d", _filter->offset, _filter->count);
1006         SAFE_STRLCAT(option_query, condition, sizeof(option_query));
1007
1008         if(STRING_VALID(option_query))
1009         {
1010                 *generated_option = strdup(option_query);
1011         }
1012         else
1013         {
1014                 *generated_option = NULL;
1015         }
1016
1017         return ret;
1018 }
1019
1020 int media_filter_create(filter_h *filter)
1021 {
1022         int ret = MEDIA_CONTENT_ERROR_NONE;
1023
1024         media_content_retvm_if(filter == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid filter");
1025
1026         filter_s *_filter = (filter_s*)calloc(1, sizeof(filter_s));
1027         media_content_retvm_if(_filter == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1028
1029         _filter->storage_id = NULL;
1030         _filter->condition = NULL;
1031         _filter->order_keyword = NULL;
1032         _filter->order_type = -1;
1033         _filter->condition_collate_type = MEDIA_CONTENT_COLLATE_DEFAULT;
1034         _filter->order_collate_type = MEDIA_CONTENT_COLLATE_DEFAULT;
1035         _filter->offset = -1;
1036         _filter->count = -1;
1037
1038         *filter = (filter_h)_filter;
1039
1040         return ret;
1041 }
1042
1043 int media_filter_destroy(filter_h filter)
1044 {
1045         int ret = MEDIA_CONTENT_ERROR_NONE;
1046         filter_s *_filter = (filter_s*)filter;
1047
1048         if(_filter)
1049         {
1050                 SAFE_FREE(_filter->storage_id);
1051                 SAFE_FREE(_filter->condition);
1052                 SAFE_FREE(_filter->order_keyword);
1053                 SAFE_FREE(_filter);
1054
1055                 ret = MEDIA_CONTENT_ERROR_NONE;
1056         }
1057         else
1058         {
1059                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1060                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1061         }
1062
1063         return ret;
1064 }
1065
1066 int media_filter_set_offset(filter_h filter, int offset, int count)
1067 {
1068         int ret = MEDIA_CONTENT_ERROR_NONE;
1069         filter_s *_filter = (filter_s*)filter;
1070
1071         if(_filter != NULL)
1072         {
1073                 _filter->offset = offset;
1074                 _filter->count = count;
1075         }
1076         else
1077         {
1078                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1079                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1080         }
1081
1082         return ret;
1083 }
1084
1085 int media_filter_set_condition(filter_h filter, const char *condition, media_content_collation_e collate_type)
1086 {
1087         int ret = MEDIA_CONTENT_ERROR_NONE;
1088         filter_s *_filter = (filter_s*)filter;
1089
1090         if((_filter != NULL) && STRING_VALID(condition)
1091                 && ((collate_type >= MEDIA_CONTENT_COLLATE_DEFAULT) && (collate_type <= MEDIA_CONTENT_COLLATE_LOCALIZED)))
1092         {
1093                 if(STRING_VALID(_filter->condition))
1094                 {
1095                         SAFE_FREE(_filter->condition);
1096                 }
1097
1098                 _filter->condition = strdup(condition);
1099                 media_content_retvm_if(_filter->condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1100
1101                 media_content_sec_debug("Condition string : %s", _filter->condition);
1102
1103                 _filter->condition_collate_type = collate_type;
1104         }
1105         else
1106         {
1107                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1108                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1109         }
1110
1111         return ret;
1112 }
1113
1114 int media_filter_set_order(filter_h filter, media_content_order_e order_type, const char *order_keyword, media_content_collation_e collate_type)
1115 {
1116         int ret = MEDIA_CONTENT_ERROR_NONE;
1117         filter_s *_filter = (filter_s*)filter;
1118
1119         if((_filter != NULL) && STRING_VALID(order_keyword)
1120                 && ((order_type == MEDIA_CONTENT_ORDER_ASC) ||(order_type == MEDIA_CONTENT_ORDER_DESC))
1121                 && ((collate_type >= MEDIA_CONTENT_COLLATE_DEFAULT) && (collate_type <= MEDIA_CONTENT_COLLATE_LOCALIZED)))
1122         {
1123                 SAFE_FREE(_filter->order_keyword);
1124
1125                 if(STRING_VALID(order_keyword))
1126                 {
1127                         _filter->order_keyword = strdup(order_keyword);
1128                         media_content_retvm_if(_filter->order_keyword == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1129
1130                         _filter->order_type = order_type;
1131                         _filter->order_collate_type = collate_type;
1132                 }
1133                 else
1134                 {
1135                         media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1136                         ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1137                 }
1138         }
1139         else
1140         {
1141                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1142                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1143         }
1144
1145         return ret;
1146 }
1147
1148 int media_filter_set_storage(filter_h filter, const char *storage_id)
1149 {
1150         int ret = MEDIA_CONTENT_ERROR_NONE;
1151         filter_s *_filter = (filter_s*)filter;
1152
1153         if((_filter != NULL) && STRING_VALID(storage_id))
1154         {
1155                 if(STRING_VALID(_filter->storage_id))
1156                 {
1157                         SAFE_FREE(_filter->storage_id);
1158                 }
1159
1160                 _filter->storage_id = strdup(storage_id);
1161                 media_content_retvm_if(_filter->storage_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1162
1163                 media_content_sec_debug("storage_id : %s", _filter->storage_id);
1164         }
1165         else
1166         {
1167                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1168                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1169         }
1170
1171         return ret;
1172 }
1173
1174 int media_filter_get_offset(filter_h filter, int *offset, int *count)
1175 {
1176         int ret = MEDIA_CONTENT_ERROR_NONE;
1177         filter_s *_filter = (filter_s*)filter;
1178
1179         if(_filter)
1180         {
1181                 *offset = _filter->offset;
1182                 *count = _filter->count;
1183         }
1184         else
1185         {
1186                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1187                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1188         }
1189
1190         return ret;
1191 }
1192
1193 int media_filter_get_condition(filter_h filter, char **condition, media_content_collation_e *collate_type)
1194 {
1195         int ret = MEDIA_CONTENT_ERROR_NONE;
1196         filter_s *_filter = (filter_s*)filter;
1197
1198         if(_filter)
1199         {
1200                 if(STRING_VALID(_filter->condition))
1201                 {
1202                         *condition = strdup(_filter->condition);
1203                         media_content_retvm_if(*condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1204                 }
1205                 else
1206                 {
1207                         *condition = NULL;
1208                 }
1209
1210                 *collate_type = _filter->condition_collate_type;
1211         }
1212         else
1213         {
1214                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1215                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1216         }
1217
1218         return ret;
1219 }
1220
1221 int media_filter_get_order(filter_h filter, media_content_order_e* order_type, char **order_keyword, media_content_collation_e *collate_type)
1222 {
1223         int ret = MEDIA_CONTENT_ERROR_NONE;
1224         filter_s *_filter = (filter_s*)filter;
1225
1226         if(_filter)
1227         {
1228                 if(STRING_VALID(_filter->order_keyword))
1229                 {
1230                         *order_keyword = strdup(_filter->order_keyword);
1231                         media_content_retvm_if(*order_keyword == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1232                 }
1233                 else
1234                 {
1235                         *order_keyword = NULL;
1236                 }
1237
1238                 *order_type = _filter->order_type;
1239                 *collate_type = _filter->order_collate_type;
1240         }
1241         else
1242         {
1243                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1244                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1245         }
1246
1247         return ret;
1248 }
1249
1250 int media_filter_get_storage(filter_h filter, char **storage_id)
1251 {
1252         int ret = MEDIA_CONTENT_ERROR_NONE;
1253         filter_s *_filter = (filter_s*)filter;
1254
1255         if(_filter)
1256         {
1257                 if(STRING_VALID(_filter->storage_id))
1258                 {
1259                         *storage_id = strdup(_filter->storage_id);
1260                         media_content_retvm_if(*storage_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1261                 }
1262                 else
1263                 {
1264                         *storage_id = NULL;
1265                 }
1266         }
1267         else
1268         {
1269                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1270                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1271         }
1272
1273         return ret;
1274 }