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