Remove drm source code.
[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-util-err.h>
26 #include "media-svc-media.h"
27 #include "media-svc-media-folder.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 = MS_MEDIA_ERR_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 != MS_MEDIA_ERR_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 MS_MEDIA_ERR_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 ret = MS_MEDIA_ERR_NONE;
76         sqlite3_stmt *sql_stmt = NULL;
77         int idx = 0;
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         ret = sqlite3_prepare_v2(handle, sql, -1, &sql_stmt, NULL);
85         sqlite3_free(sql);
86         if (ret != SQLITE_OK) {
87                 media_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
88                 return MS_MEDIA_ERR_DB_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 MS_MEDIA_ERR_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 = MS_MEDIA_ERR_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 != MS_MEDIA_ERR_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 MS_MEDIA_ERR_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 ret = MS_MEDIA_ERR_NONE;
128         sqlite3_stmt *sql_stmt = NULL;
129         int idx = 0;
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         ret = sqlite3_prepare_v2(handle, sql, -1, &sql_stmt, NULL);
137         sqlite3_free(sql);
138         if (ret != MS_MEDIA_ERR_NONE) {
139                 media_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
140                 return ret;
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 MS_MEDIA_ERR_NONE;
151 }
152
153 int _media_svc_count_record_with_path(sqlite3 *handle, const char *path, int *count)
154 {
155         int ret = MS_MEDIA_ERR_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 != MS_MEDIA_ERR_NONE, ret);
163
164         *count = sqlite3_column_int(sql_stmt, 0);
165
166         SQLITE3_FINALIZE(sql_stmt);
167
168         return MS_MEDIA_ERR_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         if(uid == getuid())
176         {
177                 result_psswd = strdup(MEDIA_SVC_THUMB_DEFAULT_PATH);
178                 grpinfo = getgrnam("users");
179                 if(grpinfo == NULL) {
180                         media_svc_error("getgrnam(users) returns NULL !");
181                         return NULL;
182                 }
183         }
184         else
185         {
186                 struct passwd *userinfo = getpwuid(uid);
187                 if(userinfo == NULL) {
188                         media_svc_error("getpwuid(%d) returns NULL !", uid);
189                         return NULL;
190                 }
191                 grpinfo = getgrnam("users");
192                 if(grpinfo == NULL) {
193                         media_svc_error("getgrnam(users) returns NULL !");
194                         return NULL;
195                 }
196                 // Compare git_t type and not group name
197                 if (grpinfo->gr_gid != userinfo->pw_gid) {
198                         media_svc_error("UID [%d] does not belong to 'users' group!", uid);
199                         return NULL;
200                 }
201                 asprintf(&result_psswd, "%s/data/file-manager-service/.thumb/thumb_default.png", userinfo->pw_dir);
202         }
203
204         return result_psswd;
205 }
206
207 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)
208 {
209         media_svc_debug("");
210         int ret = MS_MEDIA_ERR_NONE;
211         char *burst_id = NULL;
212
213         char * db_fields = "media_uuid, path, file_name, media_type, mime_type, size, added_time, modified_time, folder_uuid, \
214                                         thumbnail_path, title, album_id, album, artist, album_artist, genre, composer, year, recorded_date, copyright, track_num, description,\
215                                         bitrate, bitpersample, samplerate, channel, duration, longitude, latitude, altitude, width, height, datetaken, orientation,\
216                                         rating, is_drm, storage_type, burst_id, timeline, weather, sync_status, \
217                                         file_name_pinyin, title_pinyin, album_pinyin, artist_pinyin, album_artist_pinyin, genre_pinyin, composer_pinyin, copyright_pinyin, description_pinyin ";
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                 ret = _media_svc_get_burst_id(handle, &burst_id_int);
227                 if (ret != MS_MEDIA_ERR_NONE) {
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                 ret = thumbnail_request_from_db_with_size(content_info->path, thumb_path, sizeof(thumb_path), &width, &height, uid);
242                 if (ret != MS_MEDIA_ERR_NONE) {
243                         media_svc_error("thumbnail_request_from_db failed: %d", ret);
244                 } else {
245                         media_svc_debug("thumbnail_request_from_db success: %s", thumb_path);
246                         ret = __media_svc_malloc_and_strncpy(&(content_info->thumbnail_path), thumb_path);
247                         if (ret != MS_MEDIA_ERR_NONE) {
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         /*Update Pinyin If Support Pinyin*/
260         if(_media_svc_check_pinyin_support())
261         {
262                 if(STRING_VALID(content_info->file_name))
263                         _media_svc_get_pinyin_str(content_info->file_name, &content_info->file_name_pinyin);
264                 if(STRING_VALID(content_info->media_meta.title))
265                         _media_svc_get_pinyin_str(content_info->media_meta.title, &content_info->media_meta.title_pinyin);
266                 if(STRING_VALID(content_info->media_meta.album))
267                         _media_svc_get_pinyin_str(content_info->media_meta.album, &content_info->media_meta.album_pinyin);
268                 if(STRING_VALID(content_info->media_meta.artist))
269                         _media_svc_get_pinyin_str(content_info->media_meta.artist, &content_info->media_meta.artist_pinyin);
270                 if(STRING_VALID(content_info->media_meta.album_artist))
271                         _media_svc_get_pinyin_str(content_info->media_meta.album_artist, &content_info->media_meta.album_artist_pinyin);
272                 if(STRING_VALID(content_info->media_meta.genre))
273                         _media_svc_get_pinyin_str(content_info->media_meta.genre, &content_info->media_meta.genre_pinyin);
274                 if(STRING_VALID(content_info->media_meta.composer))
275                         _media_svc_get_pinyin_str(content_info->media_meta.composer, &content_info->media_meta.composer_pinyin);
276                 if(STRING_VALID(content_info->media_meta.copyright))
277                         _media_svc_get_pinyin_str(content_info->media_meta.copyright, &content_info->media_meta.copyright_pinyin);
278                 if(STRING_VALID(content_info->media_meta.description))
279                         _media_svc_get_pinyin_str(content_info->media_meta.description, &content_info->media_meta.description_pinyin);
280         }
281
282         char *sql = sqlite3_mprintf("INSERT INTO %s (%s) VALUES (%Q, %Q, %Q, %d, %Q, %lld, %d, %d, %Q, \
283                                                                                                         %Q, %Q, %d, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, \
284                                                                                                         %d, %d, %d, %d, %d, %.6f, %.6f, %.6f, %d, %d, %Q, %d, \
285                                                                                                         %d, %d, %d, %Q, %d, %Q, %d, \
286                                                                                                         %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q);",
287                 MEDIA_SVC_DB_TABLE_MEDIA, db_fields,
288                 content_info->media_uuid,
289                 content_info->path,
290                 content_info->file_name,
291                 content_info->media_type,
292                 content_info->mime_type,
293                 content_info->size,
294                 content_info->added_time,
295                 content_info->modified_time,
296                 content_info->folder_uuid,
297                 content_info->thumbnail_path,           //
298                 content_info->media_meta.title,
299                 content_info->album_id,
300                 content_info->media_meta.album,
301                 content_info->media_meta.artist,
302                 content_info->media_meta.album_artist,
303                 content_info->media_meta.genre,
304                 content_info->media_meta.composer,
305                 content_info->media_meta.year,
306                 content_info->media_meta.recorded_date,
307                 content_info->media_meta.copyright,
308                 content_info->media_meta.track_num,
309                 content_info->media_meta.description,   //
310                 content_info->media_meta.bitrate,
311                 content_info->media_meta.bitpersample,
312                 content_info->media_meta.samplerate,
313                 content_info->media_meta.channel,
314                 content_info->media_meta.duration,
315                 content_info->media_meta.longitude,
316                 content_info->media_meta.latitude,
317                 content_info->media_meta.altitude,
318                 content_info->media_meta.width,
319                 content_info->media_meta.height,
320                 content_info->media_meta.datetaken,
321                 content_info->media_meta.orientation,
322                 content_info->media_meta.rating,
323                 content_info->is_drm,
324                 content_info->storage_type,
325                 burst_id,
326                 content_info->timeline,
327                 content_info->media_meta.weather,
328                 content_info->sync_status,
329                 content_info->file_name_pinyin,
330                 content_info->media_meta.title_pinyin,
331                 content_info->media_meta.album_pinyin,
332                 content_info->media_meta.artist_pinyin,
333                 content_info->media_meta.album_artist_pinyin,
334                 content_info->media_meta.genre_pinyin,
335                 content_info->media_meta.composer_pinyin,
336                 content_info->media_meta.copyright_pinyin,
337                 content_info->media_meta.description_pinyin
338                 );
339
340         if (burst_id)
341         {
342                 sqlite3_free(burst_id);
343                 burst_id = NULL;
344         }
345
346         if(!stack_query) {
347                 ret = _media_svc_sql_query(handle, sql, uid);
348                 sqlite3_free(sql);
349                 if (ret != MS_MEDIA_ERR_NONE) {
350                         media_svc_error("failed to insert item");
351                         return ret;
352                 }
353         } else {
354                 media_svc_debug("query : %s", sql);
355                 _media_svc_sql_query_add(&g_media_svc_insert_item_query_list, &sql);
356         }
357
358         return MS_MEDIA_ERR_NONE;
359 }
360
361 int _media_svc_update_item_with_data(sqlite3 *handle, media_svc_content_info_s *content_info, uid_t uid)
362 {
363         int ret = MS_MEDIA_ERR_NONE;
364
365         /* This sql is due to sqlite3_mprintf's wrong operation when using floating point in the text format */
366         /* This code will be removed when sqlite3_mprintf works clearly */
367         char *test_sql = sqlite3_mprintf("%f, %f, %f", content_info->media_meta.longitude, content_info->media_meta.latitude, content_info->media_meta.altitude);
368         sqlite3_free(test_sql);
369
370         /*Update Pinyin If Support Pinyin*/
371         if(_media_svc_check_pinyin_support())
372         {
373                 if(STRING_VALID(content_info->media_meta.title))
374                         _media_svc_get_pinyin_str(content_info->media_meta.title, &content_info->media_meta.title_pinyin);
375                 if(STRING_VALID(content_info->media_meta.album))
376                         _media_svc_get_pinyin_str(content_info->media_meta.album, &content_info->media_meta.album_pinyin);
377                 if(STRING_VALID(content_info->media_meta.artist))
378                         _media_svc_get_pinyin_str(content_info->media_meta.artist, &content_info->media_meta.artist_pinyin);
379                 if(STRING_VALID(content_info->media_meta.album_artist))
380                         _media_svc_get_pinyin_str(content_info->media_meta.album_artist, &content_info->media_meta.album_artist_pinyin);
381                 if(STRING_VALID(content_info->media_meta.genre))
382                         _media_svc_get_pinyin_str(content_info->media_meta.genre, &content_info->media_meta.genre_pinyin);
383                 if(STRING_VALID(content_info->media_meta.composer))
384                         _media_svc_get_pinyin_str(content_info->media_meta.composer, &content_info->media_meta.composer_pinyin);
385                 if(STRING_VALID(content_info->media_meta.copyright))
386                         _media_svc_get_pinyin_str(content_info->media_meta.copyright, &content_info->media_meta.copyright_pinyin);
387                 if(STRING_VALID(content_info->media_meta.description))
388                         _media_svc_get_pinyin_str(content_info->media_meta.description, &content_info->media_meta.description_pinyin);
389         }
390
391         char *sql = sqlite3_mprintf("UPDATE %s SET \
392                 size=%lld, modified_time=%d, thumbnail_path=%Q, title=%Q, album_id=%d, album=%Q, artist=%Q, album_artist=%Q, genre=%Q, \
393                 composer=%Q, year=%Q, recorded_date=%Q, copyright=%Q, track_num=%Q, description=%Q, \
394                 bitrate=%d, bitpersample=%d, samplerate=%d, channel=%d, duration=%d, longitude=%f, latitude=%f, altitude=%f, width=%d, height=%d, datetaken=%Q, \
395                                                                                                         orientation=%d WHERE path=%Q",
396                 MEDIA_SVC_DB_TABLE_MEDIA,
397                 content_info->size,
398                 content_info->modified_time,
399                 content_info->thumbnail_path,
400                 content_info->media_meta.title,
401                 content_info->album_id,
402                 content_info->media_meta.album,
403                 content_info->media_meta.artist,
404                 content_info->media_meta.album_artist,
405                 content_info->media_meta.genre,
406                 content_info->media_meta.composer,
407                 content_info->media_meta.year,
408                 content_info->media_meta.recorded_date,
409                 content_info->media_meta.copyright,
410                 content_info->media_meta.track_num,
411                 content_info->media_meta.description,
412                 content_info->media_meta.bitrate,
413                 content_info->media_meta.bitpersample,
414                 content_info->media_meta.samplerate,
415                 content_info->media_meta.channel,
416                 content_info->media_meta.duration,
417                 content_info->media_meta.longitude,
418                 content_info->media_meta.latitude,
419                 content_info->media_meta.altitude,
420                 content_info->media_meta.width,
421                 content_info->media_meta.height,
422                 content_info->media_meta.datetaken,
423                 content_info->media_meta.orientation,
424                 content_info->path
425                 );
426
427         ret = _media_svc_sql_query(handle, sql, uid);
428         sqlite3_free(sql);
429
430         return ret;
431 }
432 int _media_svc_get_thumbnail_path_by_path(sqlite3 *handle, const char *path, char *thumbnail_path)
433 {
434         int ret = MS_MEDIA_ERR_NONE;
435         sqlite3_stmt *sql_stmt = NULL;
436
437         char *sql = sqlite3_mprintf("SELECT thumbnail_path FROM %s WHERE path='%q'", MEDIA_SVC_DB_TABLE_MEDIA, path);
438
439         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
440
441         if (ret != MS_MEDIA_ERR_NONE) {
442                 if(ret == MS_MEDIA_ERR_DB_NO_RECORD) {
443                         media_svc_debug("there is no thumbnail.");
444                 }
445                 else {
446                         media_svc_error("error when _media_svc_get_thumbnail_path_by_path. err = [%d]", ret);
447                 }
448                 return ret;
449         }
450
451         _strncpy_safe(thumbnail_path, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_PATHNAME_SIZE);
452
453         SQLITE3_FINALIZE(sql_stmt);
454
455         return MS_MEDIA_ERR_NONE;
456 }
457
458 int _media_svc_get_media_type_by_path(sqlite3 *handle, const char *path, int *media_type)
459 {
460         int ret = MS_MEDIA_ERR_NONE;
461         sqlite3_stmt *sql_stmt = NULL;
462
463         char *sql = sqlite3_mprintf("SELECT media_type FROM %s WHERE path='%q'", MEDIA_SVC_DB_TABLE_MEDIA, path);
464
465         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
466
467         if (ret != MS_MEDIA_ERR_NONE) {
468                 media_svc_error("error when _media_svc_get_media_type_by_path. err = [%d]", ret);
469                 return ret;
470         }
471
472         *media_type = sqlite3_column_int(sql_stmt, 0);
473
474         SQLITE3_FINALIZE(sql_stmt);
475
476         return MS_MEDIA_ERR_NONE;
477 }
478
479 int _media_svc_delete_item_by_path(sqlite3 *handle, const char *path, uid_t uid)
480 {
481         int ret = MS_MEDIA_ERR_NONE;
482         char *sql = sqlite3_mprintf("DELETE FROM %s WHERE validity=1 AND path='%q'", MEDIA_SVC_DB_TABLE_MEDIA, path);
483
484         ret = _media_svc_sql_query(handle, sql, uid);
485         sqlite3_free(sql);
486         if (ret != MS_MEDIA_ERR_NONE) {
487                 media_svc_error("failed to delete item");
488                 return ret;
489         }
490
491         return ret;
492 }
493
494 int _media_svc_truncate_table(sqlite3 *handle, media_svc_storage_type_e storage_type, uid_t uid)
495 {
496         int ret = MS_MEDIA_ERR_NONE;
497         char *sql = sqlite3_mprintf("DELETE FROM %s WHERE storage_type=%d", MEDIA_SVC_DB_TABLE_MEDIA, storage_type);
498
499         ret = _media_svc_sql_query(handle, sql, uid);
500         sqlite3_free(sql);
501
502         return ret;
503 }
504
505 int _media_svc_delete_invalid_items(sqlite3 *handle, media_svc_storage_type_e storage_type, uid_t uid)
506 {
507         int idx = 0;
508         media_svc_thumbnailpath_s *thumbpath_record = NULL;
509         int invalid_count = 0;
510         int ret = MS_MEDIA_ERR_NONE;
511
512         ret = __media_svc_count_invalid_records_with_thumbnail(handle, storage_type, &invalid_count);
513         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
514
515         media_svc_debug("invalid count: %d\n", invalid_count);
516
517         if (invalid_count > 0) {
518                 thumbpath_record = (media_svc_thumbnailpath_s *)calloc( invalid_count, sizeof(media_svc_thumbnailpath_s));
519                 if (thumbpath_record == NULL) {
520                         media_svc_error("fail to memory allocation");
521                         return MS_MEDIA_ERR_OUT_OF_MEMORY;
522                 }
523
524                 ret = __media_svc_get_invalid_records_with_thumbnail(handle, storage_type, invalid_count, thumbpath_record);
525                 if (ret != MS_MEDIA_ERR_NONE) {
526                         media_svc_error("error when get thumbnail record");
527                         SAFE_FREE(thumbpath_record);
528                         return ret;
529                 }
530         } else {
531                 media_svc_debug("There is no item with thumbnail");
532         }
533
534         char *sql = sqlite3_mprintf("DELETE FROM %s WHERE validity = 0 AND storage_type=%d", MEDIA_SVC_DB_TABLE_MEDIA, storage_type);
535         ret = _media_svc_sql_query(handle, sql, uid);
536         sqlite3_free(sql);
537         if(ret != MS_MEDIA_ERR_NONE) {
538                 SAFE_FREE(thumbpath_record);
539                 return ret;
540         }
541
542         /*Delete thumbnails*/
543         for (idx = 0; idx < invalid_count; idx++) {
544                 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)) {
545                         if (_media_svc_remove_file(thumbpath_record[idx].thumbnail_path) == FALSE) {
546                                 media_svc_error("fail to remove thumbnail file.");
547                         }
548                 }
549         }
550
551         SAFE_FREE(thumbpath_record);
552
553         return MS_MEDIA_ERR_NONE;
554 }
555
556 int _media_svc_delete_invalid_folder_items(sqlite3 *handle, const char *folder_path, uid_t uid)
557 {
558         int idx = 0;
559         media_svc_thumbnailpath_s *thumbpath_record = NULL;
560         int invalid_count = 0;
561         int ret = MS_MEDIA_ERR_NONE;
562
563         ret = __media_svc_count_invalid_folder_records_with_thumbnail(handle, folder_path, &invalid_count);
564         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
565
566         media_svc_debug("invalid count: %d", invalid_count);
567
568         if (invalid_count > 0) {
569                 thumbpath_record = (media_svc_thumbnailpath_s *)calloc( invalid_count, sizeof(media_svc_thumbnailpath_s));
570                 if (thumbpath_record == NULL) {
571                         media_svc_error("fail to memory allocation");
572                         return MS_MEDIA_ERR_OUT_OF_MEMORY;
573                 }
574
575                 ret = __media_svc_get_invalid_folder_records_with_thumbnail(handle, folder_path, invalid_count, thumbpath_record);
576                 if (ret != MS_MEDIA_ERR_NONE) {
577                         media_svc_error("error when get thumbnail record");
578                         SAFE_FREE(thumbpath_record);
579                         return ret;
580                 }
581         } else {
582                 media_svc_debug("There is no item with thumbnail");
583         }
584
585         char *sql = sqlite3_mprintf("DELETE FROM %s WHERE validity = 0 AND path LIKE '%q/%%'", MEDIA_SVC_DB_TABLE_MEDIA, folder_path);
586         ret = _media_svc_sql_query(handle, sql, uid);
587         sqlite3_free(sql);
588         if(ret != MS_MEDIA_ERR_NONE) {
589                 SAFE_FREE(thumbpath_record);
590                 return ret;
591         }
592
593         /*Delete thumbnails*/
594         for (idx = 0; idx < invalid_count; idx++) {
595                 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)) {
596                         if (_media_svc_remove_file(thumbpath_record[idx].thumbnail_path) == FALSE) {
597                                 media_svc_error("fail to remove thumbnail file [%s].", thumbpath_record[idx].thumbnail_path);
598                         }
599                 }
600         }
601
602         SAFE_FREE(thumbpath_record);
603
604         return MS_MEDIA_ERR_NONE;
605 }
606
607 int _media_svc_update_item_validity(sqlite3 *handle, const char *path, int validity, bool stack_query, uid_t uid)
608 {
609         int ret = MS_MEDIA_ERR_NONE;
610
611         char *sql = sqlite3_mprintf("UPDATE %s SET validity=%d WHERE path= '%q'", MEDIA_SVC_DB_TABLE_MEDIA, validity, path);
612
613         if(!stack_query) {
614                 ret = _media_svc_sql_query(handle, sql, uid);
615                 sqlite3_free(sql);
616         } else {
617                 _media_svc_sql_query_add(&g_media_svc_item_validity_query_list, &sql);
618         }
619
620         return ret;
621 }
622
623 int _media_svc_update_thumbnail_path(sqlite3 *handle, const char *path, const char *thumb_path, uid_t uid)
624 {
625         int ret = MS_MEDIA_ERR_NONE;
626
627         char *sql = sqlite3_mprintf("UPDATE %s SET thumbnail_path=%Q WHERE path= %Q", MEDIA_SVC_DB_TABLE_MEDIA, thumb_path, path);
628
629         ret = _media_svc_sql_query(handle, sql, uid);
630         sqlite3_free(sql);
631
632         return ret;
633 }
634
635 int _media_svc_update_storage_item_validity(sqlite3 *handle, media_svc_storage_type_e storage_type, int validity, uid_t uid)
636 {
637         int ret = MS_MEDIA_ERR_NONE;
638
639         char *sql = sqlite3_mprintf("UPDATE %s SET validity=%d WHERE storage_type=%d", MEDIA_SVC_DB_TABLE_MEDIA, validity, storage_type);
640
641         ret = _media_svc_sql_query(handle, sql, uid);
642         sqlite3_free(sql);
643
644         return ret;
645 }
646
647 int _media_svc_update_folder_item_validity(sqlite3 *handle, const char *folder_path, int validity, uid_t uid)
648 {
649         int ret = MS_MEDIA_ERR_NONE;
650         char *sql = NULL;
651         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
652         sqlite3_stmt *sql_stmt = NULL;
653
654         /*Get folder ID*/
655         sql = sqlite3_mprintf("SELECT folder_uuid FROM %s WHERE path='%q'", MEDIA_SVC_DB_TABLE_FOLDER, folder_path);
656         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
657         if (ret != MS_MEDIA_ERR_NONE) {
658                 media_svc_error("error when get folder_id. err = [%d]", ret);
659                 return ret;
660         }
661
662         _strncpy_safe(folder_uuid, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE+1);
663         SQLITE3_FINALIZE(sql_stmt);
664
665         /*Update folder item validity*/
666         sql = sqlite3_mprintf("UPDATE %s SET validity=%d WHERE folder_uuid='%q'", MEDIA_SVC_DB_TABLE_MEDIA, validity, folder_uuid);
667         ret = _media_svc_sql_query(handle, sql, uid);
668         sqlite3_free(sql);
669
670         return ret;
671 }
672
673 int _media_svc_update_recursive_folder_item_validity(sqlite3 *handle, const char *folder_path, int validity, uid_t uid)
674 {
675         int ret = MS_MEDIA_ERR_NONE;
676
677         /*Update folder item validity*/
678         char *sql = sqlite3_mprintf("UPDATE %s SET validity=%d WHERE path LIKE '%q/%%'", MEDIA_SVC_DB_TABLE_MEDIA, validity, folder_path);
679         ret = _media_svc_sql_query(handle, sql, uid);
680         sqlite3_free(sql);
681
682         return ret;
683 }
684
685 int _media_svc_update_item_by_path(sqlite3 *handle, const char *src_path, media_svc_storage_type_e dest_storage, const char *dest_path,
686                                 const char *file_name, int modified_time, const char *folder_uuid, const char *thumb_path, bool stack_query, uid_t uid)
687 {
688         /* update path, filename, modified_time, folder_uuid, thumbnail_path, */
689         /* played_count, last_played_time, last_played_position, favourite, storaget_type*/
690
691         int ret = MS_MEDIA_ERR_NONE;
692         char *sql = NULL;
693
694         if(thumb_path != NULL) {
695                 sql = sqlite3_mprintf("UPDATE %s SET \
696                                         path=%Q, file_name=%Q, modified_time=%d, folder_uuid=%Q, thumbnail_path=%Q, storage_type=%d, \
697                                         played_count=0, last_played_time=0, last_played_position=0 \
698                                         WHERE path=%Q",
699                                         MEDIA_SVC_DB_TABLE_MEDIA, dest_path, file_name, modified_time, folder_uuid, thumb_path, dest_storage, src_path);
700         } else {
701                 sql = sqlite3_mprintf("UPDATE %s SET \
702                                         path=%Q, file_name=%Q, modified_time=%d, folder_uuid=%Q, storage_type=%d, \
703                                         played_count=0, last_played_time=0, last_played_position=0 \
704                                         WHERE path=%Q",
705                                         MEDIA_SVC_DB_TABLE_MEDIA, dest_path, file_name, modified_time, folder_uuid, dest_storage, src_path);
706         }
707
708         if(!stack_query) {
709                 ret = _media_svc_sql_query(handle, sql, uid);
710                 sqlite3_free(sql);
711         } else {
712                 _media_svc_sql_query_add(&g_media_svc_move_item_query_list, &sql);
713         }
714
715         return ret;
716 }
717
718 int _media_svc_list_query_do(sqlite3 *handle, media_svc_query_type_e query_type, uid_t uid)
719 {
720         int ret = MS_MEDIA_ERR_NONE;
721
722         ret = _media_svc_sql_begin_trans(handle, uid);
723         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
724
725         if (query_type == MEDIA_SVC_QUERY_SET_ITEM_VALIDITY)
726                 ret = _media_svc_sql_query_list(handle, &g_media_svc_item_validity_query_list,uid);
727         else if (query_type == MEDIA_SVC_QUERY_MOVE_ITEM)
728                 ret = _media_svc_sql_query_list(handle, &g_media_svc_move_item_query_list, uid);
729         else if (query_type == MEDIA_SVC_QUERY_INSERT_ITEM)
730                 ret = _media_svc_sql_query_list(handle, &g_media_svc_insert_item_query_list, uid);
731         else
732                 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
733
734         if (ret != MS_MEDIA_ERR_NONE) {
735                 media_svc_error("_media_svc_list_query_do failed. start rollback");
736                 _media_svc_sql_rollback_trans(handle,uid);
737                 return ret;
738         }
739
740         ret = _media_svc_sql_end_trans(handle, uid);
741         if (ret != MS_MEDIA_ERR_NONE) {
742                 media_svc_error("mb_svc_sqlite3_commit_trans failed.. Now start to rollback\n");
743                 _media_svc_sql_rollback_trans(handle,uid);
744                 return ret;
745         }
746
747         return MS_MEDIA_ERR_NONE;
748 }
749
750 int _media_svc_get_media_id_by_path(sqlite3 *handle, const char *path, char *media_uuid, int max_length)
751 {
752         int ret = MS_MEDIA_ERR_NONE;
753         sqlite3_stmt *sql_stmt = NULL;
754         char *sql = sqlite3_mprintf("SELECT media_uuid FROM %s WHERE validity=1 AND path='%q'",
755                                         MEDIA_SVC_DB_TABLE_MEDIA, path);
756
757         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
758
759         if (ret != MS_MEDIA_ERR_NONE) {
760                 media_svc_error("error when __media_svc_count_invalid_records_with_thumbnail. err = [%d]", ret);
761                 return ret;
762         }
763
764         strncpy(media_uuid, (const char*)sqlite3_column_text(sql_stmt, 0), max_length);
765         media_uuid[max_length - 1] = '\0';
766
767         SQLITE3_FINALIZE(sql_stmt);
768
769         return MS_MEDIA_ERR_NONE;
770 }
771
772 int _media_svc_get_burst_id(sqlite3 *handle, int *id)
773 {
774         int ret = MS_MEDIA_ERR_NONE;
775         int cur_id = -1;
776         sqlite3_stmt *sql_stmt = NULL;
777         char *sql = sqlite3_mprintf("SELECT max(CAST(burst_id AS INTEGER)) FROM %s", MEDIA_SVC_DB_TABLE_MEDIA);
778
779         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
780
781         if (ret != MS_MEDIA_ERR_NONE) {
782                 media_svc_error("error when _media_svc_get_burst_id. err = [%d]", ret);
783                 return ret;
784         }
785
786         cur_id = sqlite3_column_int(sql_stmt, 0);
787         *id = ++cur_id;
788         SQLITE3_FINALIZE(sql_stmt);
789
790         return MS_MEDIA_ERR_NONE;
791 }
792
793 int _media_svc_get_noti_info(sqlite3 *handle, const char *path, int update_item, media_svc_noti_item **item)
794 {
795         int ret = MS_MEDIA_ERR_NONE;
796         sqlite3_stmt *sql_stmt = NULL;
797         char *sql = NULL;
798         int is_root_dir = FALSE;
799
800         if (item == NULL) {
801                 media_svc_error("_media_svc_get_noti_info failed");
802                 return MS_MEDIA_ERR_INVALID_PARAMETER;
803         }
804
805         if (update_item == MS_MEDIA_ITEM_FILE) {
806                 sql = sqlite3_mprintf("SELECT media_uuid, media_type, mime_type FROM %s WHERE path=%Q", MEDIA_SVC_DB_TABLE_MEDIA, path);
807         } else if (update_item == MS_MEDIA_ITEM_DIRECTORY) {
808                 sql = sqlite3_mprintf("SELECT folder_uuid FROM %s WHERE path=%Q", MEDIA_SVC_DB_TABLE_FOLDER, path);
809         } else {
810                 media_svc_error("_media_svc_get_noti_info failed : update item");
811                 return MS_MEDIA_ERR_INVALID_PARAMETER;
812         }
813
814         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
815
816         if (ret != MS_MEDIA_ERR_NONE) {
817                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD && update_item == MS_MEDIA_ITEM_DIRECTORY) {
818                         media_svc_debug("This is root directory of media");
819                         sql_stmt = NULL;
820                         is_root_dir = TRUE;
821                 } else {
822                         media_svc_error("error when _media_svc_get_noti_info. err = [%d]", ret);
823                         return ret;
824                 }
825         }
826
827         *item = calloc(1, sizeof(media_svc_noti_item));
828         if (*item == NULL) {
829                 media_svc_error("_media_svc_get_noti_info failed : calloc");
830                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
831         }
832
833         if (update_item == MS_MEDIA_ITEM_FILE) {
834                 if (sqlite3_column_text(sql_stmt, 0))
835                         (*item)->media_uuid = strdup((const char *)sqlite3_column_text(sql_stmt, 0));
836
837                 (*item)->media_type = sqlite3_column_int(sql_stmt, 1);
838
839                 if (sqlite3_column_text(sql_stmt, 2))
840                         (*item)->mime_type = strdup((const char *)sqlite3_column_text(sql_stmt, 2));
841         } else if (update_item == MS_MEDIA_ITEM_DIRECTORY) {
842                 if (is_root_dir) {
843                                 (*item)->media_uuid = NULL;
844                 } else {
845                         if (sqlite3_column_text(sql_stmt, 0))
846                                 (*item)->media_uuid = strdup((const char *)sqlite3_column_text(sql_stmt, 0));
847                 }
848         }
849
850         SQLITE3_FINALIZE(sql_stmt);
851
852         return MS_MEDIA_ERR_NONE;
853 }
854
855 int _media_svc_count_invalid_folder_items(sqlite3 *handle, const char *folder_path, int *count)
856 {
857         int ret = MS_MEDIA_ERR_NONE;
858         sqlite3_stmt *sql_stmt = NULL;
859         char *sql = sqlite3_mprintf("SELECT count(*) FROM %s WHERE validity=0 AND path LIKE '%q/%%'",
860                                         MEDIA_SVC_DB_TABLE_MEDIA, folder_path);
861
862         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
863
864         if (ret != MS_MEDIA_ERR_NONE) {
865                 media_svc_error("error when __media_svc_count_invalid_folder_records_with_thumbnail. err = [%d]", ret);
866                 return ret;
867         }
868
869         *count = sqlite3_column_int(sql_stmt, 0);
870
871         SQLITE3_FINALIZE(sql_stmt);
872
873         return MS_MEDIA_ERR_NONE;
874 }
875
876 int _media_svc_get_thumbnail_count(sqlite3 *handle, const char *thumb_path, int *count)
877 {
878         int ret = MS_MEDIA_ERR_NONE;
879         sqlite3_stmt *sql_stmt = NULL;
880         char *sql = sqlite3_mprintf("SELECT count(*) FROM %s WHERE thumbnail_path=%Q", MEDIA_SVC_DB_TABLE_MEDIA, thumb_path);
881
882         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
883
884         if (ret != MS_MEDIA_ERR_NONE) {
885                 media_svc_error("error when _media_svc_get_thumbnail_count. err = [%d]", ret);
886                 return ret;
887         }
888
889         *count = sqlite3_column_int(sql_stmt, 0);
890
891         SQLITE3_FINALIZE(sql_stmt);
892
893         return MS_MEDIA_ERR_NONE;
894 }