2b14233e7c4fae0b9d370d8b21e826daf928b9b1
[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 <grp.h>
24 #include <pwd.h>
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"
32
33 #define GLOBAL_USER    0 //#define     tzplatform_getenv(TZ_GLOBAL) //TODO
34
35 typedef struct{
36         char thumbnail_path[MEDIA_SVC_PATHNAME_SIZE];
37 }media_svc_thumbnailpath_s;
38
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;
42
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);
49
50 static int __media_svc_count_invalid_records_with_thumbnail(sqlite3 *handle, media_svc_storage_type_e storage_type, int *count)
51 {
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);
56
57         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
58
59         if (ret != MEDIA_INFO_ERROR_NONE) {
60                 media_svc_error("error when __media_svc_count_invalid_records_with_thumbnail. err = [%d]", ret);
61                 return ret;
62         }
63
64         *count = sqlite3_column_int(sql_stmt, 0);
65
66         SQLITE3_FINALIZE(sql_stmt);
67
68         return MEDIA_INFO_ERROR_NONE;
69
70 }
71
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)
74 {
75         int err = -1;
76         int idx = 0;
77         sqlite3_stmt *sql_stmt = NULL;
78
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);
81
82         media_svc_debug("[SQL query] : %s", sql);
83
84         err = sqlite3_prepare_v2(handle, sql, -1, &sql_stmt, NULL);
85         sqlite3_free(sql);
86         if (err != SQLITE_OK) {
87                 media_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
88                 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
89         }
90
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);
94                 idx++;
95         }
96
97         SQLITE3_FINALIZE(sql_stmt);
98
99         return MEDIA_INFO_ERROR_NONE;
100 }
101
102 static int __media_svc_count_invalid_folder_records_with_thumbnail(sqlite3 *handle, const char *folder_path, int *count)
103 {
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);
108
109         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
110
111         if (ret != MEDIA_INFO_ERROR_NONE) {
112                 media_svc_error("error when __media_svc_count_invalid_folder_records_with_thumbnail. err = [%d]", ret);
113                 return ret;
114         }
115
116         *count = sqlite3_column_int(sql_stmt, 0);
117
118         SQLITE3_FINALIZE(sql_stmt);
119
120         return MEDIA_INFO_ERROR_NONE;
121
122 }
123
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)
126 {
127         int err = -1;
128         int idx = 0;
129         sqlite3_stmt *sql_stmt = NULL;
130
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);
133
134         media_svc_debug("[SQL query] : %s", sql);
135
136         err = sqlite3_prepare_v2(handle, sql, -1, &sql_stmt, NULL);
137         sqlite3_free(sql);
138         if (err != SQLITE_OK) {
139                 media_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
140                 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
141         }
142
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]));
145                 idx++;
146         }
147
148         SQLITE3_FINALIZE(sql_stmt);
149
150         return MEDIA_INFO_ERROR_NONE;
151 }
152
153 int _media_svc_count_record_with_path(sqlite3 *handle, const char *path, int *count)
154 {
155         int ret = MEDIA_INFO_ERROR_NONE;
156         sqlite3_stmt *sql_stmt = NULL;
157
158         char *sql = sqlite3_mprintf("SELECT count(*) FROM %s WHERE path='%q'", MEDIA_SVC_DB_TABLE_MEDIA, path);
159
160         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
161
162         media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
163
164         *count = sqlite3_column_int(sql_stmt, 0);
165
166         SQLITE3_FINALIZE(sql_stmt);
167
168         return MEDIA_INFO_ERROR_NONE;
169 }
170
171 char* _media_svc_get_thumb_default_path(uid_t uid)
172 {
173         char *result_psswd = NULL;
174         struct group *grpinfo = NULL;
175         char * dir = NULL;
176         if(uid == GLOBAL_USER)
177         {
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 !");
182                         return NULL;
183                 }
184         }
185         else
186         {
187                 struct passwd *userinfo = getpwuid(uid);
188                 if(userinfo == NULL) {
189                         media_svc_error("getpwuid(%d) returns NULL !", uid);
190                         return NULL;
191                 }
192                 grpinfo = getgrnam("users");
193                 if(grpinfo == NULL) {
194                         media_svc_error("getgrnam(users) returns NULL !");
195                         return NULL;
196                 }
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);
200                         return NULL;
201                 }
202                 asprintf(&result_psswd, "%s/data/file-manager-service/.thumb/thumb_default.png", userinfo->pw_dir);
203         }
204
205         return result_psswd;
206 }
207
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)
209 {
210         media_svc_debug("");
211         int err = -1;
212         char *burst_id = NULL;
213
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";
218
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);
223
224         if (is_burst) {
225                 int burst_id_int = 0;
226                 err = _media_svc_get_burst_id(handle, &burst_id_int);
227                 if (err < 0) {
228                         burst_id = NULL;
229                 }
230
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);
234                 }
235
236                 /* Get thumbnail for burst shot */
237                 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
238                 int width = 0;
239                 int height = 0;
240
241                 err = thumbnail_request_from_db_with_size(content_info->path, thumb_path, sizeof(thumb_path), &width, &height, uid);
242                 if (err < 0) {
243                         media_svc_error("thumbnail_request_from_db failed: %d", err);
244                 } else {
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);
247                         if (err < 0) {
248                                 content_info->thumbnail_path = NULL;
249                         }
250                 }
251
252                 if (content_info->media_meta.width <= 0)
253                         content_info->media_meta.width = width;
254
255                 if (content_info->media_meta.height <= 0)
256                         content_info->media_meta.height = height;
257         }
258
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, \
262                                                                                                         %d, %d, %d, %Q);",
263                 MEDIA_SVC_DB_TABLE_MEDIA, db_fields,
264                 content_info->media_uuid,
265                 content_info->path,
266                 content_info->file_name,
267                 content_info->media_type,
268                 content_info->mime_type,
269                 content_info->size,
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,
299                 burst_id);
300
301         if (burst_id) sqlite3_free(burst_id);
302         burst_id = NULL;
303
304         if(!stack_query) {
305                 err = _media_svc_sql_query(handle, sql, uid);
306                 sqlite3_free(sql);
307                 if (err != SQLITE_OK) {
308                         media_svc_error("failed to insert item");
309
310                         return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
311                 }
312         } else {
313                 media_svc_debug("query : %s", sql);
314                 _media_svc_sql_query_add(&g_media_svc_insert_item_query_list, &sql);
315         }
316
317         return MEDIA_INFO_ERROR_NONE;
318 }
319
320 int _media_svc_update_item_with_data(sqlite3 *handle, media_svc_content_info_s *content_info, uid_t uid)
321 {
322         int err = -1;
323
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);
328
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,
335                 content_info->size,
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,
360                 content_info->path
361                 );
362
363         err = _media_svc_sql_query(handle, sql, uid);
364         sqlite3_free(sql);
365         if (err != SQLITE_OK) {
366                 media_svc_error("failed to update item");
367
368                 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
369         }
370
371         return MEDIA_INFO_ERROR_NONE;
372 }
373 int _media_svc_get_thumbnail_path_by_path(sqlite3 *handle, const char *path, char *thumbnail_path)
374 {
375         int ret = MEDIA_INFO_ERROR_NONE;
376         sqlite3_stmt *sql_stmt = NULL;
377
378         char *sql = sqlite3_mprintf("SELECT thumbnail_path FROM %s WHERE path='%q'", MEDIA_SVC_DB_TABLE_MEDIA, path);
379
380         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
381
382         if (ret != MEDIA_INFO_ERROR_NONE) {
383                 if(ret == MEDIA_INFO_ERROR_DATABASE_NO_RECORD) {
384                         media_svc_debug("there is no thumbnail.");
385                 }
386                 else {
387                         media_svc_error("error when _media_svc_get_thumbnail_path_by_path. err = [%d]", ret);
388                 }
389                 return ret;
390         }
391
392         _strncpy_safe(thumbnail_path, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_PATHNAME_SIZE);
393
394         SQLITE3_FINALIZE(sql_stmt);
395
396         return MEDIA_INFO_ERROR_NONE;
397 }
398
399 int _media_svc_get_media_type_by_path(sqlite3 *handle, const char *path, int *media_type)
400 {
401         int ret = MEDIA_INFO_ERROR_NONE;
402         sqlite3_stmt *sql_stmt = NULL;
403
404         char *sql = sqlite3_mprintf("SELECT media_type FROM %s WHERE path='%q'", MEDIA_SVC_DB_TABLE_MEDIA, path);
405
406         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
407
408         if (ret != MEDIA_INFO_ERROR_NONE) {
409                 media_svc_error("error when _media_svc_get_media_type_by_path. err = [%d]", ret);
410                 return ret;
411         }
412
413         *media_type = sqlite3_column_int(sql_stmt, 0);
414
415         SQLITE3_FINALIZE(sql_stmt);
416
417         return MEDIA_INFO_ERROR_NONE;
418 }
419
420 int _media_svc_delete_item_by_path(sqlite3 *handle, const char *path, uid_t uid)
421 {
422         int err = -1;
423         char *sql = sqlite3_mprintf("DELETE FROM %s WHERE validity=1 AND path='%q'", MEDIA_SVC_DB_TABLE_MEDIA, path);
424
425         err = _media_svc_sql_query(handle, sql, uid);
426         sqlite3_free(sql);
427         if (err != SQLITE_OK) {
428                 media_svc_error("It failed to delete item (%d)", err);
429                 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
430         }
431
432         return MEDIA_INFO_ERROR_NONE;
433 }
434
435 int _media_svc_truncate_table(sqlite3 *handle, media_svc_storage_type_e storage_type, uid_t uid)
436 {
437         int err = -1;
438         char *sql = sqlite3_mprintf("DELETE FROM %s WHERE storage_type=%d", MEDIA_SVC_DB_TABLE_MEDIA, storage_type);
439
440         err = _media_svc_sql_query(handle, sql, uid);
441         sqlite3_free(sql);
442         if (err != SQLITE_OK) {
443                 media_svc_error("It failed to truncate table (%d)", err);
444                 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
445         }
446
447         return MEDIA_INFO_ERROR_NONE;
448
449 }
450
451 int _media_svc_delete_invalid_items(sqlite3 *handle, media_svc_storage_type_e storage_type, uid_t uid)
452 {
453         int idx = 0;
454         media_svc_thumbnailpath_s *thumbpath_record = NULL;
455         int err = -1;
456         int invalid_count = 0;
457         int ret = MEDIA_INFO_ERROR_NONE;
458
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);
461
462         media_svc_debug("invalid count: %d\n", invalid_count);
463
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;
469                 }
470
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);
475                         return ret;
476                 }
477         } else {
478                 media_svc_debug("There is no item with thumbnail");
479         }
480
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);
483         sqlite3_free(sql);
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;
488         }
489
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;
497                         }
498                 }
499         }
500
501         SAFE_FREE(thumbpath_record);
502
503         return MEDIA_INFO_ERROR_NONE;
504 }
505
506 int _media_svc_delete_invalid_folder_items(sqlite3 *handle, const char *folder_path, uid_t uid)
507 {
508         int idx = 0;
509         media_svc_thumbnailpath_s *thumbpath_record = NULL;
510         int err = -1;
511         int invalid_count = 0;
512         int ret = MEDIA_INFO_ERROR_NONE;
513
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);
516
517         media_svc_debug("invalid count: %d\n", invalid_count);
518
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;
524                 }
525
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);
530                         return ret;
531                 }
532         } else {
533                 media_svc_debug("There is no item with thumbnail");
534         }
535
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);
538         sqlite3_free(sql);
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;
543         }
544
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;
552                         }
553                 }
554         }
555
556         SAFE_FREE(thumbpath_record);
557
558         return MEDIA_INFO_ERROR_NONE;
559 }
560
561
562 int _media_svc_update_item_validity(sqlite3 *handle, const char *path, int validity, bool stack_query, uid_t uid)
563 {
564         int err = -1;
565
566         char *sql = sqlite3_mprintf("UPDATE %s SET validity=%d WHERE path= '%q'", MEDIA_SVC_DB_TABLE_MEDIA, validity, path);
567
568         if(!stack_query) {
569                 err = _media_svc_sql_query(handle, sql, uid);
570                 sqlite3_free(sql);
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;
574                 }
575         } else {
576                 _media_svc_sql_query_add(&g_media_svc_item_validity_query_list, &sql);
577         }
578
579         return MEDIA_INFO_ERROR_NONE;
580 }
581
582 int _media_svc_update_thumbnail_path(sqlite3 *handle, const char *path, const char *thumb_path, uid_t uid)
583 {
584         int err = -1;
585
586         char *sql = sqlite3_mprintf("UPDATE %s SET thumbnail_path=%Q WHERE path= %Q", MEDIA_SVC_DB_TABLE_MEDIA, thumb_path, path);
587
588         err = _media_svc_sql_query(handle, sql, uid);
589         sqlite3_free(sql);
590         if (err != SQLITE_OK) {
591                 media_svc_error("To update thumb path failed(%d)", err);
592                 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
593         }
594
595         return MEDIA_INFO_ERROR_NONE;
596 }
597
598 int _media_svc_update_storage_item_validity(sqlite3 *handle, media_svc_storage_type_e storage_type, int validity, uid_t uid)
599 {
600         int err = -1;
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);
603         sqlite3_free(sql);
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;
607         }
608
609         return MEDIA_INFO_ERROR_NONE;
610 }
611
612 int _media_svc_update_folder_item_validity(sqlite3 *handle, const char *folder_path, int validity, uid_t uid)
613 {
614         int err = -1;
615         int ret = MEDIA_INFO_ERROR_NONE;
616         char *sql = NULL;
617         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
618         sqlite3_stmt *sql_stmt = NULL;
619
620         /*Get folder ID*/
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);
625                 return ret;
626         }
627
628         _strncpy_safe(folder_uuid, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE+1);
629         SQLITE3_FINALIZE(sql_stmt);
630
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);
634         sqlite3_free(sql);
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;
638         }
639
640         return MEDIA_INFO_ERROR_NONE;
641 }
642
643 int _media_svc_update_recursive_folder_item_validity(sqlite3 *handle, const char *folder_path, int validity, uid_t uid)
644 {
645         int err = -1;
646
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);
650         sqlite3_free(sql);
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;
654         }
655
656         return MEDIA_INFO_ERROR_NONE;
657 }
658
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)
661 {
662         /* update path, filename, modified_time, folder_uuid, thumbnail_path, */
663         /* played_count, last_played_time, last_played_position, favourite, storaget_type*/
664
665         int err = -1;
666         char *sql = NULL;
667
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 \
672                                         WHERE path=%Q",
673                                         MEDIA_SVC_DB_TABLE_MEDIA, dest_path, file_name, modified_time, folder_uuid, thumb_path, dest_storage, src_path);
674         } else {
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 \
678                                         WHERE path=%Q",
679                                         MEDIA_SVC_DB_TABLE_MEDIA, dest_path, file_name, modified_time, folder_uuid, dest_storage, src_path);
680         }
681
682         if(!stack_query) {
683                 err = _media_svc_sql_query(handle, sql, uid);
684                 sqlite3_free(sql);
685                 if (err != SQLITE_OK) {
686                         media_svc_error("It failed to update metadata (%d)", err);
687                         return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
688                 }
689         } else {
690                 _media_svc_sql_query_add(&g_media_svc_move_item_query_list, &sql);
691         }
692
693         return MEDIA_INFO_ERROR_NONE;
694 }
695
696 int _media_svc_list_query_do(sqlite3 *handle, media_svc_query_type_e query_type, uid_t uid)
697 {
698         int ret = MEDIA_INFO_ERROR_NONE;
699
700         ret = _media_svc_sql_begin_trans(handle, uid);
701         media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
702
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);
709         else
710                 ret = MEDIA_INFO_ERROR_INVALID_PARAMETER;
711
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);
715                 return ret;
716         }
717
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);
722                 return ret;
723         }
724
725         return MEDIA_INFO_ERROR_NONE;
726 }
727
728 int _media_svc_get_media_id_by_path(sqlite3 *handle, const char *path, char *media_uuid, int max_length)
729 {
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);
734
735         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
736
737         if (ret != MEDIA_INFO_ERROR_NONE) {
738                 media_svc_error("error when __media_svc_count_invalid_records_with_thumbnail. err = [%d]", ret);
739                 return ret;
740         }
741
742         strncpy(media_uuid, (const char*)sqlite3_column_text(sql_stmt, 0), max_length);
743         media_uuid[max_length - 1] = '\0';
744
745         SQLITE3_FINALIZE(sql_stmt);
746
747         return MEDIA_INFO_ERROR_NONE;
748 }
749
750 int _media_svc_get_burst_id(sqlite3 *handle, int *id)
751 {
752         int ret = MEDIA_INFO_ERROR_NONE;
753         int cur_id = -1;
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);
756
757         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
758
759         if (ret != MEDIA_INFO_ERROR_NONE) {
760                 media_svc_error("error when _media_svc_get_burst_id. err = [%d]", ret);
761                 return ret;
762         }
763
764         cur_id = sqlite3_column_int(sql_stmt, 0);
765         *id = ++cur_id;
766         SQLITE3_FINALIZE(sql_stmt);
767
768         return MEDIA_INFO_ERROR_NONE;
769 }
770
771 int _media_svc_get_noti_info(sqlite3 *handle, const char *path, int update_item, media_svc_noti_item **item)
772 {
773         int ret = MEDIA_INFO_ERROR_NONE;
774         sqlite3_stmt *sql_stmt = NULL;
775         char *sql = NULL;
776         int is_root_dir = FALSE;
777
778         if (item == NULL) {
779                 media_svc_error("_media_svc_get_noti_info failed");
780                 return MEDIA_INFO_ERROR_INVALID_PARAMETER;
781         }
782
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);
787         } else {
788                 media_svc_error("_media_svc_get_noti_info failed : update item");
789                 return MEDIA_INFO_ERROR_INVALID_PARAMETER;
790         }
791
792         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
793
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");
797                         sql_stmt = NULL;
798                         is_root_dir = TRUE;
799                 } else {
800                         media_svc_error("error when _media_svc_get_noti_info. err = [%d]", ret);
801                         return ret;
802                 }
803         }
804
805         *item = calloc(1, sizeof(media_svc_noti_item));
806         if (*item == NULL) {
807                 media_svc_error("_media_svc_get_noti_info failed : calloc");
808                 return MEDIA_INFO_ERROR_OUT_OF_MEMORY;
809         }
810
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));
814
815                 (*item)->media_type = sqlite3_column_int(sql_stmt, 1);
816
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) {
820                 if (is_root_dir) {
821                                 (*item)->media_uuid = NULL;
822                 } else {
823                         if (sqlite3_column_text(sql_stmt, 0))
824                                 (*item)->media_uuid = strdup((const char *)sqlite3_column_text(sql_stmt, 0));
825                 }
826         }
827
828         SQLITE3_FINALIZE(sql_stmt);
829
830         return MEDIA_INFO_ERROR_NONE;
831 }
832
833 int _media_svc_count_invalid_folder_items(sqlite3 *handle, const char *folder_path, int *count)
834 {
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);
839
840         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
841
842         if (ret != MEDIA_INFO_ERROR_NONE) {
843                 media_svc_error("error when __media_svc_count_invalid_folder_records_with_thumbnail. err = [%d]", ret);
844                 return ret;
845         }
846
847         *count = sqlite3_column_int(sql_stmt, 0);
848
849         SQLITE3_FINALIZE(sql_stmt);
850
851         return MEDIA_INFO_ERROR_NONE;
852 }
853
854 int _media_svc_get_thumbnail_count(sqlite3 *handle, const char *thumb_path, int *count)
855 {
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);
859
860         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
861
862         if (ret != MEDIA_INFO_ERROR_NONE) {
863                 media_svc_error("error when _media_svc_get_thumbnail_count. err = [%d]", ret);
864                 return ret;
865         }
866
867         *count = sqlite3_column_int(sql_stmt, 0);
868
869         SQLITE3_FINALIZE(sql_stmt);
870
871         return MEDIA_INFO_ERROR_NONE;
872 }