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