4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Hyunjun Ko <zzoon.ko@samsung.com>, Haejeong Kim <backto.kim@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
23 #include "media-svc-media.h"
24 #include "media-svc-media-folder.h"
25 #include "media-svc-error.h"
26 #include "media-svc-debug.h"
27 #include "media-svc-util.h"
28 #include "media-svc-db-utils.h"
29 #include "media-svc-noti.h"
32 char thumbnail_path[MEDIA_SVC_PATHNAME_SIZE];
33 }media_svc_thumbnailpath_s;
35 static __thread GList *g_media_svc_item_validity_query_list = NULL;
36 static __thread GList *g_media_svc_insert_item_query_list = NULL;
37 __thread GList *g_media_svc_move_item_query_list = NULL;
39 static int __media_svc_count_invalid_records_with_thumbnail(sqlite3 *handle, media_svc_storage_type_e storage_type, int *count);
40 static int __media_svc_get_invalid_records_with_thumbnail(sqlite3 *handle, media_svc_storage_type_e storage_type,
41 int count, media_svc_thumbnailpath_s * thumb_path);
42 static int __media_svc_count_invalid_folder_records_with_thumbnail(sqlite3 *handle, const char *folder_path, int *count);
43 static int __media_svc_get_invalid_folder_records_with_thumbnail(sqlite3 *handle, const char *folder_path,
44 int count, media_svc_thumbnailpath_s * thumb_path);
46 static int __media_svc_count_invalid_records_with_thumbnail(sqlite3 *handle, media_svc_storage_type_e storage_type, int *count)
48 int ret = MEDIA_INFO_ERROR_NONE;
49 sqlite3_stmt *sql_stmt = NULL;
50 char *sql = sqlite3_mprintf("SELECT count(*) FROM %s WHERE validity=0 AND storage_type=%d AND thumbnail_path IS NOT NULL",
51 MEDIA_SVC_DB_TABLE_MEDIA, storage_type);
53 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
55 if (ret != MEDIA_INFO_ERROR_NONE) {
56 media_svc_error("error when __media_svc_count_invalid_records_with_thumbnail. err = [%d]", ret);
60 *count = sqlite3_column_int(sql_stmt, 0);
62 SQLITE3_FINALIZE(sql_stmt);
64 return MEDIA_INFO_ERROR_NONE;
68 static int __media_svc_get_invalid_records_with_thumbnail(sqlite3 *handle, media_svc_storage_type_e storage_type,
69 int count, media_svc_thumbnailpath_s * thumb_path)
73 sqlite3_stmt *sql_stmt = NULL;
75 char *sql = sqlite3_mprintf("SELECT thumbnail_path from (select thumbnail_path, validity from %s WHERE storage_type=%d AND thumbnail_path IS NOT NULL GROUP BY thumbnail_path HAVING count() = 1) WHERE validity=0",
76 MEDIA_SVC_DB_TABLE_MEDIA, storage_type);
78 media_svc_debug("[SQL query] : %s", sql);
80 err = sqlite3_prepare_v2(handle, sql, -1, &sql_stmt, NULL);
82 if (err != SQLITE_OK) {
83 media_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
84 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
87 while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
88 _strncpy_safe(thumb_path[idx].thumbnail_path, (const char *)sqlite3_column_text(sql_stmt, 0), sizeof(thumb_path[idx]));
89 //media_svc_debug("thumb_path[%d]=[%s]", idx, thumb_path[idx].thumbnail_path);
93 SQLITE3_FINALIZE(sql_stmt);
95 return MEDIA_INFO_ERROR_NONE;
98 static int __media_svc_count_invalid_folder_records_with_thumbnail(sqlite3 *handle, const char *folder_path, int *count)
100 int ret = MEDIA_INFO_ERROR_NONE;
101 sqlite3_stmt *sql_stmt = NULL;
102 char *sql = sqlite3_mprintf("SELECT count(*) FROM %s WHERE validity=0 AND path LIKE '%q/%%' AND thumbnail_path IS NOT NULL",
103 MEDIA_SVC_DB_TABLE_MEDIA, folder_path);
105 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
107 if (ret != MEDIA_INFO_ERROR_NONE) {
108 media_svc_error("error when __media_svc_count_invalid_folder_records_with_thumbnail. err = [%d]", ret);
112 *count = sqlite3_column_int(sql_stmt, 0);
114 SQLITE3_FINALIZE(sql_stmt);
116 return MEDIA_INFO_ERROR_NONE;
120 static int __media_svc_get_invalid_folder_records_with_thumbnail(sqlite3 *handle, const char *folder_path,
121 int count, media_svc_thumbnailpath_s * thumb_path)
125 sqlite3_stmt *sql_stmt = NULL;
127 char *sql = sqlite3_mprintf("SELECT thumbnail_path from (select thumbnail_path, validity from %s WHERE path LIKE '%q/%%' AND thumbnail_path IS NOT NULL GROUP BY thumbnail_path HAVING count() = 1) WHERE validity=0",
128 MEDIA_SVC_DB_TABLE_MEDIA, folder_path);
130 media_svc_debug("[SQL query] : %s", sql);
132 err = sqlite3_prepare_v2(handle, sql, -1, &sql_stmt, NULL);
134 if (err != SQLITE_OK) {
135 media_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
136 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
139 while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
140 _strncpy_safe(thumb_path[idx].thumbnail_path, (const char *)sqlite3_column_text(sql_stmt, 0), sizeof(thumb_path[idx]));
144 SQLITE3_FINALIZE(sql_stmt);
146 return MEDIA_INFO_ERROR_NONE;
149 int _media_svc_count_record_with_path(sqlite3 *handle, const char *path, int *count)
151 int ret = MEDIA_INFO_ERROR_NONE;
152 sqlite3_stmt *sql_stmt = NULL;
154 char *sql = sqlite3_mprintf("SELECT count(*) FROM %s WHERE path='%q'", MEDIA_SVC_DB_TABLE_MEDIA, path);
156 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
158 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
160 *count = sqlite3_column_int(sql_stmt, 0);
162 SQLITE3_FINALIZE(sql_stmt);
164 return MEDIA_INFO_ERROR_NONE;
167 int _media_svc_insert_item_with_data(sqlite3 *handle, media_svc_content_info_s *content_info, int is_burst, bool stack_query)
171 char *burst_id = NULL;
173 char * db_fields = "media_uuid, path, file_name, media_type, mime_type, size, added_time, modified_time, folder_uuid, \
174 thumbnail_path, title, album_id, album, artist, genre, composer, year, recorded_date, copyright, track_num, description,\
175 bitrate, samplerate, channel, duration, longitude, latitude, altitude, width, height, datetaken, orientation,\
176 rating, is_drm, storage_type, burst_id";
178 /* This sql is due to sqlite3_mprintf's wrong operation when using floating point in the text format */
179 /* This code will be removed when sqlite3_mprintf works clearly */
180 char *test_sql = sqlite3_mprintf("%f, %f, %f", content_info->media_meta.longitude, content_info->media_meta.latitude, content_info->media_meta.altitude);
181 sqlite3_free(test_sql);
184 int burst_id_int = 0;
185 err = _media_svc_get_burst_id(handle, &burst_id_int);
190 if (burst_id_int > 0) {
191 media_svc_debug("Burst id : %d", burst_id_int);
192 burst_id = sqlite3_mprintf("%d", burst_id_int);
195 /* Get thumbnail for burst shot */
196 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
200 err = thumbnail_request_from_db_with_size(content_info->path, thumb_path, sizeof(thumb_path), &width, &height);
202 media_svc_error("thumbnail_request_from_db failed: %d", err);
204 media_svc_debug("thumbnail_request_from_db success: %s", thumb_path);
205 err = __media_svc_malloc_and_strncpy(&(content_info->thumbnail_path), thumb_path);
207 content_info->thumbnail_path = NULL;
211 if (content_info->media_meta.width <= 0)
212 content_info->media_meta.width = width;
214 if (content_info->media_meta.height <= 0)
215 content_info->media_meta.height = height;
218 char *sql = sqlite3_mprintf("INSERT INTO %s (%s) VALUES (%Q, %Q, %Q, %d, %Q, %lld, %d, %d, %Q, \
219 %Q, %Q, %d, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, \
220 %d, %d, %d, %d, %.2f, %.2f, %.2f, %d, %d, %Q, %d, \
222 MEDIA_SVC_DB_TABLE_MEDIA, db_fields,
223 content_info->media_uuid,
225 content_info->file_name,
226 content_info->media_type,
227 content_info->mime_type,
229 content_info->added_time,
230 content_info->modified_time,
231 content_info->folder_uuid,
232 content_info->thumbnail_path, //
233 content_info->media_meta.title,
234 content_info->album_id,
235 content_info->media_meta.album,
236 content_info->media_meta.artist,
237 content_info->media_meta.genre,
238 content_info->media_meta.composer,
239 content_info->media_meta.year,
240 content_info->media_meta.recorded_date,
241 content_info->media_meta.copyright,
242 content_info->media_meta.track_num,
243 content_info->media_meta.description, //
244 content_info->media_meta.bitrate,
245 content_info->media_meta.samplerate,
246 content_info->media_meta.channel,
247 content_info->media_meta.duration,
248 content_info->media_meta.longitude,
249 content_info->media_meta.latitude,
250 content_info->media_meta.altitude,
251 content_info->media_meta.width,
252 content_info->media_meta.height,
253 content_info->media_meta.datetaken,
254 content_info->media_meta.orientation,
255 content_info->media_meta.rating,
256 content_info->is_drm,
257 content_info->storage_type,
260 if (burst_id) sqlite3_free(burst_id);
264 err = _media_svc_sql_query(handle, sql);
266 if (err != SQLITE_OK) {
267 media_svc_error("failed to insert item");
269 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
272 media_svc_debug("query : %s", sql);
273 _media_svc_sql_query_add(&g_media_svc_insert_item_query_list, &sql);
276 return MEDIA_INFO_ERROR_NONE;
279 int _media_svc_update_item_with_data(sqlite3 *handle, media_svc_content_info_s *content_info)
283 /* This sql is due to sqlite3_mprintf's wrong operation when using floating point in the text format */
284 /* This code will be removed when sqlite3_mprintf works clearly */
285 char *test_sql = sqlite3_mprintf("%f, %f, %f", content_info->media_meta.longitude, content_info->media_meta.latitude, content_info->media_meta.altitude);
286 sqlite3_free(test_sql);
288 char *sql = sqlite3_mprintf("UPDATE %s SET \
289 size=%lld, modified_time=%d, thumbnail_path=%Q, title=%Q, album_id=%d, album=%Q, artist=%Q, genre=%Q, \
290 composer=%Q, year=%Q, recorded_date=%Q, copyright=%Q, track_num=%Q, description=%Q, \
291 bitrate=%d, samplerate=%d, channel=%d, duration=%d, longitude=%f, latitude=%f, altitude=%f, width=%d, height=%d, datetaken=%Q, \
292 orientation=%d WHERE path=%Q",
293 MEDIA_SVC_DB_TABLE_MEDIA,
295 content_info->modified_time,
296 content_info->thumbnail_path,
297 content_info->media_meta.title,
298 content_info->album_id,
299 content_info->media_meta.album,
300 content_info->media_meta.artist,
301 content_info->media_meta.genre,
302 content_info->media_meta.composer,
303 content_info->media_meta.year,
304 content_info->media_meta.recorded_date,
305 content_info->media_meta.copyright,
306 content_info->media_meta.track_num,
307 content_info->media_meta.description,
308 content_info->media_meta.bitrate,
309 content_info->media_meta.samplerate,
310 content_info->media_meta.channel,
311 content_info->media_meta.duration,
312 content_info->media_meta.longitude,
313 content_info->media_meta.latitude,
314 content_info->media_meta.altitude,
315 content_info->media_meta.width,
316 content_info->media_meta.height,
317 content_info->media_meta.datetaken,
318 content_info->media_meta.orientation,
322 err = _media_svc_sql_query(handle, sql);
324 if (err != SQLITE_OK) {
325 media_svc_error("failed to update item");
327 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
330 return MEDIA_INFO_ERROR_NONE;
332 int _media_svc_get_thumbnail_path_by_path(sqlite3 *handle, const char *path, char *thumbnail_path)
334 int ret = MEDIA_INFO_ERROR_NONE;
335 sqlite3_stmt *sql_stmt = NULL;
337 char *sql = sqlite3_mprintf("SELECT thumbnail_path FROM %s WHERE path='%q'", MEDIA_SVC_DB_TABLE_MEDIA, path);
339 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
341 if (ret != MEDIA_INFO_ERROR_NONE) {
342 if(ret == MEDIA_INFO_ERROR_DATABASE_NO_RECORD) {
343 media_svc_debug("there is no thumbnail.");
346 media_svc_error("error when _media_svc_get_thumbnail_path_by_path. err = [%d]", ret);
351 _strncpy_safe(thumbnail_path, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_PATHNAME_SIZE);
353 SQLITE3_FINALIZE(sql_stmt);
355 return MEDIA_INFO_ERROR_NONE;
358 int _media_svc_get_media_type_by_path(sqlite3 *handle, const char *path, int *media_type)
360 int ret = MEDIA_INFO_ERROR_NONE;
361 sqlite3_stmt *sql_stmt = NULL;
363 char *sql = sqlite3_mprintf("SELECT media_type FROM %s WHERE path='%q'", MEDIA_SVC_DB_TABLE_MEDIA, path);
365 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
367 if (ret != MEDIA_INFO_ERROR_NONE) {
368 media_svc_error("error when _media_svc_get_media_type_by_path. err = [%d]", ret);
372 *media_type = sqlite3_column_int(sql_stmt, 0);
374 SQLITE3_FINALIZE(sql_stmt);
376 return MEDIA_INFO_ERROR_NONE;
379 int _media_svc_delete_item_by_path(sqlite3 *handle, const char *path)
382 char *sql = sqlite3_mprintf("DELETE FROM %s WHERE validity=1 AND path='%q'", MEDIA_SVC_DB_TABLE_MEDIA, path);
384 err = _media_svc_sql_query(handle, sql);
386 if (err != SQLITE_OK) {
387 media_svc_error("It failed to delete item (%d)", err);
388 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
391 return MEDIA_INFO_ERROR_NONE;
394 int _media_svc_truncate_table(sqlite3 *handle, media_svc_storage_type_e storage_type)
397 char *sql = sqlite3_mprintf("DELETE FROM %s WHERE storage_type=%d", MEDIA_SVC_DB_TABLE_MEDIA, storage_type);
399 err = _media_svc_sql_query(handle, sql);
401 if (err != SQLITE_OK) {
402 media_svc_error("It failed to truncate table (%d)", err);
403 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
406 return MEDIA_INFO_ERROR_NONE;
410 int _media_svc_delete_invalid_items(sqlite3 *handle, media_svc_storage_type_e storage_type)
413 media_svc_thumbnailpath_s *thumbpath_record = NULL;
415 int invalid_count = 0;
416 int ret = MEDIA_INFO_ERROR_NONE;
418 ret = __media_svc_count_invalid_records_with_thumbnail(handle, storage_type, &invalid_count);
419 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
421 media_svc_debug("invalid count: %d\n", invalid_count);
423 if (invalid_count > 0) {
424 thumbpath_record = (media_svc_thumbnailpath_s *)calloc( invalid_count, sizeof(media_svc_thumbnailpath_s));
425 if (thumbpath_record == NULL) {
426 media_svc_error("fail to memory allocation");
427 return MEDIA_INFO_ERROR_OUT_OF_MEMORY;
430 ret = __media_svc_get_invalid_records_with_thumbnail(handle, storage_type, invalid_count, thumbpath_record);
431 if (ret != MEDIA_INFO_ERROR_NONE) {
432 media_svc_error("error when get thumbnail record");
433 SAFE_FREE(thumbpath_record);
437 media_svc_debug("There is no item with thumbnail");
440 char *sql = sqlite3_mprintf("DELETE FROM %s WHERE validity = 0 AND storage_type=%d", MEDIA_SVC_DB_TABLE_MEDIA, storage_type);
441 err = _media_svc_sql_query(handle, sql);
443 if (err != SQLITE_OK) {
444 media_svc_error("To delete invalid items is failed(%d)", err);
445 SAFE_FREE(thumbpath_record);
446 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
449 /*Delete thumbnails*/
450 for (idx = 0; idx < invalid_count; idx++) {
451 if ((strlen(thumbpath_record[idx].thumbnail_path) > 0) && (strncmp(thumbpath_record[idx].thumbnail_path, MEDIA_SVC_THUMB_DEFAULT_PATH, sizeof(MEDIA_SVC_THUMB_DEFAULT_PATH)) != 0)) {
452 if (_media_svc_remove_file(thumbpath_record[idx].thumbnail_path) == FALSE) {
453 media_svc_error("fail to remove thumbnail file.");
454 //SAFE_FREE(thumbpath_record);
455 //return MEDIA_INFO_ERROR_INTERNAL;
460 SAFE_FREE(thumbpath_record);
462 return MEDIA_INFO_ERROR_NONE;
465 int _media_svc_delete_invalid_folder_items(sqlite3 *handle, const char *folder_path)
468 media_svc_thumbnailpath_s *thumbpath_record = NULL;
470 int invalid_count = 0;
471 int ret = MEDIA_INFO_ERROR_NONE;
473 ret = __media_svc_count_invalid_folder_records_with_thumbnail(handle, folder_path, &invalid_count);
474 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
476 media_svc_debug("invalid count: %d\n", invalid_count);
478 if (invalid_count > 0) {
479 thumbpath_record = (media_svc_thumbnailpath_s *)calloc( invalid_count, sizeof(media_svc_thumbnailpath_s));
480 if (thumbpath_record == NULL) {
481 media_svc_error("fail to memory allocation");
482 return MEDIA_INFO_ERROR_OUT_OF_MEMORY;
485 ret = __media_svc_get_invalid_folder_records_with_thumbnail(handle, folder_path, invalid_count, thumbpath_record);
486 if (ret != MEDIA_INFO_ERROR_NONE) {
487 media_svc_error("error when get thumbnail record");
488 SAFE_FREE(thumbpath_record);
492 media_svc_debug("There is no item with thumbnail");
495 char *sql = sqlite3_mprintf("DELETE FROM %s WHERE validity = 0 AND path LIKE '%q/%%'", MEDIA_SVC_DB_TABLE_MEDIA, folder_path);
496 err = _media_svc_sql_query(handle, sql);
498 if (err != SQLITE_OK) {
499 media_svc_error("To delete invalid items is failed(%d)", err);
500 SAFE_FREE(thumbpath_record);
501 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
504 /*Delete thumbnails*/
505 for (idx = 0; idx < invalid_count; idx++) {
506 if ((strlen(thumbpath_record[idx].thumbnail_path) > 0) && (strncmp(thumbpath_record[idx].thumbnail_path, MEDIA_SVC_THUMB_DEFAULT_PATH, sizeof(MEDIA_SVC_THUMB_DEFAULT_PATH)) != 0)) {
507 if (_media_svc_remove_file(thumbpath_record[idx].thumbnail_path) == FALSE) {
508 media_svc_error("fail to remove thumbnail file [%s].", thumbpath_record[idx].thumbnail_path);
509 //SAFE_FREE(thumbpath_record);
510 //return MEDIA_INFO_ERROR_INTERNAL;
515 SAFE_FREE(thumbpath_record);
517 return MEDIA_INFO_ERROR_NONE;
521 int _media_svc_update_item_validity(sqlite3 *handle, const char *path, int validity, bool stack_query)
525 char *sql = sqlite3_mprintf("UPDATE %s SET validity=%d WHERE path= '%q'", MEDIA_SVC_DB_TABLE_MEDIA, validity, path);
528 err = _media_svc_sql_query(handle, sql);
530 if (err != SQLITE_OK) {
531 media_svc_error("To update item as valid is failed(%d)", err);
532 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
535 _media_svc_sql_query_add(&g_media_svc_item_validity_query_list, &sql);
538 return MEDIA_INFO_ERROR_NONE;
541 int _media_svc_update_thumbnail_path(sqlite3 *handle, const char *path, const char *thumb_path)
545 char *sql = sqlite3_mprintf("UPDATE %s SET thumbnail_path=%Q WHERE path= %Q", MEDIA_SVC_DB_TABLE_MEDIA, thumb_path, path);
547 err = _media_svc_sql_query(handle, sql);
549 if (err != SQLITE_OK) {
550 media_svc_error("To update thumb path failed(%d)", err);
551 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
554 return MEDIA_INFO_ERROR_NONE;
557 int _media_svc_update_storage_item_validity(sqlite3 *handle, media_svc_storage_type_e storage_type, int validity)
560 char *sql = sqlite3_mprintf("UPDATE %s SET validity=%d WHERE storage_type=%d", MEDIA_SVC_DB_TABLE_MEDIA, validity, storage_type);
561 err = _media_svc_sql_query(handle, sql);
563 if (err != SQLITE_OK) {
564 media_svc_error("To update item as valid is failed(%d)", err);
565 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
568 return MEDIA_INFO_ERROR_NONE;
571 int _media_svc_update_folder_item_validity(sqlite3 *handle, const char *folder_path, int validity)
574 int ret = MEDIA_INFO_ERROR_NONE;
576 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
577 sqlite3_stmt *sql_stmt = NULL;
580 sql = sqlite3_mprintf("SELECT folder_uuid FROM %s WHERE path='%q'", MEDIA_SVC_DB_TABLE_FOLDER, folder_path);
581 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
582 if (ret != MEDIA_INFO_ERROR_NONE) {
583 media_svc_error("error when get folder_id. err = [%d]", ret);
587 _strncpy_safe(folder_uuid, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE+1);
588 SQLITE3_FINALIZE(sql_stmt);
590 /*Update folder item validity*/
591 sql = sqlite3_mprintf("UPDATE %s SET validity=%d WHERE folder_uuid='%q'", MEDIA_SVC_DB_TABLE_MEDIA, validity, folder_uuid);
592 err = _media_svc_sql_query(handle, sql);
594 if (err != SQLITE_OK) {
595 media_svc_error("To update folder item as valid is failed(%d)", err);
596 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
599 return MEDIA_INFO_ERROR_NONE;
602 int _media_svc_update_recursive_folder_item_validity(sqlite3 *handle, const char *folder_path, int validity)
606 /*Update folder item validity*/
607 char *sql = sqlite3_mprintf("UPDATE %s SET validity=%d WHERE path LIKE '%q/%%'", MEDIA_SVC_DB_TABLE_MEDIA, validity, folder_path);
608 err = _media_svc_sql_query(handle, sql);
610 if (err != SQLITE_OK) {
611 media_svc_error("To update recursive folder item validity is failed(%d)", err);
612 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
615 return MEDIA_INFO_ERROR_NONE;
618 int _media_svc_update_item_by_path(sqlite3 *handle, const char *src_path, media_svc_storage_type_e dest_storage, const char *dest_path,
619 const char *file_name, int modified_time, const char *folder_uuid, const char *thumb_path, bool stack_query)
621 /* update path, filename, modified_time, folder_uuid, thumbnail_path, */
622 /* played_count, last_played_time, last_played_position, favourite, storaget_type*/
627 if(thumb_path != NULL) {
628 sql = sqlite3_mprintf("UPDATE %s SET \
629 path=%Q, file_name=%Q, modified_time=%d, folder_uuid=%Q, thumbnail_path=%Q, storage_type=%d, \
630 played_count=0, last_played_time=0, last_played_position=0 \
632 MEDIA_SVC_DB_TABLE_MEDIA, dest_path, file_name, modified_time, folder_uuid, thumb_path, dest_storage, src_path);
634 sql = sqlite3_mprintf("UPDATE %s SET \
635 path=%Q, file_name=%Q, modified_time=%d, folder_uuid=%Q, storage_type=%d, \
636 played_count=0, last_played_time=0, last_played_position=0 \
638 MEDIA_SVC_DB_TABLE_MEDIA, dest_path, file_name, modified_time, folder_uuid, dest_storage, src_path);
642 err = _media_svc_sql_query(handle, sql);
644 if (err != SQLITE_OK) {
645 media_svc_error("It failed to update metadata (%d)", err);
646 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
649 _media_svc_sql_query_add(&g_media_svc_move_item_query_list, &sql);
652 return MEDIA_INFO_ERROR_NONE;
655 int _media_svc_list_query_do(sqlite3 *handle, media_svc_query_type_e query_type)
657 int ret = MEDIA_INFO_ERROR_NONE;
659 ret = _media_svc_sql_begin_trans(handle);
660 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
662 if (query_type == MEDIA_SVC_QUERY_SET_ITEM_VALIDITY)
663 ret = _media_svc_sql_query_list(handle, &g_media_svc_item_validity_query_list);
664 else if (query_type == MEDIA_SVC_QUERY_MOVE_ITEM)
665 ret = _media_svc_sql_query_list(handle, &g_media_svc_move_item_query_list);
666 else if (query_type == MEDIA_SVC_QUERY_INSERT_ITEM)
667 ret = _media_svc_sql_query_list(handle, &g_media_svc_insert_item_query_list);
669 ret = MEDIA_INFO_ERROR_INVALID_PARAMETER;
671 if (ret != MEDIA_INFO_ERROR_NONE) {
672 media_svc_error("_media_svc_list_query_do failed. start rollback");
673 _media_svc_sql_rollback_trans(handle);
677 ret = _media_svc_sql_end_trans(handle);
678 if (ret != MEDIA_INFO_ERROR_NONE) {
679 media_svc_error("mb_svc_sqlite3_commit_trans failed.. Now start to rollback\n");
680 _media_svc_sql_rollback_trans(handle);
684 return MEDIA_INFO_ERROR_NONE;
687 int _media_svc_get_media_id_by_path(sqlite3 *handle, const char *path, char *media_uuid, int max_length)
689 int ret = MEDIA_INFO_ERROR_NONE;
690 sqlite3_stmt *sql_stmt = NULL;
691 char *sql = sqlite3_mprintf("SELECT media_uuid FROM %s WHERE validity=1 AND path='%q'",
692 MEDIA_SVC_DB_TABLE_MEDIA, path);
694 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
696 if (ret != MEDIA_INFO_ERROR_NONE) {
697 media_svc_error("error when __media_svc_count_invalid_records_with_thumbnail. err = [%d]", ret);
701 strncpy(media_uuid, (const char*)sqlite3_column_text(sql_stmt, 0), max_length);
702 media_uuid[max_length - 1] = '\0';
704 SQLITE3_FINALIZE(sql_stmt);
706 return MEDIA_INFO_ERROR_NONE;
709 int _media_svc_get_burst_id(sqlite3 *handle, int *id)
711 int ret = MEDIA_INFO_ERROR_NONE;
713 sqlite3_stmt *sql_stmt = NULL;
714 char *sql = sqlite3_mprintf("SELECT max(CAST(burst_id AS INTEGER)) FROM %s", MEDIA_SVC_DB_TABLE_MEDIA);
716 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
718 if (ret != MEDIA_INFO_ERROR_NONE) {
719 media_svc_error("error when _media_svc_get_burst_id. err = [%d]", ret);
723 cur_id = sqlite3_column_int(sql_stmt, 0);
725 SQLITE3_FINALIZE(sql_stmt);
727 return MEDIA_INFO_ERROR_NONE;
730 int _media_svc_get_noti_info(sqlite3 *handle, const char *path, int update_item, media_svc_noti_item **item)
732 int ret = MEDIA_INFO_ERROR_NONE;
733 sqlite3_stmt *sql_stmt = NULL;
737 media_svc_error("_media_svc_get_noti_info failed");
738 return MEDIA_INFO_ERROR_INVALID_PARAMETER;
741 if (update_item == MS_MEDIA_ITEM_FILE) {
742 sql = sqlite3_mprintf("SELECT media_uuid, media_type, mime_type FROM %s WHERE path=%Q", MEDIA_SVC_DB_TABLE_MEDIA, path);
743 } else if (update_item == MS_MEDIA_ITEM_DIRECTORY) {
744 sql = sqlite3_mprintf("SELECT folder_uuid FROM %s WHERE path=%Q", MEDIA_SVC_DB_TABLE_FOLDER, path);
746 media_svc_error("_media_svc_get_noti_info failed : update item");
747 return MEDIA_INFO_ERROR_INVALID_PARAMETER;
750 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
752 if (ret != MEDIA_INFO_ERROR_NONE) {
753 media_svc_error("error when _media_svc_get_noti_info. err = [%d]", ret);
757 *item = calloc(1, sizeof(media_svc_noti_item));
759 media_svc_error("_media_svc_get_noti_info failed : calloc");
760 return MEDIA_INFO_ERROR_OUT_OF_MEMORY;
763 if (update_item == MS_MEDIA_ITEM_FILE) {
764 if (sqlite3_column_text(sql_stmt, 0))
765 (*item)->media_uuid = strdup((const char *)sqlite3_column_text(sql_stmt, 0));
767 (*item)->media_type = sqlite3_column_int(sql_stmt, 1);
769 if (sqlite3_column_text(sql_stmt, 2))
770 (*item)->mime_type = strdup((const char *)sqlite3_column_text(sql_stmt, 2));
771 } else if (update_item == MS_MEDIA_ITEM_DIRECTORY) {
772 if (sqlite3_column_text(sql_stmt, 0))
773 (*item)->media_uuid = strdup((const char *)sqlite3_column_text(sql_stmt, 0));
776 SQLITE3_FINALIZE(sql_stmt);
778 return MEDIA_INFO_ERROR_NONE;
781 int _media_svc_count_invalid_folder_items(sqlite3 *handle, const char *folder_path, int *count)
783 int ret = MEDIA_INFO_ERROR_NONE;
784 sqlite3_stmt *sql_stmt = NULL;
785 char *sql = sqlite3_mprintf("SELECT count(*) FROM %s WHERE validity=0 AND path LIKE '%q/%%'",
786 MEDIA_SVC_DB_TABLE_MEDIA, folder_path);
788 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
790 if (ret != MEDIA_INFO_ERROR_NONE) {
791 media_svc_error("error when __media_svc_count_invalid_folder_records_with_thumbnail. err = [%d]", ret);
795 *count = sqlite3_column_int(sql_stmt, 0);
797 SQLITE3_FINALIZE(sql_stmt);
799 return MEDIA_INFO_ERROR_NONE;
802 int _media_svc_get_thumbnail_count(sqlite3 *handle, const char *thumb_path, int *count)
804 int ret = MEDIA_INFO_ERROR_NONE;
805 sqlite3_stmt *sql_stmt = NULL;
806 char *sql = sqlite3_mprintf("SELECT count(*) FROM %s WHERE thumbnail_path=%Q", MEDIA_SVC_DB_TABLE_MEDIA, thumb_path);
808 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
810 if (ret != MEDIA_INFO_ERROR_NONE) {
811 media_svc_error("error when _media_svc_get_thumbnail_count. err = [%d]", ret);
815 *count = sqlite3_column_int(sql_stmt, 0);
817 SQLITE3_FINALIZE(sql_stmt);
819 return MEDIA_INFO_ERROR_NONE;