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.
25 #include "media-svc-media.h"
26 #include "media-svc-media-folder.h"
27 #include "media-svc-error.h"
28 #include "media-svc-debug.h"
29 #include "media-svc-util.h"
30 #include "media-svc-db-utils.h"
31 #include "media-svc-noti.h"
33 #define GLOBAL_USER 0 //#define tzplatform_getenv(TZ_GLOBAL) //TODO
36 char thumbnail_path[MEDIA_SVC_PATHNAME_SIZE];
37 }media_svc_thumbnailpath_s;
39 static __thread GList *g_media_svc_item_validity_query_list = NULL;
40 static __thread GList *g_media_svc_insert_item_query_list = NULL;
41 __thread GList *g_media_svc_move_item_query_list = NULL;
43 static int __media_svc_count_invalid_records_with_thumbnail(sqlite3 *handle, media_svc_storage_type_e storage_type, int *count);
44 static int __media_svc_get_invalid_records_with_thumbnail(sqlite3 *handle, media_svc_storage_type_e storage_type,
45 int count, media_svc_thumbnailpath_s * thumb_path);
46 static int __media_svc_count_invalid_folder_records_with_thumbnail(sqlite3 *handle, const char *folder_path, int *count);
47 static int __media_svc_get_invalid_folder_records_with_thumbnail(sqlite3 *handle, const char *folder_path,
48 int count, media_svc_thumbnailpath_s * thumb_path);
50 static int __media_svc_count_invalid_records_with_thumbnail(sqlite3 *handle, media_svc_storage_type_e storage_type, int *count)
52 int ret = MEDIA_INFO_ERROR_NONE;
53 sqlite3_stmt *sql_stmt = NULL;
54 char *sql = sqlite3_mprintf("SELECT count(*) FROM %s WHERE validity=0 AND storage_type=%d AND thumbnail_path IS NOT NULL",
55 MEDIA_SVC_DB_TABLE_MEDIA, storage_type);
57 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
59 if (ret != MEDIA_INFO_ERROR_NONE) {
60 media_svc_error("error when __media_svc_count_invalid_records_with_thumbnail. err = [%d]", ret);
64 *count = sqlite3_column_int(sql_stmt, 0);
66 SQLITE3_FINALIZE(sql_stmt);
68 return MEDIA_INFO_ERROR_NONE;
72 static int __media_svc_get_invalid_records_with_thumbnail(sqlite3 *handle, media_svc_storage_type_e storage_type,
73 int count, media_svc_thumbnailpath_s * thumb_path)
77 sqlite3_stmt *sql_stmt = NULL;
79 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",
80 MEDIA_SVC_DB_TABLE_MEDIA, storage_type);
82 media_svc_debug("[SQL query] : %s", sql);
84 err = sqlite3_prepare_v2(handle, sql, -1, &sql_stmt, NULL);
86 if (err != SQLITE_OK) {
87 media_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
88 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
91 while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
92 _strncpy_safe(thumb_path[idx].thumbnail_path, (const char *)sqlite3_column_text(sql_stmt, 0), sizeof(thumb_path[idx]));
93 //media_svc_debug("thumb_path[%d]=[%s]", idx, thumb_path[idx].thumbnail_path);
97 SQLITE3_FINALIZE(sql_stmt);
99 return MEDIA_INFO_ERROR_NONE;
102 static int __media_svc_count_invalid_folder_records_with_thumbnail(sqlite3 *handle, const char *folder_path, int *count)
104 int ret = MEDIA_INFO_ERROR_NONE;
105 sqlite3_stmt *sql_stmt = NULL;
106 char *sql = sqlite3_mprintf("SELECT count(*) FROM %s WHERE validity=0 AND path LIKE '%q/%%' AND thumbnail_path IS NOT NULL",
107 MEDIA_SVC_DB_TABLE_MEDIA, folder_path);
109 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
111 if (ret != MEDIA_INFO_ERROR_NONE) {
112 media_svc_error("error when __media_svc_count_invalid_folder_records_with_thumbnail. err = [%d]", ret);
116 *count = sqlite3_column_int(sql_stmt, 0);
118 SQLITE3_FINALIZE(sql_stmt);
120 return MEDIA_INFO_ERROR_NONE;
124 static int __media_svc_get_invalid_folder_records_with_thumbnail(sqlite3 *handle, const char *folder_path,
125 int count, media_svc_thumbnailpath_s * thumb_path)
129 sqlite3_stmt *sql_stmt = NULL;
131 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",
132 MEDIA_SVC_DB_TABLE_MEDIA, folder_path);
134 media_svc_debug("[SQL query] : %s", sql);
136 err = sqlite3_prepare_v2(handle, sql, -1, &sql_stmt, NULL);
138 if (err != SQLITE_OK) {
139 media_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
140 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
143 while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
144 _strncpy_safe(thumb_path[idx].thumbnail_path, (const char *)sqlite3_column_text(sql_stmt, 0), sizeof(thumb_path[idx]));
148 SQLITE3_FINALIZE(sql_stmt);
150 return MEDIA_INFO_ERROR_NONE;
153 int _media_svc_count_record_with_path(sqlite3 *handle, const char *path, int *count)
155 int ret = MEDIA_INFO_ERROR_NONE;
156 sqlite3_stmt *sql_stmt = NULL;
158 char *sql = sqlite3_mprintf("SELECT count(*) FROM %s WHERE path='%q'", MEDIA_SVC_DB_TABLE_MEDIA, path);
160 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
162 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
164 *count = sqlite3_column_int(sql_stmt, 0);
166 SQLITE3_FINALIZE(sql_stmt);
168 return MEDIA_INFO_ERROR_NONE;
171 char* _media_svc_get_thumb_default_path(uid_t uid)
173 char *result_psswd = NULL;
174 struct group *grpinfo = NULL;
176 if(uid == GLOBAL_USER)
178 result_psswd = strdup(MEDIA_SVC_THUMB_DEFAULT_PATH);
179 grpinfo = getgrnam("root");
180 if(grpinfo == NULL) {
181 media_svc_error("getgrnam(users) returns NULL !");
187 struct passwd *userinfo = getpwuid(uid);
188 if(userinfo == NULL) {
189 media_svc_error("getpwuid(%d) returns NULL !", uid);
192 grpinfo = getgrnam("users");
193 if(grpinfo == NULL) {
194 media_svc_error("getgrnam(users) returns NULL !");
197 // Compare git_t type and not group name
198 if (grpinfo->gr_gid != userinfo->pw_gid) {
199 media_svc_error("UID [%d] does not belong to 'users' group!", uid);
202 asprintf(&result_psswd, "%s/data/file-manager-service/.thumb/thumb_default.png", userinfo->pw_dir);
208 int _media_svc_insert_item_with_data(sqlite3 *handle, media_svc_content_info_s *content_info, int is_burst, bool stack_query, uid_t uid)
212 char *burst_id = NULL;
214 char * db_fields = "media_uuid, path, file_name, media_type, mime_type, size, added_time, modified_time, folder_uuid, \
215 thumbnail_path, title, album_id, album, artist, genre, composer, year, recorded_date, copyright, track_num, description,\
216 bitrate, samplerate, channel, duration, longitude, latitude, altitude, width, height, datetaken, orientation,\
217 rating, is_drm, storage_type, burst_id";
219 /* This sql is due to sqlite3_mprintf's wrong operation when using floating point in the text format */
220 /* This code will be removed when sqlite3_mprintf works clearly */
221 char *test_sql = sqlite3_mprintf("%f, %f, %f", content_info->media_meta.longitude, content_info->media_meta.latitude, content_info->media_meta.altitude);
222 sqlite3_free(test_sql);
225 int burst_id_int = 0;
226 err = _media_svc_get_burst_id(handle, &burst_id_int);
231 if (burst_id_int > 0) {
232 media_svc_debug("Burst id : %d", burst_id_int);
233 burst_id = sqlite3_mprintf("%d", burst_id_int);
236 /* Get thumbnail for burst shot */
237 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
241 err = thumbnail_request_from_db_with_size(content_info->path, thumb_path, sizeof(thumb_path), &width, &height, uid);
243 media_svc_error("thumbnail_request_from_db failed: %d", err);
245 media_svc_debug("thumbnail_request_from_db success: %s", thumb_path);
246 err = __media_svc_malloc_and_strncpy(&(content_info->thumbnail_path), thumb_path);
248 content_info->thumbnail_path = NULL;
252 if (content_info->media_meta.width <= 0)
253 content_info->media_meta.width = width;
255 if (content_info->media_meta.height <= 0)
256 content_info->media_meta.height = height;
259 char *sql = sqlite3_mprintf("INSERT INTO %s (%s) VALUES (%Q, %Q, %Q, %d, %Q, %lld, %d, %d, %Q, \
260 %Q, %Q, %d, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, \
261 %d, %d, %d, %d, %.2f, %.2f, %.2f, %d, %d, %Q, %d, \
263 MEDIA_SVC_DB_TABLE_MEDIA, db_fields,
264 content_info->media_uuid,
266 content_info->file_name,
267 content_info->media_type,
268 content_info->mime_type,
270 content_info->added_time,
271 content_info->modified_time,
272 content_info->folder_uuid,
273 content_info->thumbnail_path, //
274 content_info->media_meta.title,
275 content_info->album_id,
276 content_info->media_meta.album,
277 content_info->media_meta.artist,
278 content_info->media_meta.genre,
279 content_info->media_meta.composer,
280 content_info->media_meta.year,
281 content_info->media_meta.recorded_date,
282 content_info->media_meta.copyright,
283 content_info->media_meta.track_num,
284 content_info->media_meta.description, //
285 content_info->media_meta.bitrate,
286 content_info->media_meta.samplerate,
287 content_info->media_meta.channel,
288 content_info->media_meta.duration,
289 content_info->media_meta.longitude,
290 content_info->media_meta.latitude,
291 content_info->media_meta.altitude,
292 content_info->media_meta.width,
293 content_info->media_meta.height,
294 content_info->media_meta.datetaken,
295 content_info->media_meta.orientation,
296 content_info->media_meta.rating,
297 content_info->is_drm,
298 content_info->storage_type,
301 if (burst_id) sqlite3_free(burst_id);
305 err = _media_svc_sql_query(handle, sql, uid);
307 if (err != SQLITE_OK) {
308 media_svc_error("failed to insert item");
310 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
313 media_svc_debug("query : %s", sql);
314 _media_svc_sql_query_add(&g_media_svc_insert_item_query_list, &sql);
317 return MEDIA_INFO_ERROR_NONE;
320 int _media_svc_update_item_with_data(sqlite3 *handle, media_svc_content_info_s *content_info, uid_t uid)
324 /* This sql is due to sqlite3_mprintf's wrong operation when using floating point in the text format */
325 /* This code will be removed when sqlite3_mprintf works clearly */
326 char *test_sql = sqlite3_mprintf("%f, %f, %f", content_info->media_meta.longitude, content_info->media_meta.latitude, content_info->media_meta.altitude);
327 sqlite3_free(test_sql);
329 char *sql = sqlite3_mprintf("UPDATE %s SET \
330 size=%lld, modified_time=%d, thumbnail_path=%Q, title=%Q, album_id=%d, album=%Q, artist=%Q, genre=%Q, \
331 composer=%Q, year=%Q, recorded_date=%Q, copyright=%Q, track_num=%Q, description=%Q, \
332 bitrate=%d, samplerate=%d, channel=%d, duration=%d, longitude=%f, latitude=%f, altitude=%f, width=%d, height=%d, datetaken=%Q, \
333 orientation=%d WHERE path=%Q",
334 MEDIA_SVC_DB_TABLE_MEDIA,
336 content_info->modified_time,
337 content_info->thumbnail_path,
338 content_info->media_meta.title,
339 content_info->album_id,
340 content_info->media_meta.album,
341 content_info->media_meta.artist,
342 content_info->media_meta.genre,
343 content_info->media_meta.composer,
344 content_info->media_meta.year,
345 content_info->media_meta.recorded_date,
346 content_info->media_meta.copyright,
347 content_info->media_meta.track_num,
348 content_info->media_meta.description,
349 content_info->media_meta.bitrate,
350 content_info->media_meta.samplerate,
351 content_info->media_meta.channel,
352 content_info->media_meta.duration,
353 content_info->media_meta.longitude,
354 content_info->media_meta.latitude,
355 content_info->media_meta.altitude,
356 content_info->media_meta.width,
357 content_info->media_meta.height,
358 content_info->media_meta.datetaken,
359 content_info->media_meta.orientation,
363 err = _media_svc_sql_query(handle, sql, uid);
365 if (err != SQLITE_OK) {
366 media_svc_error("failed to update item");
368 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
371 return MEDIA_INFO_ERROR_NONE;
373 int _media_svc_get_thumbnail_path_by_path(sqlite3 *handle, const char *path, char *thumbnail_path)
375 int ret = MEDIA_INFO_ERROR_NONE;
376 sqlite3_stmt *sql_stmt = NULL;
378 char *sql = sqlite3_mprintf("SELECT thumbnail_path FROM %s WHERE path='%q'", MEDIA_SVC_DB_TABLE_MEDIA, path);
380 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
382 if (ret != MEDIA_INFO_ERROR_NONE) {
383 if(ret == MEDIA_INFO_ERROR_DATABASE_NO_RECORD) {
384 media_svc_debug("there is no thumbnail.");
387 media_svc_error("error when _media_svc_get_thumbnail_path_by_path. err = [%d]", ret);
392 _strncpy_safe(thumbnail_path, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_PATHNAME_SIZE);
394 SQLITE3_FINALIZE(sql_stmt);
396 return MEDIA_INFO_ERROR_NONE;
399 int _media_svc_get_media_type_by_path(sqlite3 *handle, const char *path, int *media_type)
401 int ret = MEDIA_INFO_ERROR_NONE;
402 sqlite3_stmt *sql_stmt = NULL;
404 char *sql = sqlite3_mprintf("SELECT media_type FROM %s WHERE path='%q'", MEDIA_SVC_DB_TABLE_MEDIA, path);
406 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
408 if (ret != MEDIA_INFO_ERROR_NONE) {
409 media_svc_error("error when _media_svc_get_media_type_by_path. err = [%d]", ret);
413 *media_type = sqlite3_column_int(sql_stmt, 0);
415 SQLITE3_FINALIZE(sql_stmt);
417 return MEDIA_INFO_ERROR_NONE;
420 int _media_svc_delete_item_by_path(sqlite3 *handle, const char *path, uid_t uid)
423 char *sql = sqlite3_mprintf("DELETE FROM %s WHERE validity=1 AND path='%q'", MEDIA_SVC_DB_TABLE_MEDIA, path);
425 err = _media_svc_sql_query(handle, sql, uid);
427 if (err != SQLITE_OK) {
428 media_svc_error("It failed to delete item (%d)", err);
429 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
432 return MEDIA_INFO_ERROR_NONE;
435 int _media_svc_truncate_table(sqlite3 *handle, media_svc_storage_type_e storage_type, uid_t uid)
438 char *sql = sqlite3_mprintf("DELETE FROM %s WHERE storage_type=%d", MEDIA_SVC_DB_TABLE_MEDIA, storage_type);
440 err = _media_svc_sql_query(handle, sql, uid);
442 if (err != SQLITE_OK) {
443 media_svc_error("It failed to truncate table (%d)", err);
444 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
447 return MEDIA_INFO_ERROR_NONE;
451 int _media_svc_delete_invalid_items(sqlite3 *handle, media_svc_storage_type_e storage_type, uid_t uid)
454 media_svc_thumbnailpath_s *thumbpath_record = NULL;
456 int invalid_count = 0;
457 int ret = MEDIA_INFO_ERROR_NONE;
459 ret = __media_svc_count_invalid_records_with_thumbnail(handle, storage_type, &invalid_count);
460 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
462 media_svc_debug("invalid count: %d\n", invalid_count);
464 if (invalid_count > 0) {
465 thumbpath_record = (media_svc_thumbnailpath_s *)calloc( invalid_count, sizeof(media_svc_thumbnailpath_s));
466 if (thumbpath_record == NULL) {
467 media_svc_error("fail to memory allocation");
468 return MEDIA_INFO_ERROR_OUT_OF_MEMORY;
471 ret = __media_svc_get_invalid_records_with_thumbnail(handle, storage_type, invalid_count, thumbpath_record);
472 if (ret != MEDIA_INFO_ERROR_NONE) {
473 media_svc_error("error when get thumbnail record");
474 SAFE_FREE(thumbpath_record);
478 media_svc_debug("There is no item with thumbnail");
481 char *sql = sqlite3_mprintf("DELETE FROM %s WHERE validity = 0 AND storage_type=%d", MEDIA_SVC_DB_TABLE_MEDIA, storage_type);
482 err = _media_svc_sql_query(handle, sql, uid);
484 if (err != SQLITE_OK) {
485 media_svc_error("To delete invalid items is failed(%d)", err);
486 SAFE_FREE(thumbpath_record);
487 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
490 /*Delete thumbnails*/
491 for (idx = 0; idx < invalid_count; idx++) {
492 if ((strlen(thumbpath_record[idx].thumbnail_path) > 0) && (strncmp(thumbpath_record[idx].thumbnail_path, _media_svc_get_thumb_default_path(uid), sizeof(_media_svc_get_thumb_default_path(uid))) != 0)) {
493 if (_media_svc_remove_file(thumbpath_record[idx].thumbnail_path) == FALSE) {
494 media_svc_error("fail to remove thumbnail file.");
495 //SAFE_FREE(thumbpath_record);
496 //return MEDIA_INFO_ERROR_INTERNAL;
501 SAFE_FREE(thumbpath_record);
503 return MEDIA_INFO_ERROR_NONE;
506 int _media_svc_delete_invalid_folder_items(sqlite3 *handle, const char *folder_path, uid_t uid)
509 media_svc_thumbnailpath_s *thumbpath_record = NULL;
511 int invalid_count = 0;
512 int ret = MEDIA_INFO_ERROR_NONE;
514 ret = __media_svc_count_invalid_folder_records_with_thumbnail(handle, folder_path, &invalid_count);
515 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
517 media_svc_debug("invalid count: %d\n", invalid_count);
519 if (invalid_count > 0) {
520 thumbpath_record = (media_svc_thumbnailpath_s *)calloc( invalid_count, sizeof(media_svc_thumbnailpath_s));
521 if (thumbpath_record == NULL) {
522 media_svc_error("fail to memory allocation");
523 return MEDIA_INFO_ERROR_OUT_OF_MEMORY;
526 ret = __media_svc_get_invalid_folder_records_with_thumbnail(handle, folder_path, invalid_count, thumbpath_record);
527 if (ret != MEDIA_INFO_ERROR_NONE) {
528 media_svc_error("error when get thumbnail record");
529 SAFE_FREE(thumbpath_record);
533 media_svc_debug("There is no item with thumbnail");
536 char *sql = sqlite3_mprintf("DELETE FROM %s WHERE validity = 0 AND path LIKE '%q/%%'", MEDIA_SVC_DB_TABLE_MEDIA, folder_path);
537 err = _media_svc_sql_query(handle, sql, uid);
539 if (err != SQLITE_OK) {
540 media_svc_error("To delete invalid items is failed(%d)", err);
541 SAFE_FREE(thumbpath_record);
542 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
545 /*Delete thumbnails*/
546 for (idx = 0; idx < invalid_count; idx++) {
547 if ((strlen(thumbpath_record[idx].thumbnail_path) > 0) && (strncmp(thumbpath_record[idx].thumbnail_path, _media_svc_get_thumb_default_path(uid), sizeof(_media_svc_get_thumb_default_path(uid))) != 0)) {
548 if (_media_svc_remove_file(thumbpath_record[idx].thumbnail_path) == FALSE) {
549 media_svc_error("fail to remove thumbnail file [%s].", thumbpath_record[idx].thumbnail_path);
550 //SAFE_FREE(thumbpath_record);
551 //return MEDIA_INFO_ERROR_INTERNAL;
556 SAFE_FREE(thumbpath_record);
558 return MEDIA_INFO_ERROR_NONE;
562 int _media_svc_update_item_validity(sqlite3 *handle, const char *path, int validity, bool stack_query, uid_t uid)
566 char *sql = sqlite3_mprintf("UPDATE %s SET validity=%d WHERE path= '%q'", MEDIA_SVC_DB_TABLE_MEDIA, validity, path);
569 err = _media_svc_sql_query(handle, sql, uid);
571 if (err != SQLITE_OK) {
572 media_svc_error("To update item as valid is failed(%d)", err);
573 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
576 _media_svc_sql_query_add(&g_media_svc_item_validity_query_list, &sql);
579 return MEDIA_INFO_ERROR_NONE;
582 int _media_svc_update_thumbnail_path(sqlite3 *handle, const char *path, const char *thumb_path, uid_t uid)
586 char *sql = sqlite3_mprintf("UPDATE %s SET thumbnail_path=%Q WHERE path= %Q", MEDIA_SVC_DB_TABLE_MEDIA, thumb_path, path);
588 err = _media_svc_sql_query(handle, sql, uid);
590 if (err != SQLITE_OK) {
591 media_svc_error("To update thumb path failed(%d)", err);
592 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
595 return MEDIA_INFO_ERROR_NONE;
598 int _media_svc_update_storage_item_validity(sqlite3 *handle, media_svc_storage_type_e storage_type, int validity, uid_t uid)
601 char *sql = sqlite3_mprintf("UPDATE %s SET validity=%d WHERE storage_type=%d", MEDIA_SVC_DB_TABLE_MEDIA, validity, storage_type);
602 err = _media_svc_sql_query(handle, sql, uid);
604 if (err != SQLITE_OK) {
605 media_svc_error("To update item as valid is failed(%d)", err);
606 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
609 return MEDIA_INFO_ERROR_NONE;
612 int _media_svc_update_folder_item_validity(sqlite3 *handle, const char *folder_path, int validity, uid_t uid)
615 int ret = MEDIA_INFO_ERROR_NONE;
617 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
618 sqlite3_stmt *sql_stmt = NULL;
621 sql = sqlite3_mprintf("SELECT folder_uuid FROM %s WHERE path='%q'", MEDIA_SVC_DB_TABLE_FOLDER, folder_path);
622 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
623 if (ret != MEDIA_INFO_ERROR_NONE) {
624 media_svc_error("error when get folder_id. err = [%d]", ret);
628 _strncpy_safe(folder_uuid, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE+1);
629 SQLITE3_FINALIZE(sql_stmt);
631 /*Update folder item validity*/
632 sql = sqlite3_mprintf("UPDATE %s SET validity=%d WHERE folder_uuid='%q'", MEDIA_SVC_DB_TABLE_MEDIA, validity, folder_uuid);
633 err = _media_svc_sql_query(handle, sql, uid);
635 if (err != SQLITE_OK) {
636 media_svc_error("To update folder item as valid is failed(%d)", err);
637 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
640 return MEDIA_INFO_ERROR_NONE;
643 int _media_svc_update_recursive_folder_item_validity(sqlite3 *handle, const char *folder_path, int validity, uid_t uid)
647 /*Update folder item validity*/
648 char *sql = sqlite3_mprintf("UPDATE %s SET validity=%d WHERE path LIKE '%q/%%'", MEDIA_SVC_DB_TABLE_MEDIA, validity, folder_path);
649 err = _media_svc_sql_query(handle, sql, uid);
651 if (err != SQLITE_OK) {
652 media_svc_error("To update recursive folder item validity is failed(%d)", err);
653 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
656 return MEDIA_INFO_ERROR_NONE;
659 int _media_svc_update_item_by_path(sqlite3 *handle, const char *src_path, media_svc_storage_type_e dest_storage, const char *dest_path,
660 const char *file_name, int modified_time, const char *folder_uuid, const char *thumb_path, bool stack_query, uid_t uid)
662 /* update path, filename, modified_time, folder_uuid, thumbnail_path, */
663 /* played_count, last_played_time, last_played_position, favourite, storaget_type*/
668 if(thumb_path != NULL) {
669 sql = sqlite3_mprintf("UPDATE %s SET \
670 path=%Q, file_name=%Q, modified_time=%d, folder_uuid=%Q, thumbnail_path=%Q, storage_type=%d, \
671 played_count=0, last_played_time=0, last_played_position=0 \
673 MEDIA_SVC_DB_TABLE_MEDIA, dest_path, file_name, modified_time, folder_uuid, thumb_path, dest_storage, src_path);
675 sql = sqlite3_mprintf("UPDATE %s SET \
676 path=%Q, file_name=%Q, modified_time=%d, folder_uuid=%Q, storage_type=%d, \
677 played_count=0, last_played_time=0, last_played_position=0 \
679 MEDIA_SVC_DB_TABLE_MEDIA, dest_path, file_name, modified_time, folder_uuid, dest_storage, src_path);
683 err = _media_svc_sql_query(handle, sql, uid);
685 if (err != SQLITE_OK) {
686 media_svc_error("It failed to update metadata (%d)", err);
687 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
690 _media_svc_sql_query_add(&g_media_svc_move_item_query_list, &sql);
693 return MEDIA_INFO_ERROR_NONE;
696 int _media_svc_list_query_do(sqlite3 *handle, media_svc_query_type_e query_type, uid_t uid)
698 int ret = MEDIA_INFO_ERROR_NONE;
700 ret = _media_svc_sql_begin_trans(handle, uid);
701 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
703 if (query_type == MEDIA_SVC_QUERY_SET_ITEM_VALIDITY)
704 ret = _media_svc_sql_query_list(handle, &g_media_svc_item_validity_query_list,uid);
705 else if (query_type == MEDIA_SVC_QUERY_MOVE_ITEM)
706 ret = _media_svc_sql_query_list(handle, &g_media_svc_move_item_query_list, uid);
707 else if (query_type == MEDIA_SVC_QUERY_INSERT_ITEM)
708 ret = _media_svc_sql_query_list(handle, &g_media_svc_insert_item_query_list, uid);
710 ret = MEDIA_INFO_ERROR_INVALID_PARAMETER;
712 if (ret != MEDIA_INFO_ERROR_NONE) {
713 media_svc_error("_media_svc_list_query_do failed. start rollback");
714 _media_svc_sql_rollback_trans(handle,uid);
718 ret = _media_svc_sql_end_trans(handle, uid);
719 if (ret != MEDIA_INFO_ERROR_NONE) {
720 media_svc_error("mb_svc_sqlite3_commit_trans failed.. Now start to rollback\n");
721 _media_svc_sql_rollback_trans(handle,uid);
725 return MEDIA_INFO_ERROR_NONE;
728 int _media_svc_get_media_id_by_path(sqlite3 *handle, const char *path, char *media_uuid, int max_length)
730 int ret = MEDIA_INFO_ERROR_NONE;
731 sqlite3_stmt *sql_stmt = NULL;
732 char *sql = sqlite3_mprintf("SELECT media_uuid FROM %s WHERE validity=1 AND path='%q'",
733 MEDIA_SVC_DB_TABLE_MEDIA, path);
735 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
737 if (ret != MEDIA_INFO_ERROR_NONE) {
738 media_svc_error("error when __media_svc_count_invalid_records_with_thumbnail. err = [%d]", ret);
742 strncpy(media_uuid, (const char*)sqlite3_column_text(sql_stmt, 0), max_length);
743 media_uuid[max_length - 1] = '\0';
745 SQLITE3_FINALIZE(sql_stmt);
747 return MEDIA_INFO_ERROR_NONE;
750 int _media_svc_get_burst_id(sqlite3 *handle, int *id)
752 int ret = MEDIA_INFO_ERROR_NONE;
754 sqlite3_stmt *sql_stmt = NULL;
755 char *sql = sqlite3_mprintf("SELECT max(CAST(burst_id AS INTEGER)) FROM %s", MEDIA_SVC_DB_TABLE_MEDIA);
757 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
759 if (ret != MEDIA_INFO_ERROR_NONE) {
760 media_svc_error("error when _media_svc_get_burst_id. err = [%d]", ret);
764 cur_id = sqlite3_column_int(sql_stmt, 0);
766 SQLITE3_FINALIZE(sql_stmt);
768 return MEDIA_INFO_ERROR_NONE;
771 int _media_svc_get_noti_info(sqlite3 *handle, const char *path, int update_item, media_svc_noti_item **item)
773 int ret = MEDIA_INFO_ERROR_NONE;
774 sqlite3_stmt *sql_stmt = NULL;
776 int is_root_dir = FALSE;
779 media_svc_error("_media_svc_get_noti_info failed");
780 return MEDIA_INFO_ERROR_INVALID_PARAMETER;
783 if (update_item == MS_MEDIA_ITEM_FILE) {
784 sql = sqlite3_mprintf("SELECT media_uuid, media_type, mime_type FROM %s WHERE path=%Q", MEDIA_SVC_DB_TABLE_MEDIA, path);
785 } else if (update_item == MS_MEDIA_ITEM_DIRECTORY) {
786 sql = sqlite3_mprintf("SELECT folder_uuid FROM %s WHERE path=%Q", MEDIA_SVC_DB_TABLE_FOLDER, path);
788 media_svc_error("_media_svc_get_noti_info failed : update item");
789 return MEDIA_INFO_ERROR_INVALID_PARAMETER;
792 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
794 if (ret != MEDIA_INFO_ERROR_NONE) {
795 if (ret == MEDIA_INFO_ERROR_DATABASE_NO_RECORD && update_item == MS_MEDIA_ITEM_DIRECTORY) {
796 media_svc_error("This is root directory of media");
800 media_svc_error("error when _media_svc_get_noti_info. err = [%d]", ret);
805 *item = calloc(1, sizeof(media_svc_noti_item));
807 media_svc_error("_media_svc_get_noti_info failed : calloc");
808 return MEDIA_INFO_ERROR_OUT_OF_MEMORY;
811 if (update_item == MS_MEDIA_ITEM_FILE) {
812 if (sqlite3_column_text(sql_stmt, 0))
813 (*item)->media_uuid = strdup((const char *)sqlite3_column_text(sql_stmt, 0));
815 (*item)->media_type = sqlite3_column_int(sql_stmt, 1);
817 if (sqlite3_column_text(sql_stmt, 2))
818 (*item)->mime_type = strdup((const char *)sqlite3_column_text(sql_stmt, 2));
819 } else if (update_item == MS_MEDIA_ITEM_DIRECTORY) {
821 (*item)->media_uuid = NULL;
823 if (sqlite3_column_text(sql_stmt, 0))
824 (*item)->media_uuid = strdup((const char *)sqlite3_column_text(sql_stmt, 0));
828 SQLITE3_FINALIZE(sql_stmt);
830 return MEDIA_INFO_ERROR_NONE;
833 int _media_svc_count_invalid_folder_items(sqlite3 *handle, const char *folder_path, int *count)
835 int ret = MEDIA_INFO_ERROR_NONE;
836 sqlite3_stmt *sql_stmt = NULL;
837 char *sql = sqlite3_mprintf("SELECT count(*) FROM %s WHERE validity=0 AND path LIKE '%q/%%'",
838 MEDIA_SVC_DB_TABLE_MEDIA, folder_path);
840 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
842 if (ret != MEDIA_INFO_ERROR_NONE) {
843 media_svc_error("error when __media_svc_count_invalid_folder_records_with_thumbnail. err = [%d]", ret);
847 *count = sqlite3_column_int(sql_stmt, 0);
849 SQLITE3_FINALIZE(sql_stmt);
851 return MEDIA_INFO_ERROR_NONE;
854 int _media_svc_get_thumbnail_count(sqlite3 *handle, const char *thumb_path, int *count)
856 int ret = MEDIA_INFO_ERROR_NONE;
857 sqlite3_stmt *sql_stmt = NULL;
858 char *sql = sqlite3_mprintf("SELECT count(*) FROM %s WHERE thumbnail_path=%Q", MEDIA_SVC_DB_TABLE_MEDIA, thumb_path);
860 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
862 if (ret != MEDIA_INFO_ERROR_NONE) {
863 media_svc_error("error when _media_svc_get_thumbnail_count. err = [%d]", ret);
867 *count = sqlite3_column_int(sql_stmt, 0);
869 SQLITE3_FINALIZE(sql_stmt);
871 return MEDIA_INFO_ERROR_NONE;