[ACR-1308] Remove deprecated APIs
[platform/core/api/media-content.git] / src / media_db.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_content.h>
19 #include <media_info_private.h>
20 #include <media_util_private.h>
21
22 static char * __media_db_get_group_name(media_group_e group);
23 static int __media_db_make_query(filter_h filter, attribute_h attr, char *select_query, int select_query_size, char **condition_query, char **option_query);
24
25 static char * __media_db_get_group_name(media_group_e group)
26 {
27         switch (group) {
28         case MEDIA_CONTENT_GROUP_DISPLAY_NAME:
29                 return (char *)DB_FIELD_MEDIA_DISPLAY_NAME;
30         case MEDIA_CONTENT_GROUP_TYPE:
31                 return (char *)DB_FIELD_MEDIA_TYPE;
32         case MEDIA_CONTENT_GROUP_MIME_TYPE:
33                 return (char *) DB_FIELD_MEDIA_MIME_TYPE;
34         case MEDIA_CONTENT_GROUP_SIZE:
35                 return (char *) DB_FIELD_MEDIA_SIZE;
36         case MEDIA_CONTENT_GROUP_ADDED_TIME:
37                 return (char *) DB_FIELD_MEDIA_ADDED_TIME;
38         case MEDIA_CONTENT_GROUP_MODIFIED_TIME:
39                 return (char *) DB_FIELD_MEDIA_MODIFIED_TIME;
40         case MEDIA_CONTENT_GROUP_TITLE:
41                 return (char *) DB_FIELD_MEDIA_TITLE;
42         case MEDIA_CONTENT_GROUP_ARTIST:
43                 return (char *) DB_FIELD_MEDIA_ARTIST;
44         case MEDIA_CONTENT_GROUP_ALBUM_ARTIST:
45                 return (char *) DB_FIELD_MEDIA_ALBUM_ARTIST;
46         case MEDIA_CONTENT_GROUP_GENRE:
47                 return (char *) DB_FIELD_MEDIA_GENRE;
48         case MEDIA_CONTENT_GROUP_COMPOSER:
49                 return (char *) DB_FIELD_MEDIA_COMPOSER;
50         case MEDIA_CONTENT_GROUP_YEAR:
51                 return (char *) DB_FIELD_MEDIA_YEAR;
52         case MEDIA_CONTENT_GROUP_RECORDED_DATE:
53                 return (char *) DB_FIELD_MEDIA_RECORDED_DATE;
54         case MEDIA_CONTENT_GROUP_COPYRIGHT:
55                 return (char *) DB_FIELD_MEDIA_COPYRIGHT;
56         case MEDIA_CONTENT_GROUP_TRACK_NUM:
57                 return (char *) DB_FIELD_MEDIA_TRACK_NUM;
58         case MEDIA_CONTENT_GROUP_DESCRIPTION:
59                 return (char *) DB_FIELD_MEDIA_DESCRIPTION;
60         case MEDIA_CONTENT_GROUP_LONGITUDE:
61                 return (char *) DB_FIELD_MEDIA_LONGITUDE;
62         case MEDIA_CONTENT_GROUP_LATITUDE:
63                 return (char *) DB_FIELD_MEDIA_LATITUDE;
64         case MEDIA_CONTENT_GROUP_ALTITUDE:
65                 return (char *) DB_FIELD_MEDIA_ALTITUDE;
66         case MEDIA_CONTENT_GROUP_RATING:
67                 return (char *) DB_FIELD_MEDIA_RATING;
68 #ifdef _USE_TVPD_MODE
69         case MEDIA_CONTENT_GROUP_MODIFIED_MONTH:
70                 return (char *) DB_FIELD_MEDIA_MODIFIED_DATE;
71         case MEDIA_CONTENT_GROUP_MODIFIED_DATE:
72                 return (char *) DB_FIELD_MEDIA_MODIFIED_DATE;
73 #endif
74 #ifdef _USE_SENIOR_MODE
75         case MEDIA_CONTENT_GROUP_CONTACT:
76                 return (char *) DB_FIELD_MEDIA_CONTACT;
77 #endif
78 #ifdef _USE_TVPD_MODE
79         case MEDIA_CONTENT_GROUP_ALBUM:
80                 return (char *) DB_FIELD_MEDIA_ALBUM;
81         case MEDIA_PVR_GROUP_DURATION:
82                 return (char *) DB_FIELD_PVR_DURATION;
83         case MEDIA_PVR_GROUP_TIME_ZONE:
84                 return (char *) DB_FIELD_PVR_TIME_ZONE;
85         case MEDIA_PVR_GROUP_PTC:
86                 return (char *) DB_FIELD_PVR_PTC;
87         case MEDIA_PVR_GROUP_MAJOR:
88                 return (char *) DB_FIELD_PVR_MAJOR;
89         case MEDIA_PVR_GROUP_MINOR:
90                 return (char *) DB_FIELD_PVR_MINOR;
91         case MEDIA_PVR_GROUP_CHANNEL_TYPE:
92                 return (char *) DB_FIELD_PVR_CHANNEL_TYPE;
93         case MEDIA_PVR_GROUP_CHANNEL_NAME:
94                 return (char *) DB_FIELD_PVR_CHANNEL_NAME;
95         case MEDIA_PVR_GROUP_CHANNEL_NUM:
96                 return (char *) DB_FIELD_PVR_CHANNEL_NUM;
97         case MEDIA_PVR_GROUP_PROGRAM_TITLE:
98                 return (char *) DB_FIELD_PVR_PROGRAM_TITLE;
99         case MEDIA_PVR_GROUP_PROGRAM_NUM:
100                 return (char *) DB_FIELD_PVR_PROGRAM_NUM;
101         case MEDIA_PVR_GROUP_PROGRAM_CRID:
102                 return (char *) DB_FIELD_PVR_PROGRAM_CRID;
103         case MEDIA_PVR_GROUP_GUIDANCE:
104                 return (char *) DB_FIELD_PVR_GUIDANCE;
105         case MEDIA_PVR_GROUP_SYNOPSIS:
106                 return (char *) DB_FIELD_PVR_SYNOPSIS;
107         case MEDIA_PVR_GROUP_GENRE:
108                 return (char *) DB_FIELD_PVR_GENRE;
109         case MEDIA_PVR_GROUP_LANGUAGE:
110                 return (char *) DB_FIELD_PVR_LANGUAGE;
111         case MEDIA_PVR_GROUP_EMBARGO_TIME:
112                 return (char *) DB_FIELD_PVR_EMBARGO_TIME;
113         case MEDIA_PVR_GROUP_EXPIRY_TIME:
114                 return (char *) DB_FIELD_PVR_EXPIRY_TIME;
115         case MEDIA_PVR_GROUP_START_TIME:
116                 return (char *) DB_FIELD_PVR_START_TIME;
117         case MEDIA_PVR_GROUP_PROGRAM_START_TIME:
118                 return (char *) DB_FIELD_PVR_PROGRAM_START_TIME;
119         case MEDIA_PVR_GROUP_PROGRAM_END_TIME:
120                 return (char *) DB_FIELD_PVR_PROGRAM_END_TIME;
121         case MEDIA_PVR_GROUP_PROGRAM_DATE:
122                 return (char *) DB_FIELD_PVR_PROGRAM_DATE;
123         case MEDIA_PVR_GROUP_PARENTAL_RATING:
124                 return (char *) DB_FIELD_PVR_PARENTAL_RATING;
125         case MEDIA_PVR_GROUP_TIMER_RECORD:
126                 return (char *) DB_FIELD_PVR_TIMER_RECORD;
127         case MEDIA_PVR_GROUP_SERIES_RECORD:
128                 return (char *) DB_FIELD_PVR_SERIES_RECORD;
129         case MEDIA_PVR_GROUP_HD:
130                 return (char *) DB_FIELD_PVR_HD;
131         case MEDIA_PVR_GROUP_SUBTITLE:
132                 return (char *) DB_FIELD_PVR_SUBTITLE;
133         case MEDIA_PVR_GROUP_TTX:
134                 return (char *) DB_FIELD_PVR_TTX;
135         case MEDIA_PVR_GROUP_AD:
136                 return (char *) DB_FIELD_PVR_AD;
137         case MEDIA_PVR_GROUP_HARDOF_HEARINGRADIO:
138                 return (char *) DB_FIELD_PVR_HARDOF_HEARINGRADIO;
139         case MEDIA_PVR_GROUP_DATA_SERVICE:
140                 return (char *) DB_FIELD_PVR_DATA_SERVICE;
141         case MEDIA_PVR_GROUP_CONTENT_LOCK:
142                 return (char *) DB_FIELD_PVR_CONTENT_LOCK;
143         case MEDIA_PVR_GROUP_CONTENT_WATCH:
144                 return (char *) DB_FIELD_PVR_CONTENT_WATCH;
145         case MEDIA_PVR_GROUP_HAS_AUDIO_ONLY:
146                 return (char *) DB_FIELD_PVR_HAS_AUDIO_ONLY;
147         case MEDIA_PVR_GROUP_IS_LOCAL_RECORDED:
148                 return (char *) DB_FIELD_PVR_IS_LOCAL_RECORD;
149         case MEDIA_PVR_GROUP_RESOLUTION:
150                 return (char *) DB_FIELD_PVR_RESOLUTION;
151         case MEDIA_PVR_GROUP_ASPECTRATIO:
152                 return (char *) DB_FIELD_PVR_ASPECTRATIO;
153         case MEDIA_PVR_GROUP_MODIFIED_MONTH:
154                 return (char *) DB_FIELD_PVR_MODIFIED_DATE;
155         case MEDIA_PVR_GROUP_MODIFIED_DATE:
156                 return (char *) DB_FIELD_PVR_MODIFIED_DATE;
157         case MEDIA_PVR_GROUP_SPORTS_TYPE:
158                 return (char *) DB_FIELD_PVR_SPORTS_TYPE;
159         case MEDIA_PVR_GROUP_GUIDANCE_LENGTH:
160                 return (char *) DB_FIELD_PVR_GUIDANCE_LENGTH;
161         case MEDIA_PVR_GROUP_TVMODE:
162                 return (char *) DB_FIELD_PVR_TVMODE;
163         case MEDIA_PVR_GROUP_PLAY_COUNT:
164                 return (char *) DB_FIELD_PVR_PLAY_COUNT;
165         case MEDIA_PVR_GROUP_PRIVATE_DATA:
166                 return (char *) DB_FIELD_PVR_PRIVATE_DATA;
167         case MEDIA_UHD_GROUP_CONTENT_TITLE:
168                 return (char *) DB_FIELD_UHD_CONTENT_TITLE;
169         case MEDIA_UHD_GROUP_RELEASE_DATE:
170                 return (char *) DB_FIELD_UHD_RELEASE_DATE;
171         case MEDIA_UHD_GROUP_SUB_TYPE:
172                 return (char *) DB_FIELD_UHD_SUB_TYPE;
173         case MEDIA_UHD_GROUP_FILE_NAME:
174                 return (char *) DB_FIELD_UHD_FILE_NAME;
175         case MEDIA_UHD_GROUP_PLAYED_COUNT:
176                 return (char *) DB_FIELD_UHD_PLAYED_COUNT;
177 #endif
178         default:
179                 return NULL;
180         }
181
182         return NULL;
183 }
184
185 static int __media_db_make_query(filter_h filter, attribute_h attr, char *select_query, int select_query_size, char **condition_query, char **option_query)
186 {
187         int ret = MEDIA_CONTENT_ERROR_NONE;
188         filter_s *_filter = NULL;
189
190         if (filter != NULL) {
191                 _filter = (filter_s*)filter;
192
193                 if (STRING_VALID(_filter->condition)) {
194                         ret = _media_filter_attribute_generate(attr, filter, condition_query);
195                         media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
196                 }
197
198                 if (_filter->is_full_order == false) {
199                         /* Process for filter v1 */
200                         ret = _media_filter_attribute_option_generate(attr, filter, option_query);
201                         if (ret != MEDIA_CONTENT_ERROR_NONE) {
202                                 SAFE_FREE(*condition_query);
203                                 return ret;
204                         }
205                 } else {
206                         /* Process for filter v2 */
207                         ret = _media_filter_attribute_option_generate_with_full_query(attr, filter, option_query);
208                         if (ret != MEDIA_CONTENT_ERROR_NONE) {
209                                 SAFE_FREE(*condition_query);
210                                 return ret;
211                         }
212                 }
213
214                 if (STRING_VALID(*condition_query)) {
215                         SAFE_STRLCAT(select_query, QUERY_KEYWORD_SPACE, select_query_size);
216                         SAFE_STRLCAT(select_query, QUERY_KEYWORD_AND, select_query_size);
217                 }
218         }
219
220         return ret;
221 }
222
223 int _media_db_get_group_count(filter_h filter, group_list_e group_type, int *group_count)
224 {
225         int ret = MEDIA_CONTENT_ERROR_NONE;
226         sqlite3_stmt *stmt = NULL;
227         char select_query[MAX_QUERY_SIZE] = {0, };
228         char *condition_query = NULL;
229         char *option_query = NULL;
230         attribute_h attr = NULL;
231         filter_s *_filter = (filter_s*)filter;
232
233         attr = _content_get_attirbute_handle();
234         memset(select_query, 0x00, sizeof(select_query));
235
236         switch (group_type) {
237         case MEDIA_GROUP_NONE:
238         case MEDIA_GROUP_TAG_BY_MEDIA_ID:
239         case MEDIA_GROUP_BOOKMARK_BY_MEDIA_ID:
240         case MEDIA_GROUP_FACE_BY_MEDIA_ID:
241                 {
242                         media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
243                         return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
244                 }
245                 break;
246         case MEDIA_GROUP_ALBUM:
247                 attr = _content_get_alias_attirbute_handle();
248
249                 if ((_filter != NULL) && STRING_VALID(_filter->storage_id))
250                         snprintf(select_query, sizeof(select_query), SELECT_ALBUM_COUNT, _filter->storage_id);
251                 else
252                         snprintf(select_query, sizeof(select_query), SELECT_ALBUM_COUNT, DB_VIEW_MEDIA);
253
254                 break;
255         case MEDIA_GROUP_FOLDER:
256                 attr = _content_get_alias_attirbute_handle();
257
258                 if ((_filter != NULL) && STRING_VALID(_filter->storage_id))
259                         snprintf(select_query, sizeof(select_query), SELECT_FOLDER_COUNT_BY_STORAGE_ID, _filter->storage_id, _filter->storage_id);
260                 else
261                         snprintf(select_query, sizeof(select_query), SELECT_FOLDER_COUNT, DB_VIEW_MEDIA);
262
263                 break;
264         case MEDIA_GROUP_PLAYLIST:
265                 if (!SAFE_STRLCPY(select_query, SELECT_PLAYLIST_COUNT, sizeof(select_query))) {
266                         media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
267                         return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
268                 }
269                 break;
270         case MEDIA_GROUP_TAG:
271                 if (!SAFE_STRLCPY(select_query, SELECT_TAG_COUNT, sizeof(select_query))) {
272                         media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
273                         return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
274                 }
275                 break;
276         case MEDIA_GROUP_BOOKMARK:
277                 attr = _content_get_alias_attirbute_handle();
278
279                 if ((_filter != NULL) && STRING_VALID(_filter->storage_id))
280                         snprintf(select_query, sizeof(select_query), SELECT_BOOKMARK_COUNT, _filter->storage_id);
281                 else
282                         snprintf(select_query, sizeof(select_query), SELECT_BOOKMARK_COUNT, DB_VIEW_MEDIA);
283
284                 break;
285         case MEDIA_GROUP_STORAGE:
286                 if (!SAFE_STRLCPY(select_query, SELECT_STORAGE_COUNT, sizeof(select_query))) {
287                         media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
288                         return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
289                 }
290                 break;
291         case MEDIA_GROUP_FACE:
292                 attr = _content_get_alias_attirbute_handle();
293
294                 if ((_filter != NULL) && STRING_VALID(_filter->storage_id))
295                         snprintf(select_query, sizeof(select_query), SELECT_FACE_COUNT, _filter->storage_id);
296                 else
297                         snprintf(select_query, sizeof(select_query), SELECT_FACE_COUNT, DB_VIEW_MEDIA);
298                 break;
299 #ifdef _USE_TVPD_MODE
300         case MEDIA_GROUP_PVR:
301         case MEDIA_GROUP_UHD:
302 #endif
303         default:
304                 {
305                         media_content_error("Invalid group type [%d]", group_type);
306                         return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
307                 }
308                 break;
309         }
310
311         ret = __media_db_make_query(filter, attr, select_query, sizeof(select_query), &condition_query, &option_query);
312         media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
313
314         ret = _content_query_prepare(&stmt, select_query, condition_query, option_query);
315         SAFE_FREE(condition_query);
316         SAFE_FREE(option_query);
317         media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
318
319         if (sqlite3_step(stmt) == SQLITE_ROW)
320                 *group_count = (int)sqlite3_column_int(stmt, 0);
321
322         SQLITE3_FINALIZE(stmt);
323
324         return ret;
325 }
326
327 int _media_db_get_media_group_count(media_group_e group, filter_h filter, int *group_count)
328 {
329         int ret = MEDIA_CONTENT_ERROR_NONE;
330         sqlite3_stmt *stmt = NULL;
331         char select_query[MAX_QUERY_SIZE] = {0, };
332         char *condition_query = NULL;
333         char *option_query = NULL;
334         attribute_h attr = NULL;
335         filter_s *_filter = (filter_s*)filter;
336
337         attr = _content_get_attirbute_handle();
338         memset(select_query, 0x00, sizeof(select_query));
339 #ifdef _USE_TVPD_MODE
340         if (group < MEDIA_PVR_GROUP_DURATION) { /*media content*/
341                 if ((_filter != NULL) && STRING_VALID(_filter->storage_id))
342                         snprintf(select_query, sizeof(select_query), SELECT_MEDIA_GROUP_COUNT, __media_db_get_group_name(group), _filter->storage_id);
343                 else
344                         snprintf(select_query, sizeof(select_query), SELECT_MEDIA_GROUP_COUNT, __media_db_get_group_name(group), DB_VIEW_MEDIA);
345         } else if (group < MEDIA_UHD_GROUP_CONTENT_TITLE) { /*pvr content*/
346                 if ((_filter != NULL) && STRING_VALID(_filter->storage_id))
347                         snprintf(select_query, sizeof(select_query), SELECT_PVR_GROUP_COUNT_BY_STORAGE_ID, __media_db_get_group_name(group), _filter->storage_id);
348                 else
349                         snprintf(select_query, sizeof(select_query), SELECT_PVR_GROUP_COUNT, __media_db_get_group_name(group));
350         } else { /*uhd content*/
351                 if ((_filter != NULL) && STRING_VALID(_filter->storage_id))
352                         snprintf(select_query, sizeof(select_query), SELECT_UHD_GROUP_COUNT_BY_STORAGE_ID, __media_db_get_group_name(group), _filter->storage_id);
353                 else
354                         snprintf(select_query, sizeof(select_query), SELECT_UHD_GROUP_COUNT, __media_db_get_group_name(group));
355         }
356 #else
357         if ((_filter != NULL) && STRING_VALID(_filter->storage_id))
358                 snprintf(select_query, sizeof(select_query), SELECT_MEDIA_GROUP_COUNT, __media_db_get_group_name(group), _filter->storage_id);
359         else
360                 snprintf(select_query, sizeof(select_query), SELECT_MEDIA_GROUP_COUNT, __media_db_get_group_name(group), DB_VIEW_MEDIA);
361 #endif
362         ret = __media_db_make_query(filter, attr, select_query, sizeof(select_query), &condition_query, &option_query);
363         media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
364
365         SAFE_STRLCAT(select_query, QUERY_KEYWORD_SPACE, sizeof(select_query));
366         if (STRING_VALID(condition_query))
367                 SAFE_STRLCAT(select_query, condition_query, sizeof(select_query));
368         if (STRING_VALID(option_query))
369                 SAFE_STRLCAT(select_query, option_query, sizeof(select_query));
370         SAFE_STRLCAT(select_query, QUERY_KEYWORD_BRACKET, sizeof(select_query));
371
372         SAFE_FREE(condition_query);
373         SAFE_FREE(option_query);
374
375         ret = _content_query_prepare(&stmt, select_query, condition_query, option_query);
376         SAFE_FREE(condition_query);
377         SAFE_FREE(option_query);
378         media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
379
380         if (sqlite3_step(stmt) == SQLITE_ROW)
381                 *group_count = (int)sqlite3_column_int(stmt, 0);
382
383         SQLITE3_FINALIZE(stmt);
384
385         return ret;
386 }
387
388 int _media_db_get_media_group(media_group_e group, filter_h filter, media_group_cb callback, void *user_data)
389 {
390         int ret = MEDIA_CONTENT_ERROR_NONE;
391         char select_query[MAX_QUERY_SIZE] = {0, };
392         char *condition_query = NULL;
393         char *option_query = NULL;
394         char *name = NULL;
395         sqlite3_stmt *stmt = NULL;
396         attribute_h attr = NULL;
397         filter_s *_filter = (filter_s*)filter;
398
399         attr = _content_get_attirbute_handle();
400         memset(select_query, 0x00, sizeof(select_query));
401 #ifdef _USE_TVPD_MODE
402         if (group < MEDIA_PVR_GROUP_DURATION) { /*media content */
403                 if ((_filter != NULL) && STRING_VALID(_filter->storage_id))
404                         snprintf(select_query, sizeof(select_query), SELECT_MEDIA_GROUP_LIST, __media_db_get_group_name(group), _filter->storage_id);
405                 else
406                         snprintf(select_query, sizeof(select_query), SELECT_MEDIA_GROUP_LIST, __media_db_get_group_name(group), DB_VIEW_MEDIA);
407         } else if (group < MEDIA_UHD_GROUP_CONTENT_TITLE) { /*pvr content */
408                 if ((_filter != NULL) && STRING_VALID(_filter->storage_id))
409                         snprintf(select_query, sizeof(select_query), SELECT_PVR_GROUP_LIST_BY_STORAGE_ID, __media_db_get_group_name(group), _filter->storage_id);
410                 else
411                         snprintf(select_query, sizeof(select_query), SELECT_PVR_GROUP_LIST, __media_db_get_group_name(group));
412         } else { /* uhd content */
413                 if ((_filter != NULL) && STRING_VALID(_filter->storage_id))
414                         snprintf(select_query, sizeof(select_query), SELECT_UHD_GROUP_LIST_BY_STORAGE_ID, __media_db_get_group_name(group), _filter->storage_id);
415                 else
416                         snprintf(select_query, sizeof(select_query), SELECT_UHD_GROUP_LIST, __media_db_get_group_name(group));
417         }
418 #else
419         if ((_filter != NULL) && STRING_VALID(_filter->storage_id))
420                 snprintf(select_query, sizeof(select_query), SELECT_MEDIA_GROUP_LIST, __media_db_get_group_name(group), _filter->storage_id);
421         else
422                 snprintf(select_query, sizeof(select_query), SELECT_MEDIA_GROUP_LIST, __media_db_get_group_name(group), DB_VIEW_MEDIA);
423 #endif
424         ret = __media_db_make_query(filter, attr, select_query, sizeof(select_query), &condition_query, &option_query);
425         media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
426
427         ret = _content_query_prepare(&stmt, select_query, condition_query, option_query);
428         SAFE_FREE(condition_query);
429         SAFE_FREE(option_query);
430         media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
431
432         while (sqlite3_step(stmt) == SQLITE_ROW) {
433                 name = g_strdup((const char *)sqlite3_column_text(stmt, 0));
434
435                 if (callback(name, user_data) == false) {
436                         SAFE_FREE(name);
437                         break;
438                 }
439
440                 SAFE_FREE(name);
441         }
442
443         SQLITE3_FINALIZE(stmt);
444
445         return ret;
446 }
447 #ifdef _USE_TVPD_MODE
448 int _media_db_get_media_group_and_count(media_group_e group, filter_h filter, media_group_and_count_cb callback, void *user_data)
449 {
450         int ret = MEDIA_CONTENT_ERROR_NONE;
451         char select_query[DEFAULT_QUERY_SIZE] = {0, };
452         char group_query[DEFAULT_QUERY_SIZE] = {0, };
453         char *condition_query = NULL;
454         char *option_query = NULL;
455         char *name = NULL;
456         sqlite3_stmt *stmt = NULL;
457         attribute_h attr = NULL;
458         filter_s *_filter = (filter_s*)filter;
459
460         attr = _content_get_attirbute_handle();
461         memset(select_query, 0x00, sizeof(select_query));
462         memset(group_query, 0x00, sizeof(group_query));
463
464         char* group_name = __media_db_get_group_name(group);
465         if (!STRING_VALID(group_name)) {
466                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
467                 media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
468         }
469
470         if (group < MEDIA_PVR_GROUP_DURATION) { /*media content*/
471                 if ((_filter != NULL) && STRING_VALID(_filter->storage_id))
472                         snprintf(select_query, sizeof(select_query), SELECT_MEDIA_GROUP_AND_COUNT, group_name, _filter->storage_id);
473                 else
474                         snprintf(select_query, sizeof(select_query), SELECT_MEDIA_GROUP_AND_COUNT, group_name, DB_VIEW_MEDIA);
475         } else if (group < MEDIA_UHD_GROUP_CONTENT_TITLE) {/*pvr content*/
476                 if ((_filter != NULL) && STRING_VALID(_filter->storage_id))
477                         snprintf(select_query, sizeof(select_query), SELECT_PVR_GROUP_AND_COUNT_BY_STORAGE_ID, group_name, _filter->storage_id);
478                 else
479                         snprintf(select_query, sizeof(select_query), SELECT_PVR_GROUP_AND_COUNT, group_name);
480         } else {/*uhd content*/
481                 if ((_filter != NULL) && STRING_VALID(_filter->storage_id))
482                         snprintf(select_query, sizeof(select_query), SELECT_UHD_GROUP_AND_COUNT_BY_STORAGE_ID, group_name, _filter->storage_id);
483                 else
484                         snprintf(select_query, sizeof(select_query), SELECT_UHD_GROUP_AND_COUNT, group_name);
485         }
486
487         ret = __media_db_make_query(filter, attr, select_query, sizeof(select_query), &condition_query, &option_query);
488         media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
489
490         if (STRING_VALID(option_query))
491                 snprintf(group_query, sizeof(group_query), "GROUP BY %s %s", group_name, option_query);
492         else
493                 snprintf(group_query, sizeof(group_query), "GROUP BY %s", group_name);
494
495         ret = _content_query_prepare(&stmt, select_query, condition_query, group_query);
496         SAFE_FREE(condition_query);
497         SAFE_FREE(option_query);
498         media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
499
500         while (sqlite3_step(stmt) == SQLITE_ROW) {
501                 if (STRING_VALID((const char *)sqlite3_column_text(stmt, 0)))
502                         name = strdup((const char *)sqlite3_column_text(stmt, 0));
503
504                 if (callback(name, sqlite3_column_int(stmt, 1), user_data) == false) {
505                         SAFE_FREE(name);
506                         break;
507                 }
508
509                 SAFE_FREE(name);
510         }
511
512         SQLITE3_FINALIZE(stmt);
513
514         return ret;
515 }
516 #endif
517 int _media_db_get_album(filter_h filter, media_album_cb callback, void *user_data)
518 {
519         int ret = MEDIA_CONTENT_ERROR_NONE;
520         char select_query[MAX_QUERY_SIZE] = {0, };
521         char *condition_query = NULL;
522         char *option_query = NULL;
523         sqlite3_stmt *stmt = NULL;
524         attribute_h attr = NULL;
525         filter_s *_filter = (filter_s*)filter;
526
527         attr = _content_get_alias_attirbute_handle();
528         memset(select_query, 0x00, sizeof(select_query));
529
530         if ((_filter != NULL) && STRING_VALID(_filter->storage_id))
531                 snprintf(select_query, sizeof(select_query), SELECT_ALBUM_LIST, _filter->storage_id);
532         else
533                 snprintf(select_query, sizeof(select_query), SELECT_ALBUM_LIST, DB_VIEW_MEDIA);
534
535         ret = __media_db_make_query(filter, attr, select_query, sizeof(select_query), &condition_query, &option_query);
536         media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
537
538         ret = _content_query_prepare(&stmt, select_query, condition_query, option_query);
539         SAFE_FREE(condition_query);
540         SAFE_FREE(option_query);
541         media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
542
543         while (sqlite3_step(stmt) == SQLITE_ROW) {
544                 media_album_s *album = (media_album_s*)calloc(1, sizeof(media_album_s));
545
546                 if (album == NULL) {
547                         media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
548                         SQLITE3_FINALIZE(stmt);
549                         return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
550                 }
551
552                 album->album_id = (int)sqlite3_column_int(stmt, 0);
553                 album->name = g_strdup((const char *)sqlite3_column_text(stmt, 1));
554                 album->artist = g_strdup((const char *)sqlite3_column_text(stmt, 2));
555                 album->album_art_path = g_strdup((const char *)sqlite3_column_text(stmt, 3));
556
557                 if (callback((media_album_h)album, user_data) == false) {
558                         media_album_destroy((media_album_h)album);
559                         break;
560                 }
561
562                 media_album_destroy((media_album_h)album);
563         }
564
565         SQLITE3_FINALIZE(stmt);
566
567         return ret;
568 }
569
570 int _media_db_get_folder(filter_h filter, media_folder_cb callback, void *user_data)
571 {
572         int ret = MEDIA_CONTENT_ERROR_NONE;
573         char select_query[MAX_QUERY_SIZE] = {0, };
574         char *condition_query = NULL;
575         char *option_query = NULL;
576         sqlite3_stmt *stmt = NULL;
577         attribute_h attr = NULL;
578         filter_s *_filter = (filter_s*)filter;
579
580         attr = _content_get_alias_attirbute_handle();
581         memset(select_query, 0x00, sizeof(select_query));
582
583         if ((_filter != NULL) && STRING_VALID(_filter->storage_id))
584                 snprintf(select_query, sizeof(select_query), SELECT_FOLDER_LIST_BY_STORAGE_ID, _filter->storage_id, _filter->storage_id);
585         else
586                 snprintf(select_query, sizeof(select_query), SELECT_FOLDER_LIST, DB_VIEW_MEDIA);
587
588         ret = __media_db_make_query(filter, attr, select_query, sizeof(select_query), &condition_query, &option_query);
589         media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
590
591         ret = _content_query_prepare(&stmt, select_query, condition_query, option_query);
592
593         SAFE_FREE(condition_query);
594         SAFE_FREE(option_query);
595         media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
596
597         while (sqlite3_step(stmt) == SQLITE_ROW) {
598                 media_folder_s *_folder = (media_folder_s*)calloc(1, sizeof(media_folder_s));
599
600                 if (_folder == NULL) {
601                         media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
602                         SQLITE3_FINALIZE(stmt);
603                         return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
604                 }
605
606                 _folder->folder_id = g_strdup((const char *)sqlite3_column_text(stmt, 0));
607                 _folder->path = g_strdup((const char *)sqlite3_column_text(stmt, 1));
608                 _folder->name = g_strdup((const char *)sqlite3_column_text(stmt, 2));
609                 _folder->storage_type = (int)sqlite3_column_int(stmt, 3);
610                 _folder->storage_uuid = g_strdup((const char *)sqlite3_column_text(stmt, 4));
611
612                 if (callback((media_folder_h)_folder, user_data) == false) {
613                         media_folder_destroy((media_folder_h) _folder);
614                         break;
615                 }
616
617                 media_folder_destroy((media_folder_h) _folder);
618         }
619
620         SQLITE3_FINALIZE(stmt);
621
622         return ret;
623 }
624
625 int _media_db_get_playlist(filter_h filter, media_playlist_cb callback, void *user_data)
626 {
627         int ret = MEDIA_CONTENT_ERROR_NONE;
628         char select_query[MAX_QUERY_SIZE] = {0, };
629         char *condition_query = NULL;
630         char *option_query = NULL;
631         sqlite3_stmt *stmt = NULL;
632         attribute_h attr = NULL;
633
634         attr = _content_get_attirbute_handle();
635         memset(select_query, 0x00, sizeof(select_query));
636
637         if (!SAFE_STRLCPY(select_query, SELECT_PLAYLIST_LIST, sizeof(select_query))) {
638                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
639                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
640         }
641
642         ret = __media_db_make_query(filter, attr, select_query, sizeof(select_query), &condition_query, &option_query);
643         media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
644
645         ret = _content_query_prepare(&stmt, select_query, condition_query, option_query);
646         SAFE_FREE(condition_query);
647         SAFE_FREE(option_query);
648         media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
649
650         while (sqlite3_step(stmt) == SQLITE_ROW) {
651                 media_playlist_s *_playlist = (media_playlist_s*)calloc(1, sizeof(media_playlist_s));
652
653                 if (_playlist == NULL) {
654                         media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
655                         SQLITE3_FINALIZE(stmt);
656                         return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
657                 }
658
659                 _playlist->playlist_id = (int)sqlite3_column_int(stmt, 0);
660                 _playlist->name = g_strdup((const char *)sqlite3_column_text(stmt, 1));
661                 _playlist->thumbnail_path = g_strdup((const char *)sqlite3_column_text(stmt, 2));
662
663                 if (callback((media_playlist_h)_playlist, user_data) == false) {
664                         media_playlist_destroy((media_playlist_h)_playlist);
665                         break;
666                 }
667                 media_playlist_destroy((media_playlist_h)_playlist);
668         }
669
670         SQLITE3_FINALIZE(stmt);
671
672         return ret;
673 }
674
675 int _media_db_get_playlist_item(int playlist_id, filter_h filter, playlist_member_cb callback, void *user_data)
676 {
677         int ret = MEDIA_CONTENT_ERROR_NONE;
678         char select_query[MAX_QUERY_SIZE] = {0, };
679         char *condition_query = NULL;
680         char *option_query = NULL;
681         sqlite3_stmt *stmt = NULL;
682         attribute_h attr = NULL;
683
684         attr = _content_get_attirbute_handle();
685         memset(select_query, 0x00, sizeof(select_query));
686
687         /*snprintf(select_query, sizeof(select_query), SELECT_PLAYLIST_ITEM_ID_FROM_PLAYLIST_VIEW, playlist_id);*/
688         snprintf(select_query, sizeof(select_query), SELECT_PLAYLIST_ITEM_ALL_FROM_PLAYLIST_VIEW, playlist_id);
689
690         ret = __media_db_make_query(filter, attr, select_query, sizeof(select_query), &condition_query, &option_query);
691         media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
692
693         ret = _content_query_prepare(&stmt, select_query, condition_query, option_query);
694         SAFE_FREE(condition_query);
695         SAFE_FREE(option_query);
696         media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
697
698         while (sqlite3_step(stmt) == SQLITE_ROW) {
699                 int playlist_member_id = 0;
700                 playlist_member_id = (int)sqlite3_column_int(stmt, MEDIA_INFO_ITEM_MAX);        /*MEDIA_INFO_ITEM_MAX is pm_id*/
701
702                 media_info_s *_media = (media_info_s*)calloc(1, sizeof(media_info_s));
703
704                 if (_media == NULL) {
705                         media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
706                         SQLITE3_FINALIZE(stmt);
707                         return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
708                 }
709
710                 _media_info_item_get_detail(stmt, (media_info_h)_media);
711
712                 if (callback(playlist_member_id, (media_info_h)_media, user_data) == false) {
713                         media_info_destroy((media_info_h)_media);
714                         break;
715                 }
716                 media_info_destroy((media_info_h)_media);
717
718         }
719
720         SQLITE3_FINALIZE(stmt);
721
722         return ret;
723 }
724
725 int _media_db_get_tag(const char *media_id, filter_h filter, media_tag_cb callback, void *user_data)
726 {
727         int ret = MEDIA_CONTENT_ERROR_NONE;
728         char select_query[MAX_QUERY_SIZE] = {0, };
729         char *condition_query = NULL;
730         char *option_query = NULL;
731         sqlite3_stmt *stmt = NULL;
732         attribute_h attr = NULL;
733
734         attr = _content_get_attirbute_handle();
735         memset(select_query, 0x00, sizeof(select_query));
736
737         if (!STRING_VALID(media_id)) {
738                 if (!SAFE_STRLCPY(select_query, SELECT_TAG_LIST, sizeof(select_query))) {
739                         media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
740                         return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
741                 }
742         } else
743                 snprintf(select_query, sizeof(select_query), SELECT_TAG_LIST_BY_MEDIA_ID, media_id);
744
745         ret = __media_db_make_query(filter, attr, select_query, sizeof(select_query), &condition_query, &option_query);
746         media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
747
748         SAFE_STRLCAT(select_query, QUERY_KEYWORD_SPACE, sizeof(select_query));
749
750         ret = _content_query_prepare(&stmt, select_query, condition_query, option_query);
751         SAFE_FREE(condition_query);
752         SAFE_FREE(option_query);
753         media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
754
755         while (sqlite3_step(stmt) == SQLITE_ROW) {
756                 media_tag_s *_tag = (media_tag_s*)calloc(1, sizeof(media_tag_s));
757
758                 if (_tag == NULL) {
759                         media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
760                         SQLITE3_FINALIZE(stmt);
761                         return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
762                 }
763
764                 _tag->tag_id = (int)sqlite3_column_int(stmt, 0);
765                 _tag->name = g_strdup((const char *)sqlite3_column_text(stmt, 1));
766
767                 if (callback((media_tag_h)_tag, user_data) == false) {
768                         media_tag_destroy((media_tag_h)_tag);
769                         break;
770                 }
771                 media_tag_destroy((media_tag_h)_tag);
772         }
773
774         SQLITE3_FINALIZE(stmt);
775
776         return ret;
777 }
778
779 int _media_db_get_bookmark(const char *media_id, filter_h filter, media_bookmark_cb callback, void *user_data)
780 {
781         int ret = MEDIA_CONTENT_ERROR_NONE;
782         char select_query[MAX_QUERY_SIZE] = {0, };
783         char *condition_query = NULL;
784         char *option_query = NULL;
785         sqlite3_stmt *stmt = NULL;
786         attribute_h attr = NULL;
787         filter_s *_filter = (filter_s*)filter;
788
789         attr = _content_get_alias_attirbute_handle();
790
791         memset(select_query, 0x00, sizeof(select_query));
792
793         if (STRING_VALID(media_id)) {   //get bookmark by media_id
794                 if ((_filter != NULL) && STRING_VALID(_filter->storage_id))
795                         snprintf(select_query, sizeof(select_query), SELECT_BOOKMARK_LIST_BY_MEDIA_ID, _filter->storage_id, media_id);
796                 else
797                         snprintf(select_query, sizeof(select_query), SELECT_BOOKMARK_LIST_BY_MEDIA_ID, DB_VIEW_MEDIA, media_id);
798         } else {
799                 if ((_filter != NULL) && STRING_VALID(_filter->storage_id))
800                         snprintf(select_query, sizeof(select_query), SELECT_BOOKMARK_LIST, _filter->storage_id);
801                 else
802                         snprintf(select_query, sizeof(select_query), SELECT_BOOKMARK_LIST, DB_VIEW_MEDIA);
803         }
804
805         ret = __media_db_make_query(filter, attr, select_query, sizeof(select_query), &condition_query, &option_query);
806         media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
807
808         ret = _content_query_prepare(&stmt, select_query, condition_query, option_query);
809         SAFE_FREE(condition_query);
810         SAFE_FREE(option_query);
811         media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
812
813         while (sqlite3_step(stmt) == SQLITE_ROW) {
814                 media_bookmark_s *bookmark = (media_bookmark_s*)calloc(1, sizeof(media_bookmark_s));
815
816                 if (bookmark == NULL) {
817                         media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
818                         SQLITE3_FINALIZE(stmt);
819                         return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
820                 }
821
822                 bookmark->bookmark_id = (int)sqlite3_column_int(stmt, 0);
823                 bookmark->media_id = g_strdup((const char *)sqlite3_column_text(stmt, 1));
824                 bookmark->marked_time = (int)sqlite3_column_int(stmt, 2);
825                 bookmark->thumbnail_path = g_strdup((const char *)sqlite3_column_text(stmt, 3));
826                 bookmark->name = g_strdup((const char *)sqlite3_column_text(stmt, 4));
827
828                 if (callback((media_bookmark_h)bookmark, user_data) == false) {
829                         media_bookmark_destroy((media_bookmark_h)bookmark);
830                         break;
831                 }
832
833                 media_bookmark_destroy((media_bookmark_h)bookmark);
834         }
835
836         SQLITE3_FINALIZE(stmt);
837
838         return ret;
839
840 }
841
842 int _media_db_get_face(const char *media_id, filter_h filter, media_face_cb callback, void *user_data)
843 {
844         int ret = MEDIA_CONTENT_ERROR_NONE;
845         char select_query[MAX_QUERY_SIZE] = {0, };
846         char *condition_query = NULL;
847         char *option_query = NULL;
848         sqlite3_stmt *stmt = NULL;
849         attribute_h attr = NULL;
850         filter_s *_filter = (filter_s*)filter;
851
852         attr = _content_get_alias_attirbute_handle();
853
854         memset(select_query, 0x00, sizeof(select_query));
855
856         if (STRING_VALID(media_id)) {   //get face by media_id
857                 if ((_filter != NULL) && STRING_VALID(_filter->storage_id))
858                         snprintf(select_query, sizeof(select_query), SELECT_FACE_LIST_BY_MEDIA_ID, _filter->storage_id, media_id);
859                 else
860                         snprintf(select_query, sizeof(select_query), SELECT_FACE_LIST_BY_MEDIA_ID, DB_VIEW_MEDIA, media_id);
861         } else {
862                 if ((_filter != NULL) && STRING_VALID(_filter->storage_id))
863                         snprintf(select_query, sizeof(select_query), SELECT_FACE_LIST, _filter->storage_id);
864                 else
865                         snprintf(select_query, sizeof(select_query), SELECT_FACE_LIST, DB_VIEW_MEDIA);
866         }
867
868         ret = __media_db_make_query(filter, attr, select_query, sizeof(select_query), &condition_query, &option_query);
869         media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
870
871         ret = _content_query_prepare(&stmt, select_query, condition_query, option_query);
872         SAFE_FREE(condition_query);
873         SAFE_FREE(option_query);
874         media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
875
876         while (sqlite3_step(stmt) == SQLITE_ROW) {
877                 media_face_s *face = (media_face_s*)calloc(1, sizeof(media_face_s));
878
879                 if (face == NULL) {
880                         media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
881                         SQLITE3_FINALIZE(stmt);
882                         return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
883                 }
884
885                 face->face_id = (int)sqlite3_column_int(stmt, 0);
886                 face->media_id = g_strdup((const char *)sqlite3_column_text(stmt, 1));
887                 face->face_rect_x = (int)sqlite3_column_int(stmt, 2);
888                 face->face_rect_y = (int)sqlite3_column_int(stmt, 3);
889                 face->face_rect_w = (int)sqlite3_column_int(stmt, 4);
890                 face->face_rect_h = (int)sqlite3_column_int(stmt, 5);
891                 face->orientation = (int)sqlite3_column_int(stmt, 6);
892                 face->face_tag = g_strdup((const char *)sqlite3_column_text(stmt, 7));
893
894                 if (callback((media_face_h)face, user_data) == false) {
895                         media_face_destroy((media_face_h)face);
896                         break;
897                 }
898
899                 media_face_destroy((media_face_h)face);
900         }
901
902         SQLITE3_FINALIZE(stmt);
903
904         return ret;
905
906 }
907
908 int _media_db_get_group_item_count_by_id(int group_id, filter_h filter, group_list_e group_type, int *item_count)
909 {
910         int ret = MEDIA_CONTENT_ERROR_NONE;
911         sqlite3_stmt *stmt = NULL;
912         char select_query[MAX_QUERY_SIZE] = {0, };
913         char *condition_query = NULL;
914         char *option_query = NULL;
915         attribute_h attr = NULL;
916         filter_s *_filter = (filter_s*)filter;
917
918         attr = _content_get_attirbute_handle();
919         memset(select_query, 0x00, sizeof(select_query));
920
921         if (group_type == MEDIA_GROUP_ALBUM) {
922                 if ((_filter != NULL) && STRING_VALID(_filter->storage_id))
923                         snprintf(select_query, sizeof(select_query), SELECT_MEDIA_COUNT_FROM_ALBUM, _filter->storage_id, group_id);
924                 else
925                         snprintf(select_query, sizeof(select_query), SELECT_MEDIA_COUNT_FROM_ALBUM, DB_VIEW_MEDIA, group_id);
926         } else if (group_type == MEDIA_GROUP_PLAYLIST) {
927                 snprintf(select_query, sizeof(select_query), SELECT_MEDIA_COUNT_FROM_PLAYLIST, group_id);
928         } else if (group_type == MEDIA_GROUP_TAG) {
929                 snprintf(select_query, sizeof(select_query), SELECT_MEDIA_COUNT_FROM_TAG, group_id);
930         } else {
931                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
932                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
933         }
934
935         ret = __media_db_make_query(filter, attr, select_query, sizeof(select_query), &condition_query, &option_query);
936         media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
937
938         ret = _content_query_prepare(&stmt, select_query, condition_query, option_query);
939         SAFE_FREE(condition_query);
940         SAFE_FREE(option_query);
941         media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
942
943         if (sqlite3_step(stmt) == SQLITE_ROW)
944                 *item_count = (int)sqlite3_column_int(stmt, 0);
945
946         SQLITE3_FINALIZE(stmt);
947
948         return ret;
949 }
950
951 int _media_db_get_group_item_count(const char *group_name, filter_h filter, group_list_e group_type, int *item_count)
952 {
953         int ret = MEDIA_CONTENT_ERROR_NONE;
954         sqlite3_stmt *stmt = NULL;
955         char select_query[MAX_QUERY_SIZE] = {0, };
956         char *tmp_query = NULL;
957         char *condition_query = NULL;
958         char *option_query = NULL;
959         bool is_simple = FALSE;
960         attribute_h attr = NULL;
961         filter_s *_filter = (filter_s*)filter;
962
963         attr = _content_get_attirbute_handle();
964         memset(select_query, 0x00, sizeof(select_query));
965
966         if (group_type == MEDIA_GROUP_NONE) {
967                 /* There are 2 ways to get count for media table for performance
968                         If user wants to set offset and count, use SQL SELECT_MEDIA_COUNT_FROM_MEDIA.
969                         If user wants to get count without setting count, SELECT_MEDIA_COUNT_FROM_MEDIA_SIMPLE */
970                 if (_filter && ((_filter->offset < 0) && (_filter->count < 0))) {
971                                 if (STRING_VALID(_filter->storage_id))
972                                         snprintf(select_query, sizeof(select_query), SELECT_MEDIA_COUNT_FROM_MEDIA_SIMPLE, _filter->storage_id);
973                                 else
974                                         snprintf(select_query, sizeof(select_query), SELECT_MEDIA_COUNT_FROM_MEDIA_SIMPLE, DB_VIEW_MEDIA);
975                                 is_simple = TRUE;
976                 } else {
977                         snprintf(select_query, sizeof(select_query), SELECT_MEDIA_COUNT_FROM_MEDIA, DB_VIEW_MEDIA);
978                 }
979         } else if (group_type == MEDIA_GROUP_FOLDER) {
980                 if ((_filter != NULL) && STRING_VALID(_filter->storage_id))
981                         tmp_query = sqlite3_mprintf(SELECT_MEDIA_COUNT_FROM_FOLDER, _filter->storage_id, group_name);
982                 else
983                         tmp_query = sqlite3_mprintf(SELECT_MEDIA_COUNT_FROM_FOLDER, DB_VIEW_MEDIA, group_name);
984
985                 SAFE_STRLCAT(select_query, tmp_query, sizeof(select_query));
986         } else if (group_type == MEDIA_GROUP_TAG_BY_MEDIA_ID) {
987                 snprintf(select_query, sizeof(select_query), SELECT_TAG_COUNT_BY_MEDIA_ID, group_name);
988         } else if (group_type == MEDIA_GROUP_BOOKMARK_BY_MEDIA_ID) {
989                 attr = _content_get_alias_attirbute_handle();
990
991                 if ((_filter != NULL) && STRING_VALID(_filter->storage_id))
992                         tmp_query = sqlite3_mprintf(SELECT_BOOKMARK_COUNT_BY_MEDIA_ID, _filter->storage_id, group_name);
993                 else
994                         tmp_query = sqlite3_mprintf(SELECT_BOOKMARK_COUNT_BY_MEDIA_ID, DB_VIEW_MEDIA, group_name);
995
996                 SAFE_STRLCAT(select_query, tmp_query, sizeof(select_query));
997         } else if (group_type == MEDIA_GROUP_STORAGE) {
998                 tmp_query = sqlite3_mprintf(SELECT_MEDIA_COUNT_FROM_STORAGE, group_name, group_name);
999
1000                 SAFE_STRLCAT(select_query, tmp_query, sizeof(select_query));
1001         } else if (group_type == MEDIA_GROUP_FACE_BY_MEDIA_ID) {
1002                 attr = _content_get_alias_attirbute_handle();
1003
1004                 if ((_filter != NULL) && STRING_VALID(_filter->storage_id))
1005                         tmp_query = sqlite3_mprintf(SELECT_FACE_COUNT_BY_MEDIA_ID, _filter->storage_id, group_name);
1006                 else
1007                         tmp_query = sqlite3_mprintf(SELECT_FACE_COUNT_BY_MEDIA_ID, DB_VIEW_MEDIA, group_name);
1008
1009                 SAFE_STRLCAT(select_query, tmp_query, sizeof(select_query));
1010 #ifdef _USE_TVPD_MODE
1011         } else if (group_type == MEDIA_GROUP_PVR) {
1012                 if ((_filter != NULL) && STRING_VALID(_filter->storage_id))
1013                         snprintf(select_query, sizeof(select_query), SELECT_PVR_COUNT_BY_STORAGE_ID, _filter->storage_id);
1014                 else
1015                         SAFE_STRLCAT(select_query, SELECT_PVR_COUNT, sizeof(select_query));
1016         } else if (group_type == MEDIA_GROUP_UHD) {
1017                 if ((_filter != NULL) && STRING_VALID(_filter->storage_id))
1018                         snprintf(select_query, sizeof(select_query), SELECT_UHD_COUNT_BY_STORAGE_ID, _filter->storage_id);
1019                 else
1020                         SAFE_STRLCAT(select_query, SELECT_UHD_COUNT, sizeof(select_query));
1021 #endif
1022         } else {
1023                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1024                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1025         }
1026
1027         ret = __media_db_make_query(filter, attr, select_query, sizeof(select_query), &condition_query, &option_query);
1028         if (ret != MEDIA_CONTENT_ERROR_NONE) {
1029                 if (tmp_query != NULL)
1030                         SQLITE3_SAFE_FREE(tmp_query);
1031                 return ret;
1032         }
1033
1034         if (group_type == MEDIA_GROUP_NONE) {
1035                 SAFE_STRLCAT(select_query, QUERY_KEYWORD_SPACE, sizeof(select_query));
1036                 if (STRING_VALID(condition_query))
1037                         SAFE_STRLCAT(select_query, condition_query, sizeof(select_query));
1038                 if (STRING_VALID(option_query))
1039                         SAFE_STRLCAT(select_query, option_query, sizeof(select_query));
1040
1041                 if (!is_simple)
1042                         SAFE_STRLCAT(select_query, QUERY_KEYWORD_BRACKET, sizeof(select_query));
1043
1044                 SAFE_FREE(condition_query);
1045                 SAFE_FREE(option_query);
1046         }
1047
1048         ret = _content_query_prepare(&stmt, select_query, condition_query, option_query);
1049         if (tmp_query != NULL)
1050                 SQLITE3_SAFE_FREE(tmp_query);
1051         SAFE_FREE(condition_query);
1052         SAFE_FREE(option_query);
1053         media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
1054
1055         if (sqlite3_step(stmt) == SQLITE_ROW)
1056                 *item_count = (int)sqlite3_column_int(stmt, 0);
1057
1058         SQLITE3_FINALIZE(stmt);
1059
1060         return ret;
1061 }
1062
1063 int _media_db_get_group_item_by_id(int group_id, filter_h filter, media_info_cb callback, void *user_data, group_list_e group_type)
1064 {
1065         int ret = MEDIA_CONTENT_ERROR_NONE;
1066         char select_query[MAX_QUERY_SIZE] = {0, };
1067         char *condition_query = NULL;
1068         char *option_query = NULL;
1069         sqlite3_stmt *stmt = NULL;
1070         attribute_h attr = NULL;
1071         filter_s *_filter = (filter_s*)filter;
1072
1073         attr = _content_get_attirbute_handle();
1074         memset(select_query, 0x00, sizeof(select_query));
1075
1076         if (group_type == MEDIA_GROUP_ALBUM) {
1077                 if ((_filter != NULL) && STRING_VALID(_filter->storage_id))
1078                         snprintf(select_query, sizeof(select_query), SELECT_MEDIA_FROM_ALBUM, _filter->storage_id, group_id);
1079                 else
1080                         snprintf(select_query, sizeof(select_query), SELECT_MEDIA_FROM_ALBUM, DB_VIEW_MEDIA, group_id);
1081         } else if (group_type == MEDIA_GROUP_PLAYLIST) {
1082                 if ((_filter != NULL) && STRING_VALID(_filter->storage_id))
1083                         snprintf(select_query, sizeof(select_query), SELECT_MEDIA_FROM_PLAYLIST, _filter->storage_id, group_id);
1084                 else
1085                         snprintf(select_query, sizeof(select_query), SELECT_MEDIA_FROM_PLAYLIST, DB_VIEW_MEDIA, group_id);
1086         } else if (group_type == MEDIA_GROUP_TAG) {
1087                 if ((_filter != NULL) && STRING_VALID(_filter->storage_id))
1088                         snprintf(select_query, sizeof(select_query), SELECT_MEDIA_FROM_TAG, _filter->storage_id, group_id);
1089                 else
1090                         snprintf(select_query, sizeof(select_query), SELECT_MEDIA_FROM_TAG, DB_VIEW_MEDIA, group_id);
1091         } else {
1092                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1093                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1094         }
1095
1096         ret = __media_db_make_query(filter, attr, select_query, sizeof(select_query), &condition_query, &option_query);
1097         media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
1098
1099         ret = _content_query_prepare(&stmt, select_query, condition_query, option_query);
1100         SAFE_FREE(condition_query);
1101         SAFE_FREE(option_query);
1102         media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
1103
1104         while (sqlite3_step(stmt) == SQLITE_ROW) {
1105                 media_info_s *item = (media_info_s*)calloc(1, sizeof(media_info_s));
1106                 if (item == NULL) {
1107                         media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
1108                         SQLITE3_FINALIZE(stmt);
1109                         return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
1110                 }
1111
1112                 _media_info_item_get_detail(stmt, (media_info_h)item);
1113
1114                 if (callback((media_info_h)item, user_data) == false) {
1115                         media_info_destroy((media_info_h)item);
1116                         break;
1117                 }
1118
1119                 media_info_destroy((media_info_h)item);
1120         }
1121
1122         SQLITE3_FINALIZE(stmt);
1123
1124         return ret;
1125 }
1126
1127 int _media_db_get_group_item(const char *group_name, filter_h filter, media_info_cb callback, void *user_data, group_list_e group_type)
1128 {
1129         int ret = MEDIA_CONTENT_ERROR_NONE;
1130         char select_query[MAX_QUERY_SIZE] = {0, };
1131         char *condition_query = NULL;
1132         char *option_query = NULL;
1133         sqlite3_stmt *stmt = NULL;
1134         attribute_h attr = NULL;
1135         filter_s *_filter = (filter_s*)filter;
1136
1137         attr = _content_get_attirbute_handle();
1138         memset(select_query, 0x00, sizeof(select_query));
1139
1140         if (group_type == MEDIA_GROUP_NONE) {
1141                 if ((_filter != NULL) && STRING_VALID(_filter->storage_id))
1142                         snprintf(select_query, sizeof(select_query), SELECT_MEDIA_ITEM, _filter->storage_id);
1143                 else
1144                         snprintf(select_query, sizeof(select_query), SELECT_MEDIA_ITEM, DB_VIEW_MEDIA);
1145         } else if (group_type == MEDIA_GROUP_FOLDER) {
1146                 if ((_filter != NULL) && STRING_VALID(_filter->storage_id))
1147                         snprintf(select_query, sizeof(select_query), SELECT_MEDIA_FROM_FOLDER, _filter->storage_id, group_name);
1148                 else
1149                         snprintf(select_query, sizeof(select_query), SELECT_MEDIA_FROM_FOLDER, DB_VIEW_MEDIA, group_name);
1150         } else if (group_type == MEDIA_GROUP_STORAGE) {
1151                 snprintf(select_query, sizeof(select_query), SELECT_MEDIA_FROM_STORAGE, group_name, group_name);
1152         } else {
1153                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1154                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1155         }
1156
1157         ret = __media_db_make_query(filter, attr, select_query, sizeof(select_query), &condition_query, &option_query);
1158         media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
1159
1160         ret = _content_query_prepare(&stmt, select_query, condition_query, option_query);
1161         SAFE_FREE(condition_query);
1162         SAFE_FREE(option_query);
1163         media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
1164
1165         while (sqlite3_step(stmt) == SQLITE_ROW) {
1166                 media_info_s *item = (media_info_s*)calloc(1, sizeof(media_info_s));
1167                 if (item == NULL) {
1168                         media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
1169                         SQLITE3_FINALIZE(stmt);
1170                         return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
1171                 }
1172
1173                 _media_info_item_get_detail(stmt, (media_info_h)item);
1174
1175                 if (callback((media_info_h)item, user_data) == false) {
1176                         media_info_destroy((media_info_h)item);
1177                         break;
1178                 }
1179
1180                 media_info_destroy((media_info_h)item);
1181         }
1182
1183         SQLITE3_FINALIZE(stmt);
1184
1185         return ret;
1186 }
1187
1188 #ifdef _USE_SENIOR_MODE
1189 int _media_db_get_group_item_by_union_select(const char *group_name, filter_h filter1, filter_h filter2, media_info_cb callback, void *user_data)
1190 {
1191         int ret = MEDIA_CONTENT_ERROR_NONE;
1192         char select_query1[MAX_QUERY_SIZE] = {0, };
1193         char *condition_query1 = NULL;
1194         char *option_query1 = NULL;
1195         char select_query2[MAX_QUERY_SIZE] = {0, };
1196         char *condition_query2 = NULL;
1197         char *option_query2 = NULL;
1198         sqlite3_stmt *stmt = NULL;
1199         attribute_h attr = NULL;
1200         filter_s *_filter1 = (filter_s*)filter1;
1201         filter_s *_filter2 = (filter_s*)filter2;
1202
1203         attr = _content_get_attirbute_handle();
1204
1205         memset(select_query1, 0x00, sizeof(select_query1));
1206
1207         if ((_filter1 != NULL) && STRING_VALID(_filter1->storage_id))
1208                 snprintf(select_query1, sizeof(select_query1), SELECT_MEDIA_FROM_FOLDER, _filter1->storage_id, group_name);
1209         else
1210                 snprintf(select_query1, sizeof(select_query1), SELECT_MEDIA_FROM_FOLDER, DB_VIEW_MEDIA, group_name);
1211
1212         ret = __media_db_make_query(filter1, attr, select_query1, sizeof(select_query1), &condition_query1, &option_query1);
1213         if (ret != MEDIA_CONTENT_ERROR_NONE) {
1214                 SAFE_FREE(condition_query1);
1215                 SAFE_FREE(option_query1);
1216                 media_content_error("create select_query1 failed");
1217                 return ret;
1218         }
1219
1220         memset(select_query2, 0x00, sizeof(select_query2));
1221
1222         if ((_filter2 != NULL) && STRING_VALID(_filter2->storage_id))
1223                 snprintf(select_query2, sizeof(select_query2), SELECT_MEDIA_FROM_FOLDER, _filter2->storage_id, group_name);
1224         else
1225                 snprintf(select_query2, sizeof(select_query2), SELECT_MEDIA_FROM_FOLDER, DB_VIEW_MEDIA, group_name);
1226
1227         ret = __media_db_make_query(filter2, attr, select_query2, sizeof(select_query2), &condition_query2, &option_query2);
1228         if (ret != MEDIA_CONTENT_ERROR_NONE) {
1229                 SAFE_FREE(condition_query1);
1230                 SAFE_FREE(option_query1);
1231                 SAFE_FREE(condition_query2);
1232                 SAFE_FREE(option_query2);
1233                 media_content_error("create select_query2 failed");
1234                 return ret;
1235         }
1236
1237         ret = _content_query_prepare_by_union_select(&stmt, select_query1, condition_query1, option_query1, select_query2, condition_query2, option_query2);
1238         SAFE_FREE(condition_query1);
1239         SAFE_FREE(option_query1);
1240         SAFE_FREE(condition_query2);
1241         SAFE_FREE(option_query2);
1242         media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
1243
1244         while (sqlite3_step(stmt) == SQLITE_ROW) {
1245                 media_info_s *item = (media_info_s*)calloc(1, sizeof(media_info_s));
1246                 if (item == NULL) {
1247                         media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
1248                         SQLITE3_FINALIZE(stmt);
1249                         return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
1250                 }
1251
1252                 _media_info_item_get_detail(stmt, (media_info_h)item);
1253
1254                 if (callback((media_info_h)item, user_data) == false) {
1255                         media_info_destroy((media_info_h)item);
1256                         break;
1257                 }
1258
1259                 media_info_destroy((media_info_h)item);
1260         }
1261
1262         SQLITE3_FINALIZE(stmt);
1263
1264         return ret;
1265 }
1266 #endif
1267
1268 int _media_db_get_media_group_item_count(const char *group_name, filter_h filter, media_group_e group, int *item_count)
1269 {
1270         int ret = MEDIA_CONTENT_ERROR_NONE;
1271         sqlite3_stmt *stmt = NULL;
1272         char select_query[MAX_QUERY_SIZE] = {0, };
1273         char *tmp_query = NULL;
1274         char *condition_query = NULL;
1275         char *option_query = NULL;
1276         attribute_h attr = NULL;
1277         filter_s *_filter = (filter_s*)filter;
1278
1279         attr = _content_get_attirbute_handle();
1280         memset(select_query, 0x00, sizeof(select_query));
1281 #ifdef _USE_TVPD_MODE
1282         if (group < MEDIA_PVR_GROUP_DURATION) { /*media content */
1283                 if (group_name != NULL) {
1284                         if ((_filter != NULL) && STRING_VALID(_filter->storage_id))
1285                                 tmp_query = sqlite3_mprintf(SELECT_MEDIA_COUNT_FROM_GROUP, _filter->storage_id, __media_db_get_group_name(group), group_name);
1286                         else
1287                                 tmp_query = sqlite3_mprintf(SELECT_MEDIA_COUNT_FROM_GROUP, DB_VIEW_MEDIA, __media_db_get_group_name(group), group_name);
1288
1289                         SAFE_STRLCAT(select_query, tmp_query, sizeof(select_query));
1290                 } else {
1291                         if ((_filter != NULL) && STRING_VALID(_filter->storage_id))
1292                                 snprintf(select_query, sizeof(select_query), SELECT_MEDIA_COUNT_FROM_GROUP_NULL, _filter->storage_id, __media_db_get_group_name(group));
1293                         else
1294                                 snprintf(select_query, sizeof(select_query), SELECT_MEDIA_COUNT_FROM_GROUP_NULL, DB_VIEW_MEDIA, __media_db_get_group_name(group));
1295                 }
1296         } else if (group < MEDIA_UHD_GROUP_CONTENT_TITLE) {/*pvr content*/
1297                 if (group_name != NULL) {
1298                         if ((_filter != NULL) && STRING_VALID(_filter->storage_id))
1299                                 tmp_query = sqlite3_mprintf(SELECT_PVR_COUNT_FROM_GROUP_BY_STORAGE_ID, _filter->storage_id, __media_db_get_group_name(group), group_name);
1300                         else
1301                                 tmp_query = sqlite3_mprintf(SELECT_PVR_COUNT_FROM_GROUP, __media_db_get_group_name(group), group_name);
1302
1303                         SAFE_STRLCAT(select_query, tmp_query, sizeof(select_query));
1304                 } else {
1305                         if ((_filter != NULL) && STRING_VALID(_filter->storage_id))
1306                                 snprintf(select_query, sizeof(select_query), SELECT_PVR_COUNT_FROM_GROUP_NULL_BY_STORAGE_ID, _filter->storage_id, __media_db_get_group_name(group));
1307                         else
1308                                 snprintf(select_query, sizeof(select_query), SELECT_PVR_COUNT_FROM_GROUP_NULL, __media_db_get_group_name(group));
1309                 }
1310         } else {/*uhd content*/
1311                 if (group_name != NULL) {
1312                         if ((_filter != NULL) && STRING_VALID(_filter->storage_id))
1313                                 tmp_query = sqlite3_mprintf(SELECT_UHD_COUNT_FROM_GROUP_BY_STORAGE_ID, _filter->storage_id, __media_db_get_group_name(group), group_name);
1314                         else
1315                                 tmp_query = sqlite3_mprintf(SELECT_UHD_COUNT_FROM_GROUP, __media_db_get_group_name(group), group_name);
1316
1317                         SAFE_STRLCAT(select_query, tmp_query, sizeof(select_query));
1318                 } else {
1319                         if ((_filter != NULL) && STRING_VALID(_filter->storage_id))
1320                                 snprintf(select_query, sizeof(select_query), SELECT_UHD_COUNT_FROM_GROUP_NULL_BY_STORAGE_ID, _filter->storage_id, __media_db_get_group_name(group));
1321                         else
1322                                 snprintf(select_query, sizeof(select_query), SELECT_UHD_COUNT_FROM_GROUP_NULL, __media_db_get_group_name(group));
1323                 }
1324         }
1325 #else
1326         if (group_name != NULL) {
1327                 if ((_filter != NULL) && STRING_VALID(_filter->storage_id)) {
1328                         tmp_query = sqlite3_mprintf(SELECT_MEDIA_COUNT_FROM_GROUP, _filter->storage_id, __media_db_get_group_name(group), group_name);
1329                         SAFE_STRLCAT(select_query, tmp_query, sizeof(select_query));
1330                 } else {
1331                         tmp_query = sqlite3_mprintf(SELECT_MEDIA_COUNT_FROM_GROUP, DB_VIEW_MEDIA, __media_db_get_group_name(group), group_name);
1332                         SAFE_STRLCAT(select_query, tmp_query, sizeof(select_query));
1333                 }
1334         } else {
1335                 if ((_filter != NULL) && STRING_VALID(_filter->storage_id))
1336                         snprintf(select_query, sizeof(select_query), SELECT_MEDIA_COUNT_FROM_GROUP_NULL, _filter->storage_id, __media_db_get_group_name(group));
1337                 else
1338                         snprintf(select_query, sizeof(select_query), SELECT_MEDIA_COUNT_FROM_GROUP_NULL, DB_VIEW_MEDIA, __media_db_get_group_name(group));
1339         }
1340 #endif
1341         ret = __media_db_make_query(filter, attr, select_query, sizeof(select_query), &condition_query, &option_query);
1342         if (ret != MEDIA_CONTENT_ERROR_NONE) {
1343                 if (tmp_query != NULL)
1344                         SQLITE3_SAFE_FREE(tmp_query);
1345                 return ret;
1346         }
1347
1348         SAFE_STRLCAT(select_query, QUERY_KEYWORD_SPACE, sizeof(select_query));
1349         if (STRING_VALID(condition_query))
1350                 SAFE_STRLCAT(select_query, condition_query, sizeof(select_query));
1351         if (STRING_VALID(option_query))
1352                 SAFE_STRLCAT(select_query, option_query, sizeof(select_query));
1353         SAFE_STRLCAT(select_query, QUERY_KEYWORD_BRACKET, sizeof(select_query));
1354
1355         SAFE_FREE(condition_query);
1356         SAFE_FREE(option_query);
1357
1358         ret = _content_query_prepare(&stmt, select_query, condition_query, option_query);
1359         if (tmp_query != NULL)
1360                 SQLITE3_SAFE_FREE(tmp_query);
1361         SAFE_FREE(condition_query);
1362         SAFE_FREE(option_query);
1363         media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
1364
1365         if (sqlite3_step(stmt) == SQLITE_ROW)
1366                 *item_count = (int)sqlite3_column_int(stmt, 0);
1367
1368         SQLITE3_FINALIZE(stmt);
1369
1370         return ret;
1371 }
1372
1373 int _media_db_get_media_group_item(const char *group_name, filter_h filter, media_group_e group, media_info_cb callback, void *user_data)
1374 {
1375         int ret = MEDIA_CONTENT_ERROR_NONE;
1376         char select_query[MAX_QUERY_SIZE] = {0, };
1377         char *tmp_query = NULL;
1378         char *condition_query = NULL;
1379         char *option_query = NULL;
1380         sqlite3_stmt *stmt = NULL;
1381         attribute_h attr = NULL;
1382         filter_s *_filter = (filter_s*)filter;
1383
1384         attr = _content_get_attirbute_handle();
1385         memset(select_query, 0x00, sizeof(select_query));
1386
1387         if (group_name != NULL) {
1388                 if ((_filter != NULL) && STRING_VALID(_filter->storage_id))
1389                         tmp_query = sqlite3_mprintf(SELECT_MEDIA_FROM_GROUP, _filter->storage_id, __media_db_get_group_name(group), group_name);
1390                 else
1391                         tmp_query = sqlite3_mprintf(SELECT_MEDIA_FROM_GROUP, DB_VIEW_MEDIA, __media_db_get_group_name(group), group_name);
1392
1393                 SAFE_STRLCAT(select_query, tmp_query, sizeof(select_query));
1394         } else {
1395                 if ((_filter != NULL) && STRING_VALID(_filter->storage_id))
1396                         snprintf(select_query, sizeof(select_query), SELECT_MEDIA_FROM_GROUP_NULL, _filter->storage_id, __media_db_get_group_name(group));
1397                 else
1398                         snprintf(select_query, sizeof(select_query), SELECT_MEDIA_FROM_GROUP_NULL, DB_VIEW_MEDIA, __media_db_get_group_name(group));
1399         }
1400
1401         ret = __media_db_make_query(filter, attr, select_query, sizeof(select_query), &condition_query, &option_query);
1402         if (ret != MEDIA_CONTENT_ERROR_NONE) {
1403                 if (tmp_query != NULL)
1404                         SQLITE3_SAFE_FREE(tmp_query);
1405                 return ret;
1406         }
1407
1408         ret = _content_query_prepare(&stmt, select_query, condition_query, option_query);
1409         if (tmp_query != NULL)
1410                 SQLITE3_SAFE_FREE(tmp_query);
1411         SAFE_FREE(condition_query);
1412         SAFE_FREE(option_query);
1413         media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
1414
1415         while (sqlite3_step(stmt) == SQLITE_ROW) {
1416                 media_info_s *item = (media_info_s*)calloc(1, sizeof(media_info_s));
1417                 if (item == NULL) {
1418                         media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
1419                         SQLITE3_FINALIZE(stmt);
1420                         return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
1421                 }
1422
1423                 _media_info_item_get_detail(stmt, (media_info_h)item);
1424
1425                 if (callback((media_info_h)item, user_data) == false) {
1426                         media_info_destroy((media_info_h)item);
1427                         break;
1428                 }
1429
1430                 media_info_destroy((media_info_h)item);
1431         }
1432
1433         SQLITE3_FINALIZE(stmt);
1434
1435         return ret;
1436 }
1437
1438 int _media_db_get_storage(filter_h filter, media_storage_cb callback, void *user_data)
1439 {
1440         int ret = MEDIA_CONTENT_ERROR_NONE;
1441         char select_query[DEFAULT_QUERY_SIZE] = {0, };
1442         char *condition_query = NULL;
1443         char *option_query = NULL;
1444         sqlite3_stmt *stmt = NULL;
1445         attribute_h attr = NULL;
1446
1447         attr = _content_get_attirbute_handle();
1448         memset(select_query, 0x00, sizeof(select_query));
1449
1450         if (!SAFE_STRLCPY(select_query, SELECT_STORAGE_LIST, sizeof(select_query))) {
1451                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1452                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1453         }
1454
1455         ret = __media_db_make_query(filter, attr, select_query, sizeof(select_query), &condition_query, &option_query);
1456         media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
1457
1458         ret = _content_query_prepare(&stmt, select_query, condition_query, option_query);
1459         SAFE_FREE(condition_query);
1460         SAFE_FREE(option_query);
1461         media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
1462
1463         while (sqlite3_step(stmt) == SQLITE_ROW) {
1464                 media_storage_s *_storage = (media_storage_s*)calloc(1, sizeof(media_storage_s));
1465
1466                 if (_storage == NULL) {
1467                         media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
1468                         SQLITE3_FINALIZE(stmt);
1469                         return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
1470                 }
1471
1472                 _storage->storage_id = g_strdup((const char *)sqlite3_column_text(stmt, 0));
1473                 _storage->storage_path = g_strdup((const char *)sqlite3_column_text(stmt, 1));
1474                 _storage->storage_type = (int)sqlite3_column_int(stmt, 2);
1475
1476                 if (callback((media_storage_h)_storage, user_data) == false) {
1477                         media_storage_destroy((media_storage_h) _storage);
1478                         break;
1479                 }
1480
1481                 media_storage_destroy((media_storage_h) _storage);
1482         }
1483
1484         SQLITE3_FINALIZE(stmt);
1485
1486         return ret;
1487 }
1488 #ifdef _USE_TVPD_MODE
1489 int _media_db_get_pvr_group_item(const char *group_name, filter_h filter, media_group_e group, media_pvr_cb callback, void *user_data)
1490 {
1491         int ret = MEDIA_CONTENT_ERROR_NONE;
1492         char select_query[DEFAULT_QUERY_SIZE] = {0, };
1493         char *tmp_query = NULL;
1494         char *condition_query = NULL;
1495         char *option_query = NULL;
1496         sqlite3_stmt *stmt = NULL;
1497         attribute_h attr = NULL;
1498         filter_s *_filter = (filter_s*)filter;
1499
1500         attr = _content_get_attirbute_handle();
1501         memset(select_query, 0x00, sizeof(select_query));
1502
1503         if (group_name != NULL) {
1504                 if ((_filter != NULL) && STRING_VALID(_filter->storage_id))
1505                         tmp_query = sqlite3_mprintf(SELECT_PVR_FROM_GROUP_BY_STORAGE_ID, _filter->storage_id, __media_db_get_group_name(group), group_name);
1506                 else
1507                         tmp_query = sqlite3_mprintf(SELECT_PVR_FROM_GROUP, __media_db_get_group_name(group), group_name);
1508
1509                 SAFE_STRLCAT(select_query, tmp_query, sizeof(select_query));
1510         } else {
1511                 if ((_filter != NULL) && STRING_VALID(_filter->storage_id))
1512                         snprintf(select_query, sizeof(select_query), SELECT_PVR_FROM_GROUP_NULL_BY_STORAGE_ID, _filter->storage_id, __media_db_get_group_name(group));
1513                 else
1514                         snprintf(select_query, sizeof(select_query), SELECT_PVR_FROM_GROUP_NULL, __media_db_get_group_name(group));
1515         }
1516
1517         ret = __media_db_make_query(filter, attr, select_query, sizeof(select_query), &condition_query, &option_query);
1518         if (ret != MEDIA_CONTENT_ERROR_NONE) {
1519                 if (tmp_query != NULL)
1520                         sqlite3_free(tmp_query);
1521                 return ret;
1522         }
1523
1524         ret = _content_query_prepare(&stmt, select_query, condition_query, option_query);
1525         if (tmp_query != NULL)
1526                 sqlite3_free(tmp_query);
1527         SAFE_FREE(condition_query);
1528         SAFE_FREE(option_query);
1529         media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
1530
1531         while (sqlite3_step(stmt) == SQLITE_ROW) {
1532                 media_pvr_s *item = (media_pvr_s*)calloc(1, sizeof(media_pvr_s));
1533                 if (item == NULL) {
1534                         media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
1535                         SQLITE3_FINALIZE(stmt);
1536                         return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
1537                 }
1538
1539                 _media_pvr_item_get_detail(stmt, (media_pvr_h)item);
1540
1541                 if (callback((media_pvr_h)item, user_data) == false) {
1542                         media_info_destroy((media_pvr_h)item);
1543                         break;
1544                 }
1545
1546                 media_pvr_destroy((media_pvr_h)item);
1547         }
1548
1549         SQLITE3_FINALIZE(stmt);
1550
1551         return ret;
1552 }
1553
1554 typedef enum {
1555         MEDIA_PVR_MEDIA_ID = 0,
1556         MEDIA_PVR_STORAGE_ID,
1557         MEDIA_PVR_PATH,
1558         MEDIA_PVR_SIZE,
1559         MEDIA_PVR_DURATION,
1560         MEDIA_PVR_TIME_ZONE,
1561         MEDIA_PVR_PTC,
1562         MEDIA_PVR_MAJOR,
1563         MEDIA_PVR_MINOR,
1564         MEDIA_PVR_CHANNEL_TYPE,
1565         MEDIA_PVR_CHANNEL_NAME,
1566         MEDIA_PVR_CHANNEL_NUM,
1567         MEDIA_PVR_PROGRAM_TITLE,
1568         MEDIA_PVR_PROGRAM_NUM,
1569         MEDIA_PVR_PROGRAM_CRID,
1570         MEDIA_PVR_GUIDANCE,
1571         MEDIA_PVR_SYNOPSIS,
1572         MEDIA_PVR_GENRE,
1573         MEDIA_PVR_LANGUAGE,
1574         MEDIA_PVR_EMBARGO_TIME,
1575         MEDIA_PVR_EXPIRY_TIME,
1576         MEDIA_PVR_START_TIME,
1577         MEDIA_PVR_PROGRAM_START_TIME,
1578         MEDIA_PVR_PROGRAM_END_TIME,
1579         MEDIA_PVR_PROGRAM_DATE,
1580         MEDIA_PVR_PARENTAL_RATING,
1581         MEDIA_PVR_TIMER_RECORD,
1582         MEDIA_PVR_SERIES_RECORD,
1583         MEDIA_PVR_HD,
1584         MEDIA_PVR_SUBTITLE,
1585         MEDIA_PVR_TTX,
1586         MEDIA_PVR_AD,
1587         MEDIA_PVR_HARDOF_HEARINGRADIO,
1588         MEDIA_PVR_DATA_SERVICE,
1589         MEDIA_PVR_CONTENT_LOCK,
1590         MEDIA_PVR_CONTENT_WATCH,
1591         MEDIA_PVR_HAS_AUDIO_ONLY,
1592         MEDIA_PVR_IS_LOCAL_RECORDED,
1593         MEDIA_PVR_RESOLUTION,
1594         MEDIA_PVR_ASPECTRATIO,
1595         MEDIA_PVR_MODIFIED_MONTH,
1596         MEDIA_PVR_SPORTS_TYPE,
1597         MEDIA_PVR_GUIDANCE_LENGTH,
1598         MEDIA_PVR_TVMODE,
1599         MEDIA_PVR_PLAY_COUNT,
1600         MEDIA_PVR_PRIVATE_DATA,
1601         MEDIA_PVR_HIGHLIGHT,
1602 } media_pvr_field_e;
1603
1604 void _media_pvr_item_get_detail(sqlite3_stmt* stmt, media_pvr_h pvr)
1605 {
1606         media_pvr_s *_pvr = (media_pvr_s*)pvr;
1607
1608         if (STRING_VALID((const char *)sqlite3_column_text(stmt, MEDIA_PVR_MEDIA_ID)))
1609                 _pvr->media_id = strdup((const char *)sqlite3_column_text(stmt, MEDIA_PVR_MEDIA_ID));
1610
1611         if (STRING_VALID((const char *)sqlite3_column_text(stmt, MEDIA_PVR_STORAGE_ID)))
1612                 _pvr->storage_id = strdup((const char *)sqlite3_column_text(stmt, MEDIA_PVR_STORAGE_ID));
1613
1614         if (STRING_VALID((const char *)sqlite3_column_text(stmt, MEDIA_PVR_PATH)))
1615                 _pvr->path = strdup((const char *)sqlite3_column_text(stmt, MEDIA_PVR_PATH));
1616
1617         _pvr->size = (unsigned long long)sqlite3_column_int64(stmt, MEDIA_PVR_SIZE);
1618
1619         _pvr->duration = (int)sqlite3_column_int(stmt, MEDIA_PVR_DURATION);
1620
1621         _pvr->timezone = (int)sqlite3_column_int(stmt, MEDIA_PVR_TIME_ZONE);
1622
1623         _pvr->ptc = (int)sqlite3_column_int(stmt, MEDIA_PVR_PTC);
1624
1625         _pvr->major = (int)sqlite3_column_int(stmt, MEDIA_PVR_MAJOR);
1626
1627         _pvr->minor = (int)sqlite3_column_int(stmt, MEDIA_PVR_MINOR);
1628
1629         _pvr->channel_type = (int)sqlite3_column_int(stmt, MEDIA_PVR_CHANNEL_TYPE);
1630
1631         if (STRING_VALID((const char *)sqlite3_column_text(stmt, MEDIA_PVR_CHANNEL_NAME)))
1632                 _pvr->channel_name = strdup((const char *)sqlite3_column_text(stmt, MEDIA_PVR_CHANNEL_NAME));
1633
1634         if (STRING_VALID((const char *)sqlite3_column_text(stmt, MEDIA_PVR_CHANNEL_NUM)))
1635                 _pvr->channel_num = strdup((const char *)sqlite3_column_text(stmt, MEDIA_PVR_CHANNEL_NUM));
1636
1637         if (STRING_VALID((const char *)sqlite3_column_text(stmt, MEDIA_PVR_PROGRAM_TITLE)))
1638                 _pvr->program_title = strdup((const char *)sqlite3_column_text(stmt, MEDIA_PVR_PROGRAM_TITLE));
1639
1640         _pvr->program_num = (int)sqlite3_column_int(stmt, MEDIA_PVR_PROGRAM_NUM);
1641
1642         if (STRING_VALID((const char *)sqlite3_column_text(stmt, MEDIA_PVR_PROGRAM_CRID)))
1643                 _pvr->program_crid = strdup((const char *)sqlite3_column_text(stmt, MEDIA_PVR_PROGRAM_CRID));
1644
1645         if (STRING_VALID((const char *)sqlite3_column_text(stmt, MEDIA_PVR_GUIDANCE)))
1646                 _pvr->guidance = strdup((const char *)sqlite3_column_text(stmt, MEDIA_PVR_GUIDANCE));
1647
1648         if (STRING_VALID((const char *)sqlite3_column_text(stmt, MEDIA_PVR_SYNOPSIS)))
1649                 _pvr->synopsis = strdup((const char *)sqlite3_column_text(stmt, MEDIA_PVR_SYNOPSIS));
1650
1651         if (STRING_VALID((const char *)sqlite3_column_text(stmt, MEDIA_PVR_GENRE)))
1652                 _pvr->genre = strdup((const char *)sqlite3_column_text(stmt, MEDIA_PVR_GENRE));
1653
1654         if (STRING_VALID((const char *)sqlite3_column_text(stmt, MEDIA_PVR_LANGUAGE)))
1655                 _pvr->language = strdup((const char *)sqlite3_column_text(stmt, MEDIA_PVR_LANGUAGE));
1656
1657         if (STRING_VALID((const char *)sqlite3_column_text(stmt, MEDIA_PVR_MODIFIED_MONTH)))
1658                 _pvr->modified_month = strdup((const char *)sqlite3_column_text(stmt, MEDIA_PVR_MODIFIED_MONTH));
1659
1660         if (STRING_VALID((const char *)sqlite3_column_text(stmt, MEDIA_PVR_PRIVATE_DATA)))
1661                 _pvr->private_data = strdup((const char *)sqlite3_column_text(stmt, MEDIA_PVR_PRIVATE_DATA));
1662
1663         _pvr->embargo_time = (int)sqlite3_column_int(stmt, MEDIA_PVR_EMBARGO_TIME);
1664
1665         _pvr->expiry_time = (int)sqlite3_column_int(stmt, MEDIA_PVR_EXPIRY_TIME);
1666
1667         _pvr->start_time = (int)sqlite3_column_int(stmt, MEDIA_PVR_START_TIME);
1668
1669         _pvr->program_start_time = (int)sqlite3_column_int(stmt, MEDIA_PVR_PROGRAM_START_TIME);
1670
1671         _pvr->program_end_time = (int)sqlite3_column_int(stmt, MEDIA_PVR_PROGRAM_END_TIME);
1672
1673         _pvr->program_date = (int)sqlite3_column_int(stmt, MEDIA_PVR_PROGRAM_DATE);
1674
1675         _pvr->parental_rating = (int)sqlite3_column_int(stmt, MEDIA_PVR_PARENTAL_RATING);
1676
1677         _pvr->timer_record = (int)sqlite3_column_int(stmt, MEDIA_PVR_TIMER_RECORD);
1678
1679         _pvr->series_record = (int)sqlite3_column_int(stmt, MEDIA_PVR_SERIES_RECORD);
1680
1681         _pvr->hd = (int)sqlite3_column_int(stmt, MEDIA_PVR_HD);
1682
1683         _pvr->subtitle = (int)sqlite3_column_int(stmt, MEDIA_PVR_SUBTITLE);
1684
1685         _pvr->ttx = (int)sqlite3_column_int(stmt, MEDIA_PVR_TTX);
1686
1687         _pvr->ad = (int)sqlite3_column_int(stmt, MEDIA_PVR_AD);
1688
1689         _pvr->hard_of_hearing_radio = (int)sqlite3_column_int(stmt, MEDIA_PVR_HARDOF_HEARINGRADIO);
1690
1691         _pvr->data_service = (int)sqlite3_column_int(stmt, MEDIA_PVR_DATA_SERVICE);
1692
1693         _pvr->content_lock = (int)sqlite3_column_int(stmt, MEDIA_PVR_CONTENT_LOCK);
1694
1695         _pvr->content_watch = (int)sqlite3_column_int(stmt, MEDIA_PVR_CONTENT_WATCH);
1696
1697         _pvr->has_audio_only = (int)sqlite3_column_int(stmt, MEDIA_PVR_HAS_AUDIO_ONLY);
1698
1699         _pvr->is_local_record = (int)sqlite3_column_int(stmt, MEDIA_PVR_IS_LOCAL_RECORDED);
1700
1701         _pvr->resolution = (int)sqlite3_column_int(stmt, MEDIA_PVR_RESOLUTION);
1702
1703         _pvr->aspectratio = (int)sqlite3_column_int(stmt, MEDIA_PVR_ASPECTRATIO);
1704
1705         _pvr->sports_type = (int)sqlite3_column_int(stmt, MEDIA_PVR_SPORTS_TYPE);
1706
1707         _pvr->guidance_length = (int)sqlite3_column_int(stmt, MEDIA_PVR_GUIDANCE_LENGTH);
1708
1709         _pvr->tvmode = (int)sqlite3_column_int(stmt, MEDIA_PVR_TVMODE);
1710
1711         _pvr->play_count = (int)sqlite3_column_int(stmt, MEDIA_PVR_PLAY_COUNT);
1712
1713         _pvr->highlight = (int)sqlite3_column_int(stmt, MEDIA_PVR_HIGHLIGHT);
1714
1715         return;
1716 }
1717
1718 int _media_db_get_pvr(filter_h filter, media_pvr_cb callback, void *user_data)
1719 {
1720         int ret = MEDIA_CONTENT_ERROR_NONE;
1721         char select_query[DEFAULT_QUERY_SIZE] = {0, };
1722         char *condition_query = NULL;
1723         char *option_query = NULL;
1724         sqlite3_stmt *stmt = NULL;
1725         attribute_h attr = NULL;
1726         filter_s *_filter = (filter_s*)filter;
1727
1728         attr = _content_get_attirbute_handle();
1729         memset(select_query, 0x00, sizeof(select_query));
1730
1731         if ((_filter != NULL) && STRING_VALID(_filter->storage_id))
1732                 snprintf(select_query, sizeof(select_query), SELECT_PVR_LIST_BY_STORAGE_ID, _filter->storage_id);
1733         else
1734                 SAFE_STRLCAT(select_query, SELECT_PVR_LIST, sizeof(select_query));
1735
1736         ret = __media_db_make_query(filter, attr, select_query, sizeof(select_query), &condition_query, &option_query);
1737         media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
1738
1739         ret = _content_query_prepare(&stmt, select_query, condition_query, option_query);
1740         SAFE_FREE(condition_query);
1741         SAFE_FREE(option_query);
1742         media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
1743
1744         while (sqlite3_step(stmt) == SQLITE_ROW) {
1745                 media_pvr_s *_pvr = (media_pvr_s*)calloc(1, sizeof(media_pvr_s));
1746
1747                 if (_pvr == NULL) {
1748                         media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
1749                         SQLITE3_FINALIZE(stmt);
1750                         return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
1751                 }
1752
1753                 _media_pvr_item_get_detail(stmt, (media_pvr_h)_pvr);
1754
1755                 if (callback((media_pvr_h)_pvr, user_data) == false) {
1756                         media_pvr_destroy((media_pvr_h) _pvr);
1757                         break;
1758                 }
1759
1760                 media_pvr_destroy((media_pvr_h) _pvr);
1761         }
1762
1763         SQLITE3_FINALIZE(stmt);
1764
1765         return ret;
1766 }
1767 #endif
1768
1769 int _media_db_get_storage_id_by_media_id(const char *media_id, char *storage_id)
1770 {
1771         int ret = MEDIA_CONTENT_ERROR_NONE;
1772         sqlite3_stmt *stmt = NULL;
1773         char *select_query = NULL;
1774
1775         select_query = sqlite3_mprintf(SELECT_MEDIA_STORAGE_ID_BY_ID, media_id);
1776
1777         ret = _content_query_prepare(&stmt, select_query, NULL, NULL);
1778         SQLITE3_SAFE_FREE(select_query);
1779         media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
1780
1781         if (sqlite3_step(stmt) == SQLITE_ROW) {
1782                 if (STRING_VALID((const char *)sqlite3_column_text(stmt, 0))) {
1783                         if (!SAFE_STRLCPY(storage_id, (const char *)sqlite3_column_text(stmt, 0), MEDIA_CONTENT_UUID_SIZE+1)) {
1784                                 media_content_error("Storage id copy fail");
1785                                 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
1786                         }
1787                 }
1788         } else {
1789                 media_content_error("There's no media!!");
1790                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1791         }
1792
1793         SQLITE3_FINALIZE(stmt);
1794
1795         return ret;
1796 }
1797
1798 #ifdef _USE_TVPD_MODE
1799 typedef enum {
1800         MEDIA_UHD_MEDIA_ID = 0,
1801         MEDIA_UHD_STORAGE_ID,
1802         MEDIA_UHD_PATH,
1803         MEDIA_UHD_SIZE,
1804         MEDIA_UHD_CONTENT_ID,
1805         MEDIA_UHD_CONTENT_TITLE,
1806         MEDIA_UHD_FILE_NAME,
1807         MEDIA_UHD_FOLDER_ID,
1808         MEDIA_UHD_RELEASE_DATE,
1809         MEDIA_UHD_MODIFIED_TIME,
1810         MEDIA_UHD_PLAYED_POSITION,
1811         MEDIA_UHD_SUB_TYPE,
1812         MEDIA_UHD_PLAYED_COUNT,
1813 } media_uhd_field_e;
1814
1815 void _media_uhd_item_get_detail(sqlite3_stmt* stmt, media_uhd_h uhd)
1816 {
1817         media_uhd_s *_uhd = (media_uhd_s*)uhd;
1818
1819         if (STRING_VALID((const char *)sqlite3_column_text(stmt, MEDIA_UHD_MEDIA_ID)))
1820                 _uhd->media_id = strdup((const char *)sqlite3_column_text(stmt, MEDIA_UHD_MEDIA_ID));
1821
1822         if (STRING_VALID((const char *)sqlite3_column_text(stmt, MEDIA_UHD_STORAGE_ID)))
1823                 _uhd->storage_id = strdup((const char *)sqlite3_column_text(stmt, MEDIA_UHD_STORAGE_ID));
1824
1825         if (STRING_VALID((const char *)sqlite3_column_text(stmt, MEDIA_UHD_PATH)))
1826                 _uhd->path = strdup((const char *)sqlite3_column_text(stmt, MEDIA_UHD_PATH));
1827
1828         _uhd->size = (unsigned long long)sqlite3_column_int64(stmt, MEDIA_UHD_SIZE);
1829
1830         if (STRING_VALID((const char *)sqlite3_column_text(stmt, MEDIA_UHD_CONTENT_ID)))
1831                 _uhd->content_id = strdup((const char *)sqlite3_column_text(stmt, MEDIA_UHD_CONTENT_ID));
1832
1833         if (STRING_VALID((const char *)sqlite3_column_text(stmt, MEDIA_UHD_CONTENT_TITLE)))
1834                 _uhd->content_title = strdup((const char *)sqlite3_column_text(stmt, MEDIA_UHD_CONTENT_TITLE));
1835
1836         if (STRING_VALID((const char *)sqlite3_column_text(stmt, MEDIA_UHD_FILE_NAME)))
1837                 _uhd->file_name = strdup((const char *)sqlite3_column_text(stmt, MEDIA_UHD_FILE_NAME));
1838
1839         if (STRING_VALID((const char *)sqlite3_column_text(stmt, MEDIA_UHD_RELEASE_DATE)))
1840                 _uhd->release_date = strdup((const char *)sqlite3_column_text(stmt, MEDIA_UHD_RELEASE_DATE));
1841
1842         _uhd->modified_time = (int)sqlite3_column_int(stmt, MEDIA_UHD_MODIFIED_TIME);
1843         _uhd->played_position = (int)sqlite3_column_int(stmt, MEDIA_UHD_PLAYED_POSITION);
1844         _uhd->sub_type = (int)sqlite3_column_int(stmt, MEDIA_UHD_SUB_TYPE);
1845         _uhd->played_count = (int)sqlite3_column_int(stmt, MEDIA_UHD_PLAYED_COUNT);
1846
1847         return;
1848 }
1849
1850 int _media_db_get_uhd(filter_h filter, media_uhd_cb callback, void *user_data)
1851 {
1852         int ret = MEDIA_CONTENT_ERROR_NONE;
1853         char select_query[DEFAULT_QUERY_SIZE] = {0, };
1854         char *condition_query = NULL;
1855         char *option_query = NULL;
1856         sqlite3_stmt *stmt = NULL;
1857         attribute_h attr = NULL;
1858         filter_s *_filter = (filter_s*)filter;
1859
1860         attr = _content_get_attirbute_handle();
1861         memset(select_query, 0x00, sizeof(select_query));
1862
1863         if ((_filter != NULL) && STRING_VALID(_filter->storage_id))
1864                 snprintf(select_query, sizeof(select_query), SELECT_UHD_LIST_BY_STORAGE_ID, _filter->storage_id);
1865         else
1866                 SAFE_STRLCAT(select_query, SELECT_UHD_LIST, sizeof(select_query));
1867
1868         ret = __media_db_make_query(filter, attr, select_query, sizeof(select_query), &condition_query, &option_query);
1869         media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
1870
1871         ret = _content_query_prepare(&stmt, select_query, condition_query, option_query);
1872         SAFE_FREE(condition_query);
1873         SAFE_FREE(option_query);
1874         media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
1875
1876         while (sqlite3_step(stmt) == SQLITE_ROW) {
1877                 media_uhd_s *_uhd = (media_uhd_s*)calloc(1, sizeof(media_uhd_s));
1878
1879                 if (_uhd == NULL) {
1880                         media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
1881                         SQLITE3_FINALIZE(stmt);
1882                         return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
1883                 }
1884
1885                 _media_uhd_item_get_detail(stmt, (media_uhd_h)_uhd);
1886
1887                 if (callback((media_uhd_h)_uhd, user_data) == false) {
1888                         media_uhd_destroy((media_uhd_h) _uhd);
1889                         break;
1890                 }
1891                 media_uhd_destroy((media_uhd_h) _uhd);
1892         }
1893
1894         SQLITE3_FINALIZE(stmt);
1895
1896         return ret;
1897 }
1898 #endif