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