bcdc68d1ee2bccb7af84f285280a603ad89dab59
[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 #include <media_util_private.h>
20 #include <vconf.h>
21
22 static const char *media_token[] = {
23         " ",
24         "\"",
25         "'",
26         "(",
27         ")",
28         "=",
29         "<=",
30         "<",
31         ">=",
32         ">",
33         "!=",
34 };
35
36
37 typedef struct _token_t {
38         int type;
39         char *str;
40 } token_t;
41
42 #define UNKNOWN_TYPE 1000
43 #define STRING_TYPE 100
44
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);
51
52 static bool __is_pinyin_needed(void)
53 {
54         char *lang = NULL;
55         const char *china = "zh_CN";
56         const char *hongkong = "zh_HK";
57         int ret = FALSE;
58
59         /*Check CSC first*/
60         bool pinyin_support = FALSE;
61         media_svc_check_pinyin_support(&pinyin_support);
62         if (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");
66
67                 if ((strncmp(china, lang, strlen(china)) == 0) ||
68                         (strncmp(hongkong, lang, strlen(hongkong)) == 0)) {
69                         ret = TRUE;
70                 }
71
72                 SAFE_FREE(lang);
73         }
74
75         return ret;
76 }
77
78 static char *__get_order_str(media_content_order_e order_enum)
79 {
80         switch (order_enum) {
81         case MEDIA_CONTENT_ORDER_ASC:
82                 return (char *)"ASC";
83         case MEDIA_CONTENT_ORDER_DESC:
84                 return (char *)"DESC";
85         default:
86                 return (char *)" ";
87         }
88 }
89
90 static char *__get_collate_str(media_content_collation_e collate_type)
91 {
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";
100                 else
101                         return (char *)"localized";
102         default: return (char *)" ";
103         }
104 }
105
106 static void __filter_attribute_free_value(gpointer key, gpointer value, gpointer user_data)
107 {
108         SAFE_G_FREE(key);
109         SAFE_G_FREE(value);
110 }
111
112 static char *__media_filter_replace_attr(attribute_h attr, char *name)
113 {
114         char *key_temp = NULL;
115         char *generated_value = NULL;
116         attribute_s *_attr = (attribute_s *)attr;
117
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); */
121                 return NULL;
122         }
123
124         if (STRING_VALID(generated_value))
125                 return strdup(generated_value);
126
127         media_content_error("__media_filter_replace_attr fail");
128
129         return NULL;
130 }
131
132 static int __tokenize_operator(token_t *token, const char *str, int op_type)
133 {
134         int ret = 0;
135         const char *tmp = str;
136
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);
141
142                 token->str = (char*)calloc(token_size+1, sizeof(char));
143                 media_content_retvm_if(token->str == NULL, -1, "OUT_OF_MEMORY");
144
145                 strncpy(token->str, tmp, token_size);
146                 /*media_content_debug("type : [%d] str : [%s]", token->type, token->str);*/
147                 ret = token_size;
148         } else {
149                 ret = -1;
150         }
151
152         return ret;
153 }
154
155 static int __tokenize_string(token_t *token, const char *str, int size)
156 {
157         int ret = size;
158         const char *tmp = str;
159
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");
163
164                 token->type = UNKNOWN_TYPE;
165                 strncpy(token->str, tmp, size);
166                 /*media_content_debug("type : [%d] str : [%s]", token->type, token->str);*/
167         } else {
168                 ret = -1;
169         }
170
171         return ret;
172 }
173
174 static int __tokenize_attribute(GList **token_list, const char *str)
175 {
176         int ret = 0;
177         int idx = 0;
178
179         media_content_retvm_if(!STRING_VALID(str), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Parameter string is invalid");
180
181         const char *tmp = str;
182         const char *dst_ptr = str + strlen(str);
183
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. */
188                                 tmp++;
189                                 idx = -1;
190                                 continue;
191                         }
192                         token_t *token = (token_t*)calloc(1, sizeof(token_t));
193                         media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
194
195                         token->type = UNKNOWN_TYPE;
196                         token->str = (char*)calloc(idx+1, sizeof(char));
197                         if (token->str == NULL) {
198                                 SAFE_FREE(token);
199                                 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
200                                 return -1;
201                         }
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]);
206                         idx = -1;
207                 } else if (tmp[idx] == ',') {   /* " , " */
208                         if (idx != 0) {
209                                 token_t *token = (token_t*)calloc(1, sizeof(token_t));
210                                 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
211
212                                 ret = __tokenize_string(token, tmp, idx);
213                                 if (ret < 0) {
214                                         SAFE_FREE(token);
215                                         media_content_error("tokenize error occured");
216                                         return -1;
217                                 } else {
218                                         *token_list = g_list_append(*token_list, token);
219                                         tmp = tmp + idx;
220                                 }
221                         }
222
223                         token_t *token = (token_t*)calloc(1, sizeof(token_t));
224                         int size = __tokenize_operator(token, tmp, 3);
225
226                         if (token != NULL && STRING_VALID(token->str)) {
227                                 *token_list = g_list_append(*token_list, token);
228                                 tmp += size;
229                                 idx = -1;
230                         } else {
231                                 SAFE_FREE(token);
232                                 media_content_error("tokenize error occured");
233                                 return -1;
234                         }
235                 }
236         }
237
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");
241
242                 ret = __tokenize_string(token, tmp, idx);
243                 if (ret < 0) {
244                         SAFE_FREE(token);
245                         media_content_error("tokenize error occured");
246                         return -1;
247                 }
248
249                 if (token != NULL && STRING_VALID(token->str)) {
250                         *token_list = g_list_append(*token_list, token);
251                 } else {
252                         SAFE_FREE(token);
253                         media_content_error("tokenize error occured");
254                         return -1;
255                 }
256         }
257
258         return MEDIA_CONTENT_ERROR_NONE;
259 }
260
261 static int __tokenize(GList **token_list, const char *str)
262 {
263         int ret = 0;
264         int idx = 0;
265
266         media_content_retvm_if(!STRING_VALID(str), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Parameter string is invalid");
267
268         const char *tmp = str;
269         const char *dst_ptr = str + strlen(str);
270
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. */
275                                 tmp++;
276                                 idx = -1;
277                                 continue;
278                         }
279
280                         token_t *token = (token_t*)calloc(1, sizeof(token_t));
281                         media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
282
283                         token->type = UNKNOWN_TYPE;
284                         token->str = (char *)calloc(idx+1, sizeof(char));
285                         if (token->str == NULL) {
286                                 SAFE_FREE(token);
287                                 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
288                                 return -1;
289                         }
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]);
294                         idx = -1;
295                 } else if (tmp[idx] == media_token[1][0]) {     /* " \" " */
296                         int j;
297                         bool flag = false;
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]) {
300                                         j += 1;
301                                         continue;
302                                 }
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");
306
307                                         token->str = (char*) calloc(j+1+1, sizeof(char));
308                                         if (token->str == NULL) {
309                                                 SAFE_FREE(token);
310                                                 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
311                                                 return -1;
312                                         }
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);
318                                         idx = -1;
319                                         flag = true;
320                                         break;
321                                 }
322                         }
323
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");
327
328                                 token->str = (char *) calloc(j+1, sizeof(char));
329                                 if (token->str == NULL) {
330                                         SAFE_FREE(token);
331                                         media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
332                                         return -1;
333                                 }
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);
339                                 idx = -1;
340                         }
341                 } else if (tmp[idx] == media_token[2][0]) {     /* " \' "*/
342                         int j;
343                         bool flag = false;
344                         for (j = idx+1; tmp[j]; j++) {
345                                 if (tmp[j] == media_token[2][0] && tmp[j+1] == media_token[2][0]) {
346                                         j += 1;
347                                         continue;
348                                 }
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");
352
353                                         token->str = (char *) calloc(j+1+1, sizeof(char));
354                                         if (token->str == NULL) {
355                                                 SAFE_FREE(token);
356                                                 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
357                                                 return -1;
358                                         }
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);
364                                         idx = -1;
365                                         flag = true;
366                                         break;
367                                 }
368                         }
369
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");
373
374                                 token->str = (char*) calloc(j+1, sizeof(char));
375                                 if (token->str == NULL) {
376                                         SAFE_FREE(token);
377                                         media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
378                                         return -1;
379                                 }
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);
385                                 idx = -1;
386                         }
387                 } else if (tmp[idx] == media_token[3][0]) {     /* "(" */
388                         if (idx != 0) {
389                                 token_t *token = (token_t*)calloc(1, sizeof(token_t));
390                                 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
391
392                                 ret = __tokenize_string(token, tmp, idx);
393                                 if (ret < 0) {
394                                         SAFE_FREE(token);
395                                         media_content_error("tokenize error occured");
396                                         return -1;
397                                 } else {
398                                         *token_list = g_list_append(*token_list, token);
399                                         tmp = tmp + idx;
400                                 }
401                         }
402                         token_t *token = (token_t*)calloc(1, sizeof(token_t));
403                         int size = __tokenize_operator(token, tmp, 3);
404
405                         if (token != NULL && STRING_VALID(token->str)) {
406                                 *token_list = g_list_append(*token_list, token);
407                                 tmp += size;
408                                 idx = -1;
409                         } else {
410                                 SAFE_FREE(token);
411                                 media_content_error("tokenize error occured");
412                                 return -1;
413                         }
414
415                 } else if (tmp[idx] == media_token[4][0]) {     /* ")" */
416                         if (idx != 0) {
417                                 token_t *token = (token_t*)calloc(1, sizeof(token_t));
418                                 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
419
420                                 ret = __tokenize_string(token, tmp, idx);
421                                 if (ret < 0) {
422                                         SAFE_FREE(token);
423                                         media_content_error("tokenize error occured");
424                                         return -1;
425                                 } else {
426                                         *token_list = g_list_append(*token_list, token);
427                                         tmp = tmp + idx;
428                                 }
429                         }
430                         token_t *token = (token_t*)calloc(1, sizeof(token_t));
431                         int size = __tokenize_operator(token, tmp, 4);
432
433                         if (token != NULL && STRING_VALID(token->str)) {
434                                 *token_list = g_list_append(*token_list, token);
435                                 tmp += size;
436                                 idx = -1;
437                         } else {
438                                 SAFE_FREE(token);
439                                 media_content_error("tokenize error occured");
440                                 return -1;
441                         }
442
443                 } else if (tmp[idx] == media_token[5][0]) {     /* "=" */
444                         if (idx != 0) {
445                                 token_t *token = (token_t*)calloc(1, sizeof(token_t));
446                                 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
447
448                                 ret = __tokenize_string(token, tmp, idx);
449                                 if (ret < 0) {
450                                         SAFE_FREE(token);
451                                         media_content_error("tokenize error occured");
452                                         return -1;
453                                 } else {
454                                         *token_list = g_list_append(*token_list, token);
455                                         tmp = tmp + idx;
456                                 }
457                         }
458                         token_t *token = (token_t*)calloc(1, sizeof(token_t));
459                         int size = __tokenize_operator(token, tmp, 5);
460
461                         if (token != NULL && STRING_VALID(token->str)) {
462                                 *token_list = g_list_append(*token_list, token);
463                                 tmp += size;
464                                 idx = -1;
465                         } else {
466                                 SAFE_FREE(token);
467                                 media_content_error("tokenize error occured");
468                                 return -1;
469                         }
470
471                 } else if (tmp[idx] == media_token[6][0] && tmp[idx+1] == media_token[6][1]) {  /* "<=", */
472                         if (idx != 0) {
473                                 token_t *token = (token_t*)calloc(1, sizeof(token_t));
474                                 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
475
476                                 ret = __tokenize_string(token, tmp, idx);
477                                 if (ret < 0) {
478                                         SAFE_FREE(token);
479                                         media_content_error("tokenize error occured");
480                                         return -1;
481                                 } else {
482                                         *token_list = g_list_append(*token_list, token);
483                                         tmp = tmp + idx;
484                                 }
485                         }
486                         token_t *token = (token_t*)calloc(1, sizeof(token_t));
487                         int size = __tokenize_operator(token, tmp, 6);
488
489                         if (token != NULL && STRING_VALID(token->str)) {
490                                 *token_list = g_list_append(*token_list, token);
491                                 tmp += size;
492                                 idx = -1;
493                         } else {
494                                 SAFE_FREE(token);
495                                 media_content_error("tokenize error occured");
496                                 return -1;
497                         }
498
499                 } else if (tmp[idx] == media_token[7][0]) {     /* "<", */
500                         if (idx != 0) {
501                                 token_t *token = (token_t*)calloc(1, sizeof(token_t));
502                                 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
503
504                                 ret = __tokenize_string(token, tmp, idx);
505                                 if (ret < 0) {
506                                         SAFE_FREE(token);
507                                         media_content_error("tokenize error occured");
508                                         return -1;
509                                 } else {
510                                         *token_list = g_list_append(*token_list, token);
511                                         tmp = tmp + idx;
512                                 }
513                         }
514                         token_t *token = (token_t*)calloc(1, sizeof(token_t));
515                         int size = __tokenize_operator(token, tmp, 7);
516
517                         if (token != NULL && STRING_VALID(token->str)) {
518                                 *token_list = g_list_append(*token_list, token);
519                                 tmp += size;
520                                 idx = -1;
521                         } else {
522                                 SAFE_FREE(token);
523                                 media_content_error("tokenize error occured");
524                                 return -1;
525                         }
526
527                 } else if (tmp[idx] == media_token[8][0] && tmp[idx+1] == media_token[8][1]) {  /* ">=", */
528                         if (idx != 0) {
529                                 token_t *token = (token_t*)calloc(1, sizeof(token_t));
530                                 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
531
532                                 ret = __tokenize_string(token, tmp, idx);
533                                 if (ret < 0) {
534                                         SAFE_FREE(token);
535                                         media_content_error("tokenize error occured");
536                                         return -1;
537                                 } else {
538                                         *token_list = g_list_append(*token_list, token);
539                                         tmp = tmp + idx;
540                                 }
541                         }
542                         token_t *token = (token_t*)calloc(1, sizeof(token_t));
543                         int size = __tokenize_operator(token, tmp, 8);
544
545                         if (token != NULL && STRING_VALID(token->str)) {
546                                 *token_list = g_list_append(*token_list, token);
547                                 tmp += size;
548                                 idx = -1;
549                         } else {
550                                 SAFE_FREE(token);
551                                 media_content_error("tokenize error occured");
552                                 return -1;
553                         }
554
555                 } else if (tmp[idx] == media_token[9][0]) {     /* ">", */
556                         if (idx != 0) {
557                                 token_t *token = (token_t*)calloc(1, sizeof(token_t));
558                                 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
559
560                                 ret = __tokenize_string(token, tmp, idx);
561                                 if (ret < 0) {
562                                         SAFE_FREE(token);
563                                         media_content_error("tokenize error occured");
564                                         return -1;
565                                 } else {
566                                         *token_list = g_list_append(*token_list, token);
567                                         tmp = tmp + idx;
568                                 }
569                         }
570                         token_t *token = (token_t*)calloc(1, sizeof(token_t));
571                         int size = __tokenize_operator(token, tmp, 9);
572
573                         if (token != NULL && STRING_VALID(token->str)) {
574                                 *token_list = g_list_append(*token_list, token);
575                                 tmp += size;
576                                 idx = -1;
577                         } else {
578                                 SAFE_FREE(token);
579                                 media_content_error("tokenize error occured");
580                                 return -1;
581                         }
582                 } else if (tmp[idx] == media_token[10][0] && tmp[idx+1] == media_token[10][1]) {        /* "!=", */
583                         if (idx != 0) {
584                                 token_t *token = (token_t*)calloc(1, sizeof(token_t));
585                                 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
586
587                                 ret = __tokenize_string(token, tmp, idx);
588                                 if (ret < 0) {
589                                         SAFE_FREE(token);
590                                         media_content_error("tokenize error occured");
591                                         return -1;
592                                 } else {
593                                         *token_list = g_list_append(*token_list, token);
594                                         tmp = tmp + idx;
595                                 }
596                         }
597                         token_t *token = (token_t*)calloc(1, sizeof(token_t));
598                         int size = __tokenize_operator(token, tmp, 10);
599
600                         if (token != NULL && STRING_VALID(token->str)) {
601                                 *token_list = g_list_append(*token_list, token);
602                                 tmp += size;
603                                 idx = -1;
604                         } else {
605                                 SAFE_FREE(token);
606                                 media_content_error("tokenize error occured");
607                                 return -1;
608                         }
609                 }
610         }
611
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");
615
616                 ret = __tokenize_string(token, tmp, idx);
617                 if (ret < 0) {
618                         SAFE_FREE(token);
619                         media_content_error("tokenize error occured");
620                         return -1;
621                 }
622
623                 if (token != NULL && STRING_VALID(token->str)) {
624                         *token_list = g_list_append(*token_list, token);
625                 } else {
626                         SAFE_FREE(token);
627                         media_content_error("tokenize error occured");
628                         return -1;
629                 }
630         }
631
632         return MEDIA_CONTENT_ERROR_NONE;
633 }
634
635 int _media_filter_attribute_create(attribute_h *attr)
636 {
637         int ret = MEDIA_CONTENT_ERROR_NONE;
638
639         media_content_retvm_if(attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
640
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");
643
644         _attr->attr_map = g_hash_table_new(g_str_hash, g_str_equal);
645         *attr = (attribute_h)_attr;
646
647         return ret;
648 }
649
650 int _media_filter_attribute_add(attribute_h attr, const char *user_attr, const char *platform_attr)
651 {
652         int ret = MEDIA_CONTENT_ERROR_NONE;
653         char *_user = NULL;
654         char *_platform = NULL;
655         attribute_s *_attr = (attribute_s*)attr;
656
657         media_content_retvm_if(_attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
658
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");
662
663                 _platform = g_strdup(platform_attr);
664                 if (_platform == NULL) {
665                         SAFE_G_FREE(_user);
666                         media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
667                         return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
668                 }
669
670                 g_hash_table_insert(_attr->attr_map, _user, _platform);
671         } else {
672                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
673                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
674         }
675
676         return ret;
677 }
678
679 int _media_filter_attribute_destory(attribute_h attr)
680 {
681         int ret = MEDIA_CONTENT_ERROR_NONE;
682         attribute_s *_attr = (attribute_s*)attr;
683
684         media_content_retvm_if(_attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
685
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);
689         }
690
691         SAFE_FREE(_attr);
692
693         return ret;
694 }
695
696 int _media_filter_attribute_generate(attribute_h attr, filter_h filter, char **generated_condition)
697 {
698         unsigned int idx = 0;
699         GList *token_list = NULL;
700         int ret = MEDIA_CONTENT_ERROR_NONE;
701         token_t *token;
702         filter_s *_filter = NULL;
703         char tmp_condition[MAX_QUERY_SIZE] = {0, };
704
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");
708
709         _filter = (filter_s*)filter;
710
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");
714
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;
718         }
719
720         for (idx = 0; idx < g_list_length(token_list); idx++) {
721                 token = (token_t*)g_list_nth_data(token_list, idx);
722
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;
728                         }
729                 }
730         }
731
732         /* make the statment */
733         memset(tmp_condition, 0, sizeof(tmp_condition));
734         SAFE_STRLCAT(tmp_condition, QUERY_KEYWORD_OPEN_BRACKET, sizeof(tmp_condition));
735
736         for (idx = 0; idx < g_list_length(token_list); idx++) {
737                 token = (token_t*)g_list_nth_data(token_list, idx);
738
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));
742
743                         SAFE_FREE(token->str);
744                         SAFE_FREE(token);
745                 }
746         }
747
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));
753         }
754
755         SAFE_STRLCAT(tmp_condition, QUERY_KEYWORD_BRACKET, sizeof(tmp_condition));
756
757         if (STRING_VALID(tmp_condition))
758                 *generated_condition = g_strdup(tmp_condition);
759         else
760                 *generated_condition = NULL;
761
762         media_content_sec_debug("Condition : %s", *generated_condition);
763
764         if (token_list != NULL) {
765                 g_list_free(token_list);
766                 token_list = NULL;
767         }
768
769         return ret;
770 }
771
772 int _media_filter_attribute_option_generate(attribute_h attr, filter_h filter, char **generated_option)
773 {
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, };
778
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");
781
782         _filter = (filter_s*)filter;
783
784         memset(query, 0x00, sizeof(query));
785
786         /* Order by*/
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;
791                         token_t *token;
792                         char *attr_str;
793
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;
797                         }
798
799                         for (idx = 0; idx < g_list_length(token_list); idx++) {
800                                 token = (token_t*)g_list_nth_data(token_list, idx);
801
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);
809                                                         continue;
810                                                 }
811
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));
814                                                 else
815                                                         snprintf(attr_str, strlen(replace_str) + COLLATE_STR_SIZE + 1, "%s %s", replace_str, __get_order_str(_filter->order_type));
816
817                                                 SAFE_FREE(token->str);
818                                                 token->str = attr_str;
819                                                 SAFE_FREE(replace_str);
820                                         } else {
821                                                 media_content_error("There is no matched db field for %s", token->str);
822                                         }
823                                 }
824                         }
825
826                         /* make the statment */
827                         SAFE_STRLCAT(query, QUERY_KEYWORD_ORDER_BY, sizeof(query));
828
829                         for (idx = 0; idx < g_list_length(token_list); idx++) {
830                                 token = (token_t*)g_list_nth_data(token_list, idx);
831
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));
835
836                                         SAFE_FREE(token->str);
837                                         SAFE_FREE(token);
838                                 }
839                         }
840
841                         if (token_list != NULL) {
842                                 g_list_free(token_list);
843                                 token_list = NULL;
844                         }
845                 } else {
846                         SAFE_STRLCAT(query, _filter->order_keyword, sizeof(query));
847                 }
848         }
849
850         /* offset */
851         SAFE_STRLCAT(query, QUERY_KEYWORD_SPACE, sizeof(query));
852
853         memset(option, 0, sizeof(option));
854         snprintf(option, sizeof(option), "%s %d, %d", QUERY_KEYWORD_LIMIT, _filter->offset, _filter->count);
855
856         SAFE_STRLCAT(query, option, sizeof(query));
857
858         if (STRING_VALID(query)) {
859                 *generated_option = g_strdup(query);
860                 media_content_sec_debug("Option : %s", *generated_option);
861         } else {
862                 *generated_option = NULL;
863         }
864
865         return ret;
866 }
867
868 int _media_filter_attribute_option_generate_with_full_query(attribute_h attr, filter_h filter, char **generated_option)
869 {
870         unsigned int idx = 0;
871         GList *token_list = NULL;
872         int ret = MEDIA_CONTENT_ERROR_NONE;
873         token_t *token;
874         filter_s * _filter = NULL;
875         char query[DEFAULT_QUERY_SIZE] = {0, };
876         char option[DEFAULT_QUERY_SIZE] = {0, };
877
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");
881
882         _filter = (filter_s*)filter;
883
884         memset(query, 0, sizeof(query));
885
886         /* Order by*/
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;
891                 }
892
893                 for (idx = 0; idx < g_list_length(token_list); idx++) {
894                         token = (token_t*)g_list_nth_data(token_list, idx);
895
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;
901                                 }
902                         }
903                 }
904
905                 /* make the statment */
906                 SAFE_STRLCAT(query, QUERY_KEYWORD_ORDER_BY, sizeof(query));
907
908                 for (idx = 0; idx < g_list_length(token_list); idx++) {
909                         token = (token_t*)g_list_nth_data(token_list, idx);
910
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));
914
915                                 SAFE_FREE(token->str);
916                                 SAFE_FREE(token);
917                         }
918                 }
919
920                 if (token_list != NULL) {
921                         g_list_free(token_list);
922                         token_list = NULL;
923                 }
924         }
925
926         /* offset */
927         SAFE_STRLCAT(query, QUERY_KEYWORD_SPACE, sizeof(query));
928
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));
932
933         if (STRING_VALID(query)) {
934                 *generated_option = g_strdup(query);
935                 media_content_sec_debug("Option : %s", *generated_option);
936         } else {
937                 *generated_option = NULL;
938         }
939
940         return ret;
941 }
942
943
944 int media_filter_create(filter_h *filter)
945 {
946         int ret = MEDIA_CONTENT_ERROR_NONE;
947
948         media_content_retvm_if(filter == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid filter");
949
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");
952
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;
960         _filter->count = -1;
961         _filter->is_full_condition = false;
962         _filter->is_full_order = false;
963
964         *filter = (filter_h)_filter;
965
966         return ret;
967 }
968
969 int media_filter_destroy(filter_h filter)
970 {
971         int ret = MEDIA_CONTENT_ERROR_NONE;
972         filter_s *_filter = (filter_s*)filter;
973
974         if (_filter) {
975                 SAFE_FREE(_filter->storage_id);
976                 SAFE_FREE(_filter->condition);
977                 SAFE_FREE(_filter->order_keyword);
978                 SAFE_FREE(_filter);
979
980                 ret = MEDIA_CONTENT_ERROR_NONE;
981         } else {
982                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
983                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
984         }
985
986         return ret;
987 }
988
989 int media_filter_set_offset(filter_h filter, int offset, int count)
990 {
991         int ret = MEDIA_CONTENT_ERROR_NONE;
992         filter_s *_filter = (filter_s*)filter;
993
994         if (_filter != NULL) {
995                 _filter->offset = offset;
996                 _filter->count = count;
997         } else {
998                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
999                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1000         }
1001
1002         return ret;
1003 }
1004
1005 int media_filter_set_condition(filter_h filter, const char *condition, media_content_collation_e collate_type)
1006 {
1007         int ret = MEDIA_CONTENT_ERROR_NONE;
1008         filter_s *_filter = (filter_s*)filter;
1009
1010         if ((_filter != NULL) && STRING_VALID(condition)
1011                 && ((collate_type >= MEDIA_CONTENT_COLLATE_DEFAULT) && (collate_type <= MEDIA_CONTENT_COLLATE_LOCALIZED))) {
1012
1013                 _filter->is_full_condition = false;
1014
1015                 if (STRING_VALID(_filter->condition))
1016                         SAFE_FREE(_filter->condition);
1017
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");
1022
1023                 _filter->condition = strdup(new_condition);
1024                 media_content_retvm_if(_filter->condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1025
1026                 media_content_sec_debug("Condition string : %s", _filter->condition);
1027
1028                 _filter->condition_collate_type = collate_type;
1029         } else {
1030                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1031                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1032         }
1033
1034         return ret;
1035 }
1036
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)
1038 {
1039         int ret = MEDIA_CONTENT_ERROR_NONE;
1040         filter_s *_filter = (filter_s*)filter;
1041
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))) {
1045
1046                 _filter->is_full_order = false;
1047
1048                 SAFE_FREE(_filter->order_keyword);
1049
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");
1052
1053                 _filter->order_type = order_type;
1054                 _filter->order_collate_type = collate_type;
1055         } else {
1056                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1057                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1058         }
1059
1060         return ret;
1061 }
1062
1063 int media_filter_set_storage(filter_h filter, const char *storage_id)
1064 {
1065         int ret = MEDIA_CONTENT_ERROR_NONE;
1066         filter_s *_filter = (filter_s*)filter;
1067
1068         if ((_filter != NULL) && STRING_VALID(storage_id)) {
1069                 if (STRING_VALID(_filter->storage_id))
1070                         SAFE_FREE(_filter->storage_id);
1071
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");
1074
1075                 media_content_sec_debug("storage_id : %s", _filter->storage_id);
1076         } else {
1077                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1078                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1079         }
1080
1081         return ret;
1082 }
1083
1084 int media_filter_get_offset(filter_h filter, int *offset, int *count)
1085 {
1086         int ret = MEDIA_CONTENT_ERROR_NONE;
1087         filter_s *_filter = (filter_s*)filter;
1088
1089         if (_filter) {
1090                 *offset = _filter->offset;
1091                 *count = _filter->count;
1092         } else {
1093                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1094                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1095         }
1096
1097         return ret;
1098 }
1099
1100 int media_filter_get_condition(filter_h filter, char **condition, media_content_collation_e *collate_type)
1101 {
1102         int ret = MEDIA_CONTENT_ERROR_NONE;
1103         filter_s *_filter = (filter_s*)filter;
1104
1105         if (_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");
1111
1112                         *condition = strdup(new_condition);
1113                         media_content_retvm_if(*condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1114                 } else {
1115                         *condition = NULL;
1116                 }
1117
1118                 *collate_type = _filter->condition_collate_type;
1119         } else {
1120                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1121                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1122         }
1123
1124         return ret;
1125 }
1126
1127 int media_filter_get_order(filter_h filter, media_content_order_e* order_type, char **order_keyword, media_content_collation_e *collate_type)
1128 {
1129         int ret = MEDIA_CONTENT_ERROR_NONE;
1130         filter_s *_filter = (filter_s*)filter;
1131
1132         if (_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");
1136                 } else {
1137                         *order_keyword = NULL;
1138                 }
1139
1140                 *order_type = _filter->order_type;
1141                 *collate_type = _filter->order_collate_type;
1142         } else {
1143                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1144                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1145         }
1146
1147         return ret;
1148 }
1149
1150 int media_filter_get_storage(filter_h filter, char **storage_id)
1151 {
1152         int ret = MEDIA_CONTENT_ERROR_NONE;
1153         filter_s *_filter = (filter_s*)filter;
1154
1155         if (_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");
1159                 } else {
1160                         *storage_id = NULL;
1161                 }
1162         } else {
1163                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1164                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1165         }
1166
1167         return ret;
1168 }
1169
1170 int media_filter_set_condition_v2(filter_h filter, const char *condition)
1171 {
1172         int ret = MEDIA_CONTENT_ERROR_NONE;
1173         filter_s *_filter = (filter_s*)filter;
1174
1175         if ((_filter != NULL) && STRING_VALID(condition)) {
1176                 _filter->is_full_condition = true;
1177
1178                 if (STRING_VALID(_filter->condition))
1179                         SAFE_FREE(_filter->condition);
1180
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");
1185
1186                 _filter->condition = strdup(new_condition);
1187                 media_content_retvm_if(_filter->condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1188
1189                 media_content_sec_debug("Condition string : %s", _filter->condition);
1190         } else {
1191                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1192                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1193         }
1194
1195         return ret;
1196 }
1197
1198 int media_filter_get_condition_v2(filter_h filter, char **condition)
1199 {
1200         int ret = MEDIA_CONTENT_ERROR_NONE;
1201         filter_s *_filter = (filter_s*)filter;
1202
1203         if (_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");
1209
1210                         *condition = strdup(new_condition);
1211                         media_content_retvm_if(*condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1212                 } else {
1213                         *condition = NULL;
1214                 }
1215         } else {
1216                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1217                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1218         }
1219
1220         return ret;
1221 }
1222
1223 int media_filter_set_order_v2(filter_h filter, const char *order)
1224 {
1225         int ret = MEDIA_CONTENT_ERROR_NONE;
1226         filter_s *_filter = (filter_s*)filter;
1227
1228         if ((_filter != NULL) && STRING_VALID(order)) {
1229                 _filter->is_full_order = true;
1230
1231                 SAFE_FREE(_filter->order_keyword);
1232
1233                 _filter->order_keyword = strdup(order);
1234                 media_content_retvm_if(_filter->order_keyword == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1235         } else {
1236                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1237                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1238         }
1239
1240         return ret;
1241 }
1242
1243 int media_filter_get_order_v2(filter_h filter, char **order)
1244 {
1245         int ret = MEDIA_CONTENT_ERROR_NONE;
1246         filter_s *_filter = (filter_s*)filter;
1247
1248         if (_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");
1252                 } else {
1253                         *order = NULL;
1254                 }
1255         } else {
1256                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1257                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1258         }
1259
1260         return ret;
1261 }
1262