Tizen 2.0 Release
[platform/core/multimedia/libmedia-service.git] / src / common / media-svc-media.c
1 /*
2  * libmedia-service
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Hyunjun Ko <zzoon.ko@samsung.com>, Haejeong Kim <backto.kim@samsung.com>
7  *
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  */
21
22 #include <string.h>
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
30 typedef struct{
31         char thumbnail_path[MEDIA_SVC_PATHNAME_SIZE];
32 }media_svc_thumbnailpath_s;
33
34 static __thread GList *g_media_svc_item_validity_query_list = NULL;
35 static __thread GList *g_media_svc_insert_item_query_list = NULL;
36 __thread GList *g_media_svc_move_item_query_list = NULL;
37
38 static int __media_svc_count_invalid_records_with_thumbnail(sqlite3 *handle, media_svc_storage_type_e storage_type, int *count);
39 static int __media_svc_get_invalid_records_with_thumbnail(sqlite3 *handle, media_svc_storage_type_e storage_type,
40                                                         int count, media_svc_thumbnailpath_s * thumb_path);
41 static int __media_svc_count_invalid_folder_records_with_thumbnail(sqlite3 *handle, const char *folder_path, int *count);
42 static int __media_svc_get_invalid_folder_records_with_thumbnail(sqlite3 *handle, const char *folder_path,
43                                                         int count, media_svc_thumbnailpath_s * thumb_path);
44
45 static int __media_svc_count_invalid_records_with_thumbnail(sqlite3 *handle, media_svc_storage_type_e storage_type, int *count)
46 {
47         int ret = MEDIA_INFO_ERROR_NONE;
48         sqlite3_stmt *sql_stmt = NULL;
49         char *sql = sqlite3_mprintf("SELECT count(*) FROM %s WHERE validity=0 AND storage_type=%d AND thumbnail_path IS NOT NULL",
50                                         MEDIA_SVC_DB_TABLE_MEDIA, storage_type);
51
52         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
53
54         if (ret != MEDIA_INFO_ERROR_NONE) {
55                 media_svc_error("error when __media_svc_count_invalid_records_with_thumbnail. err = [%d]", ret);
56                 return ret;
57         }
58
59         *count = sqlite3_column_int(sql_stmt, 0);
60
61         SQLITE3_FINALIZE(sql_stmt);
62
63         return MEDIA_INFO_ERROR_NONE;
64
65 }
66
67 static int __media_svc_get_invalid_records_with_thumbnail(sqlite3 *handle, media_svc_storage_type_e storage_type,
68                                                         int count, media_svc_thumbnailpath_s * thumb_path)
69 {
70         int err = -1;
71         int idx = 0;
72         sqlite3_stmt *sql_stmt = NULL;
73
74         char *sql = sqlite3_mprintf("select thumbnail_path from %s WHERE validity=0 AND storage_type=%d AND thumbnail_path IS NOT NULL",
75                                         MEDIA_SVC_DB_TABLE_MEDIA, storage_type);
76
77         media_svc_debug("[SQL query] : %s", sql);
78
79         err = sqlite3_prepare_v2(handle, sql, -1, &sql_stmt, NULL);
80         sqlite3_free(sql);
81         if (err != SQLITE_OK) {
82                 media_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
83                 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
84         }
85
86         while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
87                 _strncpy_safe(thumb_path[idx].thumbnail_path, (const char *)sqlite3_column_text(sql_stmt, 0), sizeof(thumb_path[idx]));
88                 //media_svc_debug("thumb_path[%d]=[%s]", idx, thumb_path[idx].thumbnail_path);
89                 idx++;
90         }
91
92         SQLITE3_FINALIZE(sql_stmt);
93
94         return MEDIA_INFO_ERROR_NONE;
95 }
96
97 static int __media_svc_count_invalid_folder_records_with_thumbnail(sqlite3 *handle, const char *folder_path, int *count)
98 {
99         int ret = MEDIA_INFO_ERROR_NONE;
100         sqlite3_stmt *sql_stmt = NULL;
101         char *sql = sqlite3_mprintf("SELECT count(*) FROM %s WHERE validity=0 AND path LIKE '%q/%%' AND thumbnail_path IS NOT NULL",
102                                         MEDIA_SVC_DB_TABLE_MEDIA, folder_path);
103
104         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
105
106         if (ret != MEDIA_INFO_ERROR_NONE) {
107                 media_svc_error("error when __media_svc_count_invalid_folder_records_with_thumbnail. err = [%d]", ret);
108                 return ret;
109         }
110
111         *count = sqlite3_column_int(sql_stmt, 0);
112
113         SQLITE3_FINALIZE(sql_stmt);
114
115         return MEDIA_INFO_ERROR_NONE;
116
117 }
118
119 static int __media_svc_get_invalid_folder_records_with_thumbnail(sqlite3 *handle, const char *folder_path,
120                                                         int count, media_svc_thumbnailpath_s * thumb_path)
121 {
122         int err = -1;
123         int idx = 0;
124         sqlite3_stmt *sql_stmt = NULL;
125
126         char *sql = sqlite3_mprintf("select thumbnail_path from %s WHERE validity=0 AND path LIKE '%q/%%' AND thumbnail_path IS NOT NULL",
127                                         MEDIA_SVC_DB_TABLE_MEDIA, folder_path);
128
129         media_svc_debug("[SQL query] : %s", sql);
130
131         err = sqlite3_prepare_v2(handle, sql, -1, &sql_stmt, NULL);
132         sqlite3_free(sql);
133         if (err != SQLITE_OK) {
134                 media_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
135                 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
136         }
137
138         while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
139                 _strncpy_safe(thumb_path[idx].thumbnail_path, (const char *)sqlite3_column_text(sql_stmt, 0), sizeof(thumb_path[idx]));
140                 idx++;
141         }
142
143         SQLITE3_FINALIZE(sql_stmt);
144
145         return MEDIA_INFO_ERROR_NONE;
146 }
147
148 int _media_svc_count_record_with_path(sqlite3 *handle, const char *path, int *count)
149 {
150         int ret = MEDIA_INFO_ERROR_NONE;
151         sqlite3_stmt *sql_stmt = NULL;
152
153         char *sql = sqlite3_mprintf("SELECT count(*) FROM %s WHERE path='%q'", MEDIA_SVC_DB_TABLE_MEDIA, path);
154
155         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
156
157         media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
158
159         *count = sqlite3_column_int(sql_stmt, 0);
160
161         SQLITE3_FINALIZE(sql_stmt);
162
163         return MEDIA_INFO_ERROR_NONE;
164 }
165
166 int _media_svc_insert_item_with_data(sqlite3 *handle, media_svc_content_info_s *content_info, bool stack_query)
167 {
168         media_svc_debug("");
169         int err = -1;
170
171         char * db_fields = "media_uuid, path, file_name, media_type, mime_type, size, added_time, modified_time, folder_uuid, \
172                                         thumbnail_path, title, album_id, album, artist, genre, composer, year, recorded_date, copyright, track_num, description,\
173                                         bitrate, samplerate, channel, duration, longitude, latitude, altitude, width, height, datetaken, orientation,\
174                                         rating, is_drm, storage_type";
175
176         /* This sql is due to sqlite3_mprintf's wrong operation when using floating point in the text format */
177         /* This code will be removed when sqlite3_mprintf works clearly */
178         char *test_sql = sqlite3_mprintf("%f, %f, %f", content_info->media_meta.longitude, content_info->media_meta.latitude, content_info->media_meta.altitude);
179         sqlite3_free(test_sql);
180
181         char *sql = sqlite3_mprintf("INSERT INTO %s (%s) VALUES (%Q, %Q, %Q, %d, %Q, %lld, %d, %d, %Q, \
182                                                                                                         %Q, %Q, %d, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, \
183                                                                                                         %d, %d, %d, %d, %.2f, %.2f, %.2f, %d, %d, %Q, %d, \
184                                                                                                         %d, %d, %d);",
185                 MEDIA_SVC_DB_TABLE_MEDIA, db_fields,
186                 content_info->media_uuid,
187                 content_info->path,
188                 content_info->file_name,
189                 content_info->media_type,
190                 content_info->mime_type,
191                 content_info->size,
192                 content_info->added_time,
193                 content_info->modified_time,
194                 content_info->folder_uuid,
195                 content_info->thumbnail_path,           //
196                 content_info->media_meta.title,
197                 content_info->album_id,
198                 content_info->media_meta.album,
199                 content_info->media_meta.artist,
200                 content_info->media_meta.genre,
201                 content_info->media_meta.composer,
202                 content_info->media_meta.year,
203                 content_info->media_meta.recorded_date,
204                 content_info->media_meta.copyright,
205                 content_info->media_meta.track_num,
206                 content_info->media_meta.description,   //
207                 content_info->media_meta.bitrate,
208                 content_info->media_meta.samplerate,
209                 content_info->media_meta.channel,
210                 content_info->media_meta.duration,
211                 content_info->media_meta.longitude,
212                 content_info->media_meta.latitude,
213                 content_info->media_meta.altitude,
214                 content_info->media_meta.width,
215                 content_info->media_meta.height,
216                 content_info->media_meta.datetaken,
217                 content_info->media_meta.orientation,
218                 content_info->media_meta.rating,
219                 content_info->is_drm,
220                 content_info->storage_type);
221
222         if(!stack_query) {
223                 err = _media_svc_sql_query(handle, sql);
224                 sqlite3_free(sql);
225                 if (err != SQLITE_OK) {
226                         media_svc_error("failed to insert item");
227
228                         return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
229                 }
230         } else {
231                 media_svc_debug("query : %s", sql);
232                 _media_svc_sql_query_add(&g_media_svc_insert_item_query_list, &sql);
233         }
234
235         return MEDIA_INFO_ERROR_NONE;
236 }
237
238 int _media_svc_update_item_with_data(sqlite3 *handle, media_svc_content_info_s *content_info)
239 {
240         int err = -1;
241
242         /* This sql is due to sqlite3_mprintf's wrong operation when using floating point in the text format */
243         /* This code will be removed when sqlite3_mprintf works clearly */
244         char *test_sql = sqlite3_mprintf("%f, %f, %f", content_info->media_meta.longitude, content_info->media_meta.latitude, content_info->media_meta.altitude);
245         sqlite3_free(test_sql);
246
247         char *sql = sqlite3_mprintf("UPDATE %s SET \
248                 size=%lld, modified_time=%d, thumbnail_path=%Q, title=%Q, album_id=%d, album=%Q, artist=%Q, genre=%Q, \
249                 composer=%Q, year=%Q, recorded_date=%Q, copyright=%Q, track_num=%Q, description=%Q, \
250                 bitrate=%d, samplerate=%d, channel=%d, duration=%d, longitude=%f, latitude=%f, altitude=%f, width=%d, height=%d, datetaken=%Q, \
251                                                                                                         orientation=%d WHERE path=%Q",
252                 MEDIA_SVC_DB_TABLE_MEDIA,
253                 content_info->size,
254                 content_info->modified_time,
255                 content_info->thumbnail_path,
256                 content_info->media_meta.title,
257                 content_info->album_id,
258                 content_info->media_meta.album,
259                 content_info->media_meta.artist,
260                 content_info->media_meta.genre,
261                 content_info->media_meta.composer,
262                 content_info->media_meta.year,
263                 content_info->media_meta.recorded_date,
264                 content_info->media_meta.copyright,
265                 content_info->media_meta.track_num,
266                 content_info->media_meta.description,
267                 content_info->media_meta.bitrate,
268                 content_info->media_meta.samplerate,
269                 content_info->media_meta.channel,
270                 content_info->media_meta.duration,
271                 content_info->media_meta.longitude,
272                 content_info->media_meta.latitude,
273                 content_info->media_meta.altitude,
274                 content_info->media_meta.width,
275                 content_info->media_meta.height,
276                 content_info->media_meta.datetaken,
277                 content_info->media_meta.orientation,
278                 content_info->path
279                 );
280
281         err = _media_svc_sql_query(handle, sql);
282         sqlite3_free(sql);
283         if (err != SQLITE_OK) {
284                 media_svc_error("failed to update item");
285
286                 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
287         }
288
289         return MEDIA_INFO_ERROR_NONE;
290 }
291 int _media_svc_get_thumbnail_path_by_path(sqlite3 *handle, const char *path, char *thumbnail_path)
292 {
293         int ret = MEDIA_INFO_ERROR_NONE;
294         sqlite3_stmt *sql_stmt = NULL;
295
296         char *sql = sqlite3_mprintf("SELECT thumbnail_path FROM %s WHERE path='%q'", MEDIA_SVC_DB_TABLE_MEDIA, path);
297
298         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
299
300         if (ret != MEDIA_INFO_ERROR_NONE) {
301                 if(ret == MEDIA_INFO_ERROR_DATABASE_NO_RECORD) {
302                         media_svc_debug("there is no thumbnail.");
303                 }
304                 else {
305                         media_svc_error("error when _media_svc_get_thumbnail_path_by_path. err = [%d]", ret);
306                 }
307                 return ret;
308         }
309
310         _strncpy_safe(thumbnail_path, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_PATHNAME_SIZE);
311
312         SQLITE3_FINALIZE(sql_stmt);
313
314         return MEDIA_INFO_ERROR_NONE;
315 }
316
317 int _media_svc_get_media_type_by_path(sqlite3 *handle, const char *path, int *media_type)
318 {
319         int ret = MEDIA_INFO_ERROR_NONE;
320         sqlite3_stmt *sql_stmt = NULL;
321
322         char *sql = sqlite3_mprintf("SELECT media_type FROM %s WHERE path='%q'", MEDIA_SVC_DB_TABLE_MEDIA, path);
323
324         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
325
326         if (ret != MEDIA_INFO_ERROR_NONE) {
327                 media_svc_error("error when _media_svc_get_media_type_by_path. err = [%d]", ret);
328                 return ret;
329         }
330
331         *media_type = sqlite3_column_int(sql_stmt, 0);
332
333         SQLITE3_FINALIZE(sql_stmt);
334
335         return MEDIA_INFO_ERROR_NONE;
336 }
337
338 int _media_svc_delete_item_by_path(sqlite3 *handle, const char *path)
339 {
340         int err = -1;
341         char *sql = sqlite3_mprintf("DELETE FROM %s WHERE validity=1 AND path='%q'", MEDIA_SVC_DB_TABLE_MEDIA, path);
342
343         err = _media_svc_sql_query(handle, sql);
344         sqlite3_free(sql);
345         if (err != SQLITE_OK) {
346                 media_svc_error("It failed to delete item (%d)", err);
347                 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
348         }
349
350         return MEDIA_INFO_ERROR_NONE;
351 }
352
353 int _media_svc_truncate_table(sqlite3 *handle, media_svc_storage_type_e storage_type)
354 {
355         int err = -1;
356         char *sql = sqlite3_mprintf("DELETE FROM %s WHERE storage_type=%d", MEDIA_SVC_DB_TABLE_MEDIA, storage_type);
357
358         err = _media_svc_sql_query(handle, sql);
359         sqlite3_free(sql);
360         if (err != SQLITE_OK) {
361                 media_svc_error("It failed to truncate table (%d)", err);
362                 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
363         }
364
365         return MEDIA_INFO_ERROR_NONE;
366
367 }
368
369 int _media_svc_delete_invalid_items(sqlite3 *handle, media_svc_storage_type_e storage_type)
370 {
371         int idx = 0;
372         media_svc_thumbnailpath_s *thumbpath_record = NULL;
373         int err = -1;
374         int invalid_count = 0;
375         int ret = MEDIA_INFO_ERROR_NONE;
376
377         ret = __media_svc_count_invalid_records_with_thumbnail(handle, storage_type, &invalid_count);
378         media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
379
380         media_svc_debug("invalid count: %d\n", invalid_count);
381
382         if (invalid_count > 0) {
383                 thumbpath_record = (media_svc_thumbnailpath_s *)calloc( invalid_count, sizeof(media_svc_thumbnailpath_s));
384                 if (thumbpath_record == NULL) {
385                         media_svc_error("fail to memory allocation");
386                         return MEDIA_INFO_ERROR_OUT_OF_MEMORY;
387                 }
388
389                 ret = __media_svc_get_invalid_records_with_thumbnail(handle, storage_type, invalid_count, thumbpath_record);
390                 if (ret != MEDIA_INFO_ERROR_NONE) {
391                         media_svc_error("error when get thumbnail record");
392                         SAFE_FREE(thumbpath_record);
393                         return ret;
394                 }
395         } else {
396                 media_svc_debug("There is no item with thumbnail");
397         }
398
399         char *sql = sqlite3_mprintf("DELETE FROM %s WHERE validity = 0 AND storage_type=%d", MEDIA_SVC_DB_TABLE_MEDIA, storage_type);
400         err = _media_svc_sql_query(handle, sql);
401         sqlite3_free(sql);
402         if (err != SQLITE_OK) {
403                 media_svc_error("To delete invalid items is failed(%d)", err);
404                 SAFE_FREE(thumbpath_record);
405                 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
406         }
407
408         /*Delete thumbnails*/
409         for (idx = 0; idx < invalid_count; idx++) {
410                 if (strlen(thumbpath_record[idx].thumbnail_path) > 0) {
411                         if (_media_svc_remove_file(thumbpath_record[idx].thumbnail_path) == FALSE) {
412                                 media_svc_error("fail to remove thumbnail file.");
413                                 //SAFE_FREE(thumbpath_record);
414                                 //return MEDIA_INFO_ERROR_INTERNAL;
415                         }
416                 }
417         }
418
419         SAFE_FREE(thumbpath_record);
420
421         return MEDIA_INFO_ERROR_NONE;
422 }
423
424 int _media_svc_delete_invalid_folder_items(sqlite3 *handle, const char *folder_path)
425 {
426         int idx = 0;
427         media_svc_thumbnailpath_s *thumbpath_record = NULL;
428         int err = -1;
429         int invalid_count = 0;
430         int ret = MEDIA_INFO_ERROR_NONE;
431
432         ret = __media_svc_count_invalid_folder_records_with_thumbnail(handle, folder_path, &invalid_count);
433         media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
434
435         media_svc_debug("invalid count: %d\n", invalid_count);
436
437         if (invalid_count > 0) {
438                 thumbpath_record = (media_svc_thumbnailpath_s *)calloc( invalid_count, sizeof(media_svc_thumbnailpath_s));
439                 if (thumbpath_record == NULL) {
440                         media_svc_error("fail to memory allocation");
441                         return MEDIA_INFO_ERROR_OUT_OF_MEMORY;
442                 }
443
444                 ret = __media_svc_get_invalid_folder_records_with_thumbnail(handle, folder_path, invalid_count, thumbpath_record);
445                 if (ret != MEDIA_INFO_ERROR_NONE) {
446                         media_svc_error("error when get thumbnail record");
447                         SAFE_FREE(thumbpath_record);
448                         return ret;
449                 }
450         } else {
451                 media_svc_debug("There is no item with thumbnail");
452         }
453
454         char *sql = sqlite3_mprintf("DELETE FROM %s WHERE validity = 0 AND path LIKE '%q/%%'", MEDIA_SVC_DB_TABLE_MEDIA, folder_path);
455         err = _media_svc_sql_query(handle, sql);
456         sqlite3_free(sql);
457         if (err != SQLITE_OK) {
458                 media_svc_error("To delete invalid items is failed(%d)", err);
459                 SAFE_FREE(thumbpath_record);
460                 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
461         }
462
463         /*Delete thumbnails*/
464         for (idx = 0; idx < invalid_count; idx++) {
465                 if (strlen(thumbpath_record[idx].thumbnail_path) > 0) {
466                         if (_media_svc_remove_file(thumbpath_record[idx].thumbnail_path) == FALSE) {
467                                 media_svc_error("fail to remove thumbnail file [%s].", thumbpath_record[idx].thumbnail_path);
468                                 //SAFE_FREE(thumbpath_record);
469                                 //return MEDIA_INFO_ERROR_INTERNAL;
470                         }
471                 }
472         }
473
474         SAFE_FREE(thumbpath_record);
475
476         return MEDIA_INFO_ERROR_NONE;
477 }
478
479
480 int _media_svc_update_item_validity(sqlite3 *handle, const char *path, int validity, bool stack_query)
481 {
482         int err = -1;
483
484         char *sql = sqlite3_mprintf("UPDATE %s SET validity=%d WHERE path= '%q'", MEDIA_SVC_DB_TABLE_MEDIA, validity, path);
485
486         if(!stack_query) {
487                 err = _media_svc_sql_query(handle, sql);
488                 sqlite3_free(sql);
489                 if (err != SQLITE_OK) {
490                         media_svc_error("To update item as valid is failed(%d)", err);
491                         return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
492                 }
493         } else {
494                 _media_svc_sql_query_add(&g_media_svc_item_validity_query_list, &sql);
495         }
496
497         return MEDIA_INFO_ERROR_NONE;
498 }
499
500 int _media_svc_update_thumbnail_path(sqlite3 *handle, const char *path, const char *thumb_path)
501 {
502         int err = -1;
503
504         char *sql = sqlite3_mprintf("UPDATE %s SET thumbnail_path=%Q WHERE path= %Q", MEDIA_SVC_DB_TABLE_MEDIA, thumb_path, path);
505
506         err = _media_svc_sql_query(handle, sql);
507         sqlite3_free(sql);
508         if (err != SQLITE_OK) {
509                 media_svc_error("To update thumb path failed(%d)", err);
510                 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
511         }
512
513         return MEDIA_INFO_ERROR_NONE;
514 }
515
516 int _media_svc_update_storage_item_validity(sqlite3 *handle, media_svc_storage_type_e storage_type, int validity)
517 {
518         int err = -1;
519         char *sql = sqlite3_mprintf("UPDATE %s SET validity=%d WHERE storage_type=%d", MEDIA_SVC_DB_TABLE_MEDIA, validity, storage_type);
520         err = _media_svc_sql_query(handle, sql);
521         sqlite3_free(sql);
522         if (err != SQLITE_OK) {
523                 media_svc_error("To update item as valid is failed(%d)", err);
524                 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
525         }
526
527         return MEDIA_INFO_ERROR_NONE;
528 }
529
530 int _media_svc_update_folder_item_validity(sqlite3 *handle, const char *folder_path, int validity)
531 {
532         int err = -1;
533         int ret = MEDIA_INFO_ERROR_NONE;
534         char *sql = NULL;
535         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
536         sqlite3_stmt *sql_stmt = NULL;
537
538         /*Get folder ID*/
539         sql = sqlite3_mprintf("SELECT folder_uuid FROM %s WHERE path='%q'", MEDIA_SVC_DB_TABLE_FOLDER, folder_path);
540         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
541         if (ret != MEDIA_INFO_ERROR_NONE) {
542                 media_svc_error("error when get folder_id. err = [%d]", ret);
543                 return ret;
544         }
545
546         _strncpy_safe(folder_uuid, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE+1);
547         SQLITE3_FINALIZE(sql_stmt);
548
549         /*Update folder item validity*/
550         sql = sqlite3_mprintf("UPDATE %s SET validity=%d WHERE folder_uuid='%q'", MEDIA_SVC_DB_TABLE_MEDIA, validity, folder_uuid);
551         err = _media_svc_sql_query(handle, sql);
552         sqlite3_free(sql);
553         if (err != SQLITE_OK) {
554                 media_svc_error("To update folder item as valid is failed(%d)", err);
555                 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
556         }
557
558         return MEDIA_INFO_ERROR_NONE;
559 }
560
561 int _media_svc_update_recursive_folder_item_validity(sqlite3 *handle, const char *folder_path, int validity)
562 {
563         int err = -1;
564
565         /*Update folder item validity*/
566         char *sql = sqlite3_mprintf("UPDATE %s SET validity=%d WHERE path LIKE '%q/%%'", MEDIA_SVC_DB_TABLE_MEDIA, validity, folder_path);
567         err = _media_svc_sql_query(handle, sql);
568         sqlite3_free(sql);
569         if (err != SQLITE_OK) {
570                 media_svc_error("To update recursive folder item validity is failed(%d)", err);
571                 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
572         }
573
574         return MEDIA_INFO_ERROR_NONE;
575 }
576
577 int _media_svc_update_item_by_path(sqlite3 *handle, const char *src_path, media_svc_storage_type_e dest_storage, const char *dest_path,
578                                 const char *file_name, int modified_time, const char *folder_uuid, const char *thumb_path, bool stack_query)
579 {
580         /* update path, filename, modified_time, folder_uuid, thumbnail_path, */
581         /* played_count, last_played_time, last_played_position, favourite, storaget_type*/
582
583         int err = -1;
584         char *sql = NULL;
585
586         if(thumb_path != NULL) {
587                 sql = sqlite3_mprintf("UPDATE %s SET \
588                                         path=%Q, file_name=%Q, modified_time=%d, folder_uuid=%Q, thumbnail_path=%Q, storage_type=%d, \
589                                         played_count=0, last_played_time=0, last_played_position=0 \
590                                         WHERE path=%Q",
591                                         MEDIA_SVC_DB_TABLE_MEDIA, dest_path, file_name, modified_time, folder_uuid, thumb_path, dest_storage, src_path);
592         } else {
593                 sql = sqlite3_mprintf("UPDATE %s SET \
594                                         path=%Q, file_name=%Q, modified_time=%d, folder_uuid=%Q, storage_type=%d, \
595                                         played_count=0, last_played_time=0, last_played_position=0 \
596                                         WHERE path=%Q",
597                                         MEDIA_SVC_DB_TABLE_MEDIA, dest_path, file_name, modified_time, folder_uuid, dest_storage, src_path);
598         }
599
600         if(!stack_query) {
601                 err = _media_svc_sql_query(handle, sql);
602                 sqlite3_free(sql);
603                 if (err != SQLITE_OK) {
604                         media_svc_error("It failed to update metadata (%d)", err);
605                         return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
606                 }
607         } else {
608                 _media_svc_sql_query_add(&g_media_svc_move_item_query_list, &sql);
609         }
610
611         return MEDIA_INFO_ERROR_NONE;
612 }
613
614 int _media_svc_list_query_do(sqlite3 *handle, media_svc_query_type_e query_type)
615 {
616         int ret = MEDIA_INFO_ERROR_NONE;
617
618         ret = _media_svc_sql_begin_trans(handle);
619         media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
620
621         if (query_type == MEDIA_SVC_QUERY_SET_ITEM_VALIDITY)
622                 ret = _media_svc_sql_query_list(handle, &g_media_svc_item_validity_query_list);
623         else if (query_type == MEDIA_SVC_QUERY_MOVE_ITEM)
624                 ret = _media_svc_sql_query_list(handle, &g_media_svc_move_item_query_list);
625         else if (query_type == MEDIA_SVC_QUERY_INSERT_ITEM)
626                 ret = _media_svc_sql_query_list(handle, &g_media_svc_insert_item_query_list);
627         else
628                 ret = MEDIA_INFO_ERROR_INVALID_PARAMETER;
629
630         if (ret != MEDIA_INFO_ERROR_NONE) {
631                 media_svc_error("_media_svc_list_query_do failed. start rollback");
632                 _media_svc_sql_rollback_trans(handle);
633                 return ret;
634         }
635
636         ret = _media_svc_sql_end_trans(handle);
637         if (ret != MEDIA_INFO_ERROR_NONE) {
638                 media_svc_error("mb_svc_sqlite3_commit_trans failed.. Now start to rollback\n");
639                 _media_svc_sql_rollback_trans(handle);
640                 return ret;
641         }
642
643         return MEDIA_INFO_ERROR_NONE;
644 }
645
646 int _media_svc_get_media_id_by_path(sqlite3 *handle, const char *path, char *media_uuid, int max_length)
647 {
648         int ret = MEDIA_INFO_ERROR_NONE;
649         sqlite3_stmt *sql_stmt = NULL;
650         char *sql = sqlite3_mprintf("SELECT media_uuid FROM %s WHERE validity=1 AND path='%q'",
651                                         MEDIA_SVC_DB_TABLE_MEDIA, path);
652
653         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
654
655         if (ret != MEDIA_INFO_ERROR_NONE) {
656                 media_svc_error("error when __media_svc_count_invalid_records_with_thumbnail. err = [%d]", ret);
657                 return ret;
658         }
659
660         strncpy(media_uuid, (const char*)sqlite3_column_text(sql_stmt, 0), max_length);
661         media_uuid[max_length - 1] = '\0';
662
663         SQLITE3_FINALIZE(sql_stmt);
664
665         return MEDIA_INFO_ERROR_NONE;
666
667 }