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