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