4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Hyunjun Ko <zzoon.ko@samsung.com>, Haejeong Kim <backto.kim@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
24 #include <sys/types.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"
33 #define MEDIA_SVC_MAX_COMMIT_SIZE 200
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 static __thread GList *g_media_svc_update_item_query_list = NULL;
39 static __thread GList *g_media_svc_update_list = NULL;
40 static __thread int g_media_svc_update_list_count = 0;
42 static int __media_svc_get_invalid_records_with_thumbnail(sqlite3 *handle, media_svc_storage_type_e storage_type, GArray **thumb_path)
44 int ret = MS_MEDIA_ERR_NONE;
45 sqlite3_stmt *sql_stmt = NULL;
46 char *sql = sqlite3_mprintf("SELECT thumbnail_path FROM '%s' WHERE validity=0 AND storage_type=%d AND thumbnail_path IS NOT NULL", MEDIA_SVC_DB_TABLE_MEDIA, storage_type);
48 media_svc_debug("[SQL query] : %s", sql);
50 ret = _media_svc_sql_prepare_to_step_simple(handle, sql, &sql_stmt);
51 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Query failed. err[%d]", ret);
53 while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
55 path = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
56 g_array_append_val(*thumb_path, path);
59 SQLITE3_FINALIZE(sql_stmt);
61 return MS_MEDIA_ERR_NONE;
64 static int __media_svc_get_invalid_folder_records_with_thumbnail(sqlite3 *handle, const char *folder_path, const char *folder_uuid, bool is_recursive, GArray **thumb_path)
66 int ret = MS_MEDIA_ERR_NONE;
67 sqlite3_stmt *sql_stmt = NULL;
71 sql = sqlite3_mprintf("SELECT thumbnail_path FROM '%s' WHERE validity=0 AND path LIKE '%q/%%' AND thumbnail_path IS NOT NULL", MEDIA_SVC_DB_TABLE_MEDIA, folder_path);
73 sql = sqlite3_mprintf("SELECT thumbnail_path FROM '%s' WHERE validity=0 AND folder_uuid = '%q' AND thumbnail_path IS NOT NULL", MEDIA_SVC_DB_TABLE_MEDIA, folder_uuid);
75 media_svc_debug("[SQL query] : %s", sql);
77 ret = _media_svc_sql_prepare_to_step_simple(handle, sql, &sql_stmt);
78 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Query failed. err[%d]", ret);
80 while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
82 path = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
83 g_array_append_val(*thumb_path, path);
86 SQLITE3_FINALIZE(sql_stmt);
88 return MS_MEDIA_ERR_NONE;
91 int _media_svc_count_record_with_path(sqlite3 *handle, const char *storage_id, const char *path, int *count)
93 int ret = MS_MEDIA_ERR_NONE;
94 sqlite3_stmt *sql_stmt = NULL;
96 char *sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE path='%q'", storage_id, path);
98 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
100 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
102 *count = sqlite3_column_int(sql_stmt, 0);
104 SQLITE3_FINALIZE(sql_stmt);
106 return MS_MEDIA_ERR_NONE;
109 int _media_svc_insert_item_with_data(sqlite3 *handle, const char *storage_id, media_svc_content_info_s *content_info, int is_burst, bool stack_query, uid_t uid)
111 int ret = MS_MEDIA_ERR_NONE;
112 char *burst_id = NULL;
114 const char *db_fields = "media_uuid, path, file_name, media_type, mime_type, size, added_time, modified_time, folder_uuid, thumbnail_path, \
115 title, album_id, album, artist, album_artist, genre, composer, year, recorded_date, copyright, track_num, description, category, keyword, location_tag, \
116 content_name, age_rating, author, provider, last_played_time, played_count, favourite, bitrate, bitpersample, samplerate, channel, duration, \
117 longitude, latitude, altitude, exposure_time, fnumber, iso, model, width, height, datetaken, orientation, rating, is_drm, storage_type, burst_id, timeline, weather, sync_status, \
118 file_name_pinyin, title_pinyin, album_pinyin, artist_pinyin, album_artist_pinyin, genre_pinyin, composer_pinyin, copyright_pinyin, description_pinyin, storage_uuid, is_360";
120 const char *db_fields_basic = "media_uuid, path, file_name, media_type, mime_type, size, added_time, modified_time, folder_uuid, thumbnail_path, \
121 album_id, last_played_time, played_count, favourite, is_drm, storage_type, burst_id, timeline, sync_status, file_name_pinyin, storage_uuid, is_360";
123 /* This sql is due to sqlite3_mprintf's wrong operation when using floating point in the text format */
124 /* This code will be removed when sqlite3_mprintf works clearly */
125 char *test_sql = sqlite3_mprintf("%f, %f, %f", content_info->media_meta.longitude, content_info->media_meta.latitude, content_info->media_meta.altitude);
126 SQLITE3_SAFE_FREE(test_sql);
129 int burst_id_int = 0;
130 ret = _media_svc_get_burst_id(handle, storage_id, &burst_id_int);
131 if (ret != MS_MEDIA_ERR_NONE)
134 if (burst_id_int > 0) {
135 media_svc_debug("Burst id : %d", burst_id_int);
136 burst_id = sqlite3_mprintf("%d", burst_id_int);
139 /* Get thumbnail for burst shot */
140 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
142 ret = _media_svc_request_thumbnail(content_info->path, thumb_path, sizeof(thumb_path), uid);
143 if (ret == MS_MEDIA_ERR_NONE)
144 content_info->thumbnail_path = g_strdup(thumb_path);
147 /*Update Pinyin If Support Pinyin*/
148 if (_media_svc_check_pinyin_support()) {
149 if (STRING_VALID(content_info->file_name))
150 _media_svc_get_pinyin_str(content_info->file_name, &content_info->file_name_pinyin);
151 if (STRING_VALID(content_info->media_meta.title))
152 _media_svc_get_pinyin_str(content_info->media_meta.title, &content_info->media_meta.title_pinyin);
153 if (STRING_VALID(content_info->media_meta.album))
154 _media_svc_get_pinyin_str(content_info->media_meta.album, &content_info->media_meta.album_pinyin);
155 if (STRING_VALID(content_info->media_meta.artist))
156 _media_svc_get_pinyin_str(content_info->media_meta.artist, &content_info->media_meta.artist_pinyin);
157 if (STRING_VALID(content_info->media_meta.album_artist))
158 _media_svc_get_pinyin_str(content_info->media_meta.album_artist, &content_info->media_meta.album_artist_pinyin);
159 if (STRING_VALID(content_info->media_meta.genre))
160 _media_svc_get_pinyin_str(content_info->media_meta.genre, &content_info->media_meta.genre_pinyin);
161 if (STRING_VALID(content_info->media_meta.composer))
162 _media_svc_get_pinyin_str(content_info->media_meta.composer, &content_info->media_meta.composer_pinyin);
163 if (STRING_VALID(content_info->media_meta.copyright))
164 _media_svc_get_pinyin_str(content_info->media_meta.copyright, &content_info->media_meta.copyright_pinyin);
165 if (STRING_VALID(content_info->media_meta.description))
166 _media_svc_get_pinyin_str(content_info->media_meta.description, &content_info->media_meta.description_pinyin);
169 char *sql = sqlite3_mprintf("INSERT INTO '%s' (%s) VALUES (%Q, %Q, %Q, %d, %Q, %lld, %d, %d, %Q, \
170 %Q, %Q, %d, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %d, %d, %d, \
171 %d, %d, %d, %d, %d, %.6f, %.6f, %.6f, %Q, %.6f, %d, %Q, %d, %d, %Q, %d, \
172 %d, %d, %d, %Q, %d, %Q, %d, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %d);",
173 content_info->storage_uuid, db_fields,
174 content_info->media_uuid,
176 content_info->file_name,
177 content_info->media_type,
178 content_info->mime_type,
180 content_info->added_time,
181 content_info->modified_time,
182 content_info->folder_uuid, /* 1st line */
183 content_info->thumbnail_path,
184 content_info->media_meta.title,
185 content_info->album_id,
186 content_info->media_meta.album,
187 content_info->media_meta.artist,
188 content_info->media_meta.album_artist,
189 content_info->media_meta.genre,
190 content_info->media_meta.composer,
191 content_info->media_meta.year,
192 content_info->media_meta.recorded_date,
193 content_info->media_meta.copyright,
194 content_info->media_meta.track_num,
195 content_info->media_meta.description,
196 content_info->media_meta.category,
197 content_info->media_meta.keyword,
198 content_info->media_meta.location_tag,
199 content_info->media_meta.content_name,
200 content_info->media_meta.age_rating,
201 content_info->media_meta.author,
202 content_info->media_meta.provider,
203 content_info->last_played_time,
204 content_info->played_count,
205 content_info->favourate, /* 2nd line */
206 content_info->media_meta.bitrate,
207 content_info->media_meta.bitpersample,
208 content_info->media_meta.samplerate,
209 content_info->media_meta.channel,
210 content_info->media_meta.duration,
211 content_info->media_meta.longitude,
212 content_info->media_meta.latitude,
213 content_info->media_meta.altitude,
214 content_info->media_meta.exposure_time,
215 content_info->media_meta.fnumber,
216 content_info->media_meta.iso,
217 content_info->media_meta.model,
218 content_info->media_meta.width,
219 content_info->media_meta.height,
220 content_info->media_meta.datetaken,
221 content_info->media_meta.orientation, /* 3rd line */
222 content_info->media_meta.rating,
223 content_info->is_drm,
224 content_info->storage_type,
226 content_info->timeline,
227 content_info->media_meta.weather,
228 content_info->sync_status,
229 content_info->file_name_pinyin,
230 content_info->media_meta.title_pinyin,
231 content_info->media_meta.album_pinyin,
232 content_info->media_meta.artist_pinyin,
233 content_info->media_meta.album_artist_pinyin,
234 content_info->media_meta.genre_pinyin,
235 content_info->media_meta.composer_pinyin,
236 content_info->media_meta.copyright_pinyin,
237 content_info->media_meta.description_pinyin,
238 content_info->storage_uuid,
239 content_info->media_meta.is_360 /* 4th line */
242 /* Check query length */
243 if (sql != NULL && strlen(sql) >= (MEDIA_SVC_QUERY_LEN_MAX - 1)) {
244 media_svc_debug("Query length is over %d byte.. So, remake query using only basic information..", MEDIA_SVC_QUERY_LEN_MAX - 1);
245 SQLITE3_SAFE_FREE(sql);
247 sql = sqlite3_mprintf("INSERT INTO '%s' (%s) VALUES (%Q, %Q, %Q, %d, %Q, %lld, %d, %d, %Q, %Q, %d, %d, %d, %d, %d, %d, %Q, %d, %d, %Q, %Q, %d);",
248 content_info->storage_uuid, db_fields_basic,
249 content_info->media_uuid,
251 content_info->file_name,
252 content_info->media_type,
253 content_info->mime_type,
255 content_info->added_time,
256 content_info->modified_time,
257 content_info->folder_uuid,
258 content_info->thumbnail_path,
259 content_info->album_id,
260 content_info->last_played_time,
261 content_info->played_count,
262 content_info->favourate,
263 content_info->is_drm,
264 content_info->storage_type,
266 content_info->timeline,
267 content_info->sync_status,
268 content_info->file_name_pinyin,
269 content_info->storage_uuid,
270 content_info->media_meta.is_360 /* 1st line */
274 SQLITE3_SAFE_FREE(burst_id);
277 ret = _media_svc_sql_query(sql, uid);
278 SQLITE3_SAFE_FREE(sql);
279 if (ret != MS_MEDIA_ERR_NONE) {
280 media_svc_error("failed to insert item");
284 media_svc_debug("query : %s", sql);
285 _media_svc_sql_query_add(&g_media_svc_insert_item_query_list, &sql);
288 return MS_MEDIA_ERR_NONE;
291 int _media_svc_update_meta_with_data(media_svc_content_info_s *content_info)
293 int ret = MS_MEDIA_ERR_NONE;
295 /* This sql is due to sqlite3_mprintf's wrong operation when using floating point in the text format */
296 /* This code will be removed when sqlite3_mprintf works clearly */
297 char *test_sql = sqlite3_mprintf("%f, %f, %f", content_info->media_meta.longitude, content_info->media_meta.latitude, content_info->media_meta.altitude);
298 SQLITE3_SAFE_FREE(test_sql);
300 /*Update Pinyin If Support Pinyin*/
301 if (_media_svc_check_pinyin_support()) {
302 if (STRING_VALID(content_info->file_name))
303 _media_svc_get_pinyin_str(content_info->file_name, &content_info->file_name_pinyin);
304 if (STRING_VALID(content_info->media_meta.title))
305 _media_svc_get_pinyin_str(content_info->media_meta.title, &content_info->media_meta.title_pinyin);
306 if (STRING_VALID(content_info->media_meta.album))
307 _media_svc_get_pinyin_str(content_info->media_meta.album, &content_info->media_meta.album_pinyin);
308 if (STRING_VALID(content_info->media_meta.artist))
309 _media_svc_get_pinyin_str(content_info->media_meta.artist, &content_info->media_meta.artist_pinyin);
310 if (STRING_VALID(content_info->media_meta.album_artist))
311 _media_svc_get_pinyin_str(content_info->media_meta.album_artist, &content_info->media_meta.album_artist_pinyin);
312 if (STRING_VALID(content_info->media_meta.genre))
313 _media_svc_get_pinyin_str(content_info->media_meta.genre, &content_info->media_meta.genre_pinyin);
314 if (STRING_VALID(content_info->media_meta.composer))
315 _media_svc_get_pinyin_str(content_info->media_meta.composer, &content_info->media_meta.composer_pinyin);
316 if (STRING_VALID(content_info->media_meta.copyright))
317 _media_svc_get_pinyin_str(content_info->media_meta.copyright, &content_info->media_meta.copyright_pinyin);
318 if (STRING_VALID(content_info->media_meta.description))
319 _media_svc_get_pinyin_str(content_info->media_meta.description, &content_info->media_meta.description_pinyin);
322 char *sql = sqlite3_mprintf("UPDATE %s SET title=%Q, album=%Q, artist=%Q, album_artist=%Q, genre=%Q, composer=%Q, copyright=%Q, description=%Q, \
323 file_name_pinyin=%Q, title_pinyin=%Q, album_pinyin=%Q, artist_pinyin=%Q, album_artist_pinyin=%Q, genre_pinyin=%Q, composer_pinyin=%Q, copyright_pinyin=%Q, description_pinyin=%Q \
325 MEDIA_SVC_DB_TABLE_MEDIA,
326 content_info->media_meta.title,
327 content_info->media_meta.album,
328 content_info->media_meta.artist,
329 content_info->media_meta.album_artist,
330 content_info->media_meta.genre,
331 content_info->media_meta.composer,
332 content_info->media_meta.copyright,
333 content_info->media_meta.description,
334 content_info->file_name_pinyin,
335 content_info->media_meta.title_pinyin,
336 content_info->media_meta.album_pinyin,
337 content_info->media_meta.artist_pinyin,
338 content_info->media_meta.album_artist_pinyin,
339 content_info->media_meta.genre_pinyin,
340 content_info->media_meta.composer_pinyin,
341 content_info->media_meta.copyright_pinyin,
342 content_info->media_meta.description_pinyin,
347 media_svc_debug("query : %s", sql);
348 _media_svc_sql_query_add(&g_media_svc_update_item_query_list, &sql);
350 media_svc_error("sqlite3_mprintf failed");
351 ret = MS_MEDIA_ERR_OUT_OF_MEMORY;
357 int _media_svc_update_item_with_data(const char *storage_id, media_svc_content_info_s *content_info, uid_t uid)
359 int ret = MS_MEDIA_ERR_NONE;
361 /* This sql is due to sqlite3_mprintf's wrong operation when using floating point in the text format */
362 /* This code will be removed when sqlite3_mprintf works clearly */
363 char *test_sql = sqlite3_mprintf("%f, %f, %f", content_info->media_meta.longitude, content_info->media_meta.latitude, content_info->media_meta.altitude);
364 SQLITE3_SAFE_FREE(test_sql);
366 /*Update Pinyin If Support Pinyin*/
367 if (_media_svc_check_pinyin_support()) {
368 if (STRING_VALID(content_info->file_name))
369 _media_svc_get_pinyin_str(content_info->file_name, &content_info->file_name_pinyin);
370 if (STRING_VALID(content_info->media_meta.title))
371 _media_svc_get_pinyin_str(content_info->media_meta.title, &content_info->media_meta.title_pinyin);
372 if (STRING_VALID(content_info->media_meta.album))
373 _media_svc_get_pinyin_str(content_info->media_meta.album, &content_info->media_meta.album_pinyin);
374 if (STRING_VALID(content_info->media_meta.artist))
375 _media_svc_get_pinyin_str(content_info->media_meta.artist, &content_info->media_meta.artist_pinyin);
376 if (STRING_VALID(content_info->media_meta.album_artist))
377 _media_svc_get_pinyin_str(content_info->media_meta.album_artist, &content_info->media_meta.album_artist_pinyin);
378 if (STRING_VALID(content_info->media_meta.genre))
379 _media_svc_get_pinyin_str(content_info->media_meta.genre, &content_info->media_meta.genre_pinyin);
380 if (STRING_VALID(content_info->media_meta.composer))
381 _media_svc_get_pinyin_str(content_info->media_meta.composer, &content_info->media_meta.composer_pinyin);
382 if (STRING_VALID(content_info->media_meta.copyright))
383 _media_svc_get_pinyin_str(content_info->media_meta.copyright, &content_info->media_meta.copyright_pinyin);
384 if (STRING_VALID(content_info->media_meta.description))
385 _media_svc_get_pinyin_str(content_info->media_meta.description, &content_info->media_meta.description_pinyin);
388 char *sql = sqlite3_mprintf("UPDATE '%s' SET size=%lld, modified_time=%d, thumbnail_path=%Q, title=%Q, album_id=%d, album=%Q, artist=%Q, album_artist=%Q, genre=%Q, \
389 composer=%Q, year=%Q, recorded_date=%Q, copyright=%Q, track_num=%Q, description=%Q, bitrate=%d, bitpersample=%d, samplerate=%d, channel=%d, duration=%d, \
390 longitude=%f, latitude=%f, altitude=%f, exposure_time=%Q, fnumber=%f, iso=%d, model=%Q, width=%d, height=%d, datetaken=%Q, orientation=%d WHERE path=%Q;",
393 content_info->modified_time,
394 content_info->thumbnail_path,
395 content_info->media_meta.title,
396 content_info->album_id,
397 content_info->media_meta.album,
398 content_info->media_meta.artist,
399 content_info->media_meta.album_artist,
400 content_info->media_meta.genre,
401 content_info->media_meta.composer,
402 content_info->media_meta.year,
403 content_info->media_meta.recorded_date,
404 content_info->media_meta.copyright,
405 content_info->media_meta.track_num,
406 content_info->media_meta.description,
407 content_info->media_meta.bitrate,
408 content_info->media_meta.bitpersample,
409 content_info->media_meta.samplerate,
410 content_info->media_meta.channel,
411 content_info->media_meta.duration,
412 content_info->media_meta.longitude,
413 content_info->media_meta.latitude,
414 content_info->media_meta.altitude,
415 content_info->media_meta.exposure_time,
416 content_info->media_meta.fnumber,
417 content_info->media_meta.iso,
418 content_info->media_meta.model,
419 content_info->media_meta.width,
420 content_info->media_meta.height,
421 content_info->media_meta.datetaken,
422 content_info->media_meta.orientation,
426 ret = _media_svc_sql_query(sql, uid);
427 SQLITE3_SAFE_FREE(sql);
431 int _media_svc_get_thumbnail_path_by_path(sqlite3 *handle, const char *path, char *thumbnail_path)
433 int ret = MS_MEDIA_ERR_NONE;
434 sqlite3_stmt *sql_stmt = NULL;
436 char *sql = sqlite3_mprintf("SELECT thumbnail_path FROM '%s' WHERE path='%q'", MEDIA_SVC_DB_TABLE_MEDIA, path);
438 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
440 if (ret != MS_MEDIA_ERR_NONE) {
441 if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
442 media_svc_debug("there is no thumbnail.");
444 media_svc_error("error when _media_svc_get_thumbnail_path_by_path. err = [%d]", ret);
449 SAFE_STRLCPY(thumbnail_path, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_PATHNAME_SIZE);
451 SQLITE3_FINALIZE(sql_stmt);
453 return MS_MEDIA_ERR_NONE;
456 int _media_svc_get_media_type_by_path(sqlite3 *handle, const char *storage_id, const char *path, int *media_type)
458 int ret = MS_MEDIA_ERR_NONE;
459 sqlite3_stmt *sql_stmt = NULL;
461 char *sql = sqlite3_mprintf("SELECT media_type FROM '%s' WHERE path='%q'", storage_id, path);
463 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
465 if (ret != MS_MEDIA_ERR_NONE) {
466 media_svc_error("error when _media_svc_get_media_type_by_path. err = [%d]", ret);
470 *media_type = sqlite3_column_int(sql_stmt, 0);
472 SQLITE3_FINALIZE(sql_stmt);
474 return MS_MEDIA_ERR_NONE;
477 int _media_svc_delete_item_by_path(const char *storage_id, const char *path, bool stack_query, uid_t uid)
479 int ret = MS_MEDIA_ERR_NONE;
480 char *sql = sqlite3_mprintf("DELETE FROM '%s' WHERE path='%q';", storage_id, path);
483 ret = _media_svc_sql_query(sql, uid);
484 SQLITE3_SAFE_FREE(sql);
485 if (ret != MS_MEDIA_ERR_NONE) {
486 media_svc_error("failed to delete item");
490 media_svc_debug("query : %s", sql);
491 _media_svc_sql_query_add(&g_media_svc_insert_item_query_list, &sql);
497 int _media_svc_truncate_table(const char *storage_id, media_svc_storage_type_e storage_type, uid_t uid)
499 int ret = MS_MEDIA_ERR_NONE;
501 char *sql = sqlite3_mprintf("DELETE FROM '%s' where storage_type=%d;", storage_id, storage_type);
503 ret = _media_svc_sql_query(sql, uid);
504 SQLITE3_SAFE_FREE(sql);
509 int _media_svc_delete_invalid_items(sqlite3 *handle, const char *storage_id, media_svc_storage_type_e storage_type, uid_t uid)
511 int ret = MS_MEDIA_ERR_NONE;
512 GArray *thumb_list = NULL;
514 thumb_list = g_array_new(FALSE, FALSE, sizeof(char*));
516 /*If external USB, no need this process */
517 if (storage_type != MEDIA_SVC_STORAGE_EXTERNAL_USB) {
518 ret = __media_svc_get_invalid_records_with_thumbnail(handle, storage_type, &thumb_list);
519 if (ret != MS_MEDIA_ERR_NONE) {
520 media_svc_error("error when get thumbnail record");
521 g_array_free(thumb_list, false);
527 char *sql = sqlite3_mprintf("DELETE FROM '%s' WHERE validity = 0 AND storage_type = %d;", storage_id, storage_type);
529 ret = _media_svc_sql_query(sql, uid);
530 SQLITE3_SAFE_FREE(sql);
531 if (ret != MS_MEDIA_ERR_NONE) {
532 while (thumb_list->len != 0) {
533 char *thumb_path = NULL;
534 thumb_path = g_array_index(thumb_list , char*, 0);
535 g_array_remove_index(thumb_list, 0);
536 SAFE_FREE(thumb_path);
538 g_array_free(thumb_list, false);
543 /*Delete thumbnails*/
544 while (thumb_list->len != 0) {
545 char *thumb_path = NULL;
546 thumb_path = g_array_index(thumb_list , char*, 0);
547 g_array_remove_index(thumb_list, 0);
549 ret = _media_svc_remove_file(thumb_path);
550 if (ret != MS_MEDIA_ERR_NONE)
551 media_svc_error("fail to remove thumbnail file.");
553 SAFE_FREE(thumb_path);
556 g_array_free(thumb_list, false);
559 return MS_MEDIA_ERR_NONE;
562 int _media_svc_delete_invalid_folder_items(sqlite3 *handle, const char *storage_id, const char *folder_path, bool is_recursive, uid_t uid)
564 int ret = MS_MEDIA_ERR_NONE;
565 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
566 GArray *thumb_list = NULL;
569 /*get folder uuid from DB*/
570 ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, folder_path, folder_uuid, uid);
571 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
573 thumb_list = g_array_new(FALSE, FALSE, sizeof(char*));
575 ret = __media_svc_get_invalid_folder_records_with_thumbnail(handle, folder_path, folder_uuid, is_recursive, &thumb_list);
576 if (ret != MS_MEDIA_ERR_NONE) {
577 media_svc_error("error when get thumbnail record");
578 g_array_free(thumb_list, false);
584 sql = sqlite3_mprintf("DELETE FROM '%s' WHERE validity = 0 AND path LIKE '%q/%%';", storage_id, folder_path);
586 sql = sqlite3_mprintf("DELETE FROM '%s' WHERE validity = 0 AND folder_uuid='%q';", storage_id, folder_uuid);
588 ret = _media_svc_sql_query(sql, uid);
589 SQLITE3_SAFE_FREE(sql);
590 if (ret != MS_MEDIA_ERR_NONE) {
591 while (thumb_list->len != 0) {
592 char *thumb_path = NULL;
593 thumb_path = g_array_index(thumb_list , char*, 0);
594 g_array_remove_index(thumb_list, 0);
595 SAFE_FREE(thumb_path);
597 g_array_free(thumb_list, false);
603 /*Delete thumbnails*/
604 while (thumb_list->len != 0) {
605 char *thumb_path = NULL;
606 thumb_path = g_array_index(thumb_list , char*, 0);
607 g_array_remove_index(thumb_list, 0);
609 ret = _media_svc_remove_file(thumb_path);
610 if (ret != MS_MEDIA_ERR_NONE)
611 media_svc_error("fail to remove thumbnail file.");
613 SAFE_FREE(thumb_path);
616 g_array_free(thumb_list, false);
619 return MS_MEDIA_ERR_NONE;
622 int _media_svc_update_item_validity(const char *storage_id, const char *path, int validity, bool stack_query, uid_t uid)
624 int ret = MS_MEDIA_ERR_NONE;
626 char *sql = sqlite3_mprintf("UPDATE '%s' SET validity=%d WHERE path= '%q';", storage_id, validity, path);
629 ret = _media_svc_sql_query(sql, uid);
630 SQLITE3_SAFE_FREE(sql);
632 _media_svc_sql_query_add(&g_media_svc_item_validity_query_list, &sql);
638 int _media_svc_update_thumbnail_path(const char *path, const char *thumb_path, uid_t uid)
640 int ret = MS_MEDIA_ERR_NONE;
642 char *sql = sqlite3_mprintf("UPDATE '%s' SET thumbnail_path=%Q WHERE path= %Q;", MEDIA_SVC_DB_TABLE_MEDIA, thumb_path, path);
644 ret = _media_svc_sql_query(sql, uid);
645 SQLITE3_SAFE_FREE(sql);
650 int _media_svc_update_storage_item_validity(const char *storage_id, media_svc_storage_type_e storage_type, int validity, uid_t uid)
652 int ret = MS_MEDIA_ERR_NONE;
654 char *sql = sqlite3_mprintf("UPDATE '%s' SET validity=%d WHERE storage_type=%d;", storage_id, validity, storage_type);
656 ret = _media_svc_sql_query(sql, uid);
657 SQLITE3_SAFE_FREE(sql);
662 int _media_svc_update_folder_item_validity(sqlite3 *handle, const char *storage_id, const char *folder_path, int validity, uid_t uid)
664 int ret = MS_MEDIA_ERR_NONE;
666 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
667 sqlite3_stmt *sql_stmt = NULL;
670 sql = sqlite3_mprintf("SELECT folder_uuid FROM '%s' WHERE path='%q' AND storage_uuid='%q'", MEDIA_SVC_DB_TABLE_FOLDER, folder_path, storage_id);
672 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
673 if (ret != MS_MEDIA_ERR_NONE) {
674 if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
675 media_svc_debug("folder not exist");
677 media_svc_error("error when get folder_id. err = [%d]", ret);
682 SAFE_STRLCPY(folder_uuid, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE + 1);
683 SQLITE3_FINALIZE(sql_stmt);
685 /*Update folder item validity*/
686 sql = sqlite3_mprintf("UPDATE '%s' SET validity=%d WHERE folder_uuid='%q';", storage_id, validity, folder_uuid);
688 ret = _media_svc_sql_query(sql, uid);
689 SQLITE3_SAFE_FREE(sql);
694 int _media_svc_update_recursive_folder_item_validity(const char *storage_id, const char *folder_path, int validity, uid_t uid)
696 int ret = MS_MEDIA_ERR_NONE;
698 /*Update folder item validity*/
699 char *sql = sqlite3_mprintf("UPDATE '%s' SET validity=%d WHERE (storage_type = 0 OR storage_type = 1) AND path LIKE '%q/%%';", storage_id, validity, folder_path);
701 ret = _media_svc_sql_query(sql, uid);
702 SQLITE3_SAFE_FREE(sql);
707 int _media_svc_update_item_by_path(const char *src_path, media_svc_storage_type_e dest_storage, const char *dest_path,
708 const char *file_name, int modified_time, const char *folder_uuid, const char *thumb_path, bool stack_query, uid_t uid)
710 /* update path, filename, modified_time, folder_uuid, thumbnail_path, */
711 /* played_count, last_played_time, last_played_position, favourite, storaget_type*/
713 int ret = MS_MEDIA_ERR_NONE;
716 if (thumb_path != NULL) {
717 sql = sqlite3_mprintf("UPDATE '%s' SET path=%Q, file_name=%Q, modified_time=%d, folder_uuid=%Q, thumbnail_path=%Q, storage_type=%d, \
718 played_count=0, last_played_time=0, last_played_position=0 WHERE path=%Q;",
719 MEDIA_SVC_DB_TABLE_MEDIA, dest_path, file_name, modified_time, folder_uuid, thumb_path, dest_storage, src_path);
721 sql = sqlite3_mprintf("UPDATE '%s' SET path=%Q, file_name=%Q, modified_time=%d, folder_uuid=%Q, storage_type=%d, \
722 played_count=0, last_played_time=0, last_played_position=0 WHERE path=%Q;",
723 MEDIA_SVC_DB_TABLE_MEDIA, dest_path, file_name, modified_time, folder_uuid, dest_storage, src_path);
727 ret = _media_svc_sql_query(sql, uid);
728 SQLITE3_SAFE_FREE(sql);
730 _media_svc_sql_query_add(&g_media_svc_move_item_query_list, &sql);
736 int _media_svc_list_query_do(media_svc_query_type_e query_type, uid_t uid)
738 int ret = MS_MEDIA_ERR_NONE;
740 ret = _media_svc_sql_begin_trans(uid);
741 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
743 if (query_type == MEDIA_SVC_QUERY_SET_ITEM_VALIDITY)
744 ret = _media_svc_sql_query_list(&g_media_svc_item_validity_query_list, uid);
745 else if (query_type == MEDIA_SVC_QUERY_MOVE_ITEM)
746 ret = _media_svc_sql_query_list(&g_media_svc_move_item_query_list, uid);
747 else if (query_type == MEDIA_SVC_QUERY_INSERT_ITEM)
748 ret = _media_svc_sql_query_list(&g_media_svc_insert_item_query_list, uid);
749 else if (query_type == MEDIA_SVC_QUERY_UPDATE_ITEM)
750 ret = _media_svc_sql_query_list(&g_media_svc_update_item_query_list, uid);
751 else if (query_type == MEDIA_SVC_QUERY_INSERT_FOLDER)
752 ret = _media_svc_sql_query_list(_media_svc_get_folder_list_ptr(), uid);
753 else if (query_type == MEDIA_SVC_QUERY_UPDATE_COMMON)
754 ret = _media_svc_sql_query_list(&g_media_svc_update_list, uid);
756 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
758 if (ret != MS_MEDIA_ERR_NONE) {
759 media_svc_error("_media_svc_list_query_do failed. start rollback");
760 _media_svc_sql_rollback_trans(uid);
764 ret = _media_svc_sql_end_trans(uid);
765 if (ret != MS_MEDIA_ERR_NONE) {
766 media_svc_error("mb_svc_sqlite3_commit_trans failed.. Now start to rollback");
767 _media_svc_sql_rollback_trans(uid);
771 return MS_MEDIA_ERR_NONE;
774 int _media_svc_append_query_list(const char *query, uid_t uid)
776 int ret = MS_MEDIA_ERR_NONE;
778 g_media_svc_update_list = g_list_append(g_media_svc_update_list, (gpointer)query);
780 g_media_svc_update_list_count++;
782 if (g_media_svc_update_list_count >= MEDIA_SVC_MAX_COMMIT_SIZE) {
783 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_COMMON, uid);
784 g_media_svc_update_list_count = 0;
790 int _media_svc_get_burst_id(sqlite3 *handle, const char *storage_id, int *id)
792 int ret = MS_MEDIA_ERR_NONE;
794 sqlite3_stmt *sql_stmt = NULL;
795 char *sql = sqlite3_mprintf("SELECT max(CAST(burst_id AS INTEGER)) FROM '%s'", storage_id);
797 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
799 if (ret != MS_MEDIA_ERR_NONE) {
800 media_svc_error("error when _media_svc_get_burst_id. err = [%d]", ret);
804 cur_id = sqlite3_column_int(sql_stmt, 0);
806 SQLITE3_FINALIZE(sql_stmt);
808 return MS_MEDIA_ERR_NONE;
811 int _media_svc_get_noti_info(sqlite3 *handle, const char *storage_id, const char *path, int update_item, media_svc_noti_item **item)
813 int ret = MS_MEDIA_ERR_NONE;
814 sqlite3_stmt *sql_stmt = NULL;
816 int is_root_dir = FALSE;
819 media_svc_error("invalid parameter");
820 return MS_MEDIA_ERR_INVALID_PARAMETER;
823 if (update_item == MS_MEDIA_ITEM_FILE)
824 sql = sqlite3_mprintf("SELECT media_uuid, media_type, mime_type FROM '%s' WHERE path=%Q", storage_id, path);
825 else if (update_item == MS_MEDIA_ITEM_DIRECTORY)
826 sql = sqlite3_mprintf("SELECT folder_uuid FROM '%s' WHERE path=%Q AND storage_uuid='%s'", MEDIA_SVC_DB_TABLE_FOLDER, path, storage_id);
828 media_svc_error("_media_svc_get_noti_info failed : update item");
829 return MS_MEDIA_ERR_INVALID_PARAMETER;
832 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
834 if (ret != MS_MEDIA_ERR_NONE) {
835 if (ret == MS_MEDIA_ERR_DB_NO_RECORD && update_item == MS_MEDIA_ITEM_DIRECTORY) {
836 media_svc_debug("This is root directory of media");
840 media_svc_error("error when _media_svc_get_noti_info. err = [%d]", ret);
845 *item = calloc(1, sizeof(media_svc_noti_item));
847 media_svc_error("_media_svc_get_noti_info failed : calloc");
848 return MS_MEDIA_ERR_OUT_OF_MEMORY;
851 if (update_item == MS_MEDIA_ITEM_FILE) {
852 (*item)->media_uuid = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
853 (*item)->media_type = sqlite3_column_int(sql_stmt, 1);
854 (*item)->mime_type = g_strdup((const char *)sqlite3_column_text(sql_stmt, 2));
855 } else if (update_item == MS_MEDIA_ITEM_DIRECTORY) {
857 (*item)->media_uuid = NULL;
859 (*item)->media_uuid = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
862 SQLITE3_FINALIZE(sql_stmt);
864 return MS_MEDIA_ERR_NONE;
867 int _media_svc_count_invalid_folder_items(sqlite3 *handle, const char *storage_id, const char *folder_path, int *count)
869 int ret = MS_MEDIA_ERR_NONE;
870 sqlite3_stmt *sql_stmt = NULL;
872 char *sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE validity=0 AND path LIKE '%q/%%'", storage_id, folder_path);
874 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
876 if (ret != MS_MEDIA_ERR_NONE) {
877 media_svc_error("error when _media_svc_count_invalid_folder_items. err = [%d]", ret);
881 *count = sqlite3_column_int(sql_stmt, 0);
883 SQLITE3_FINALIZE(sql_stmt);
885 return MS_MEDIA_ERR_NONE;
888 int _media_svc_get_fileinfo_by_path(sqlite3 *handle, const char *storage_id, const char *path, time_t *modified_time, unsigned long long *size)
890 int ret = MS_MEDIA_ERR_NONE;
891 sqlite3_stmt *sql_stmt = NULL;
892 char *sql = sqlite3_mprintf("SELECT modified_time, size FROM '%s' WHERE path='%q'", storage_id, path);
894 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
896 if (ret != MS_MEDIA_ERR_NONE) {
897 media_svc_error("error when _media_svc_get_fileinfo_by_path. err = [%d]", ret);
901 *modified_time = (int)sqlite3_column_int(sql_stmt, 0);
902 *size = (unsigned long long)sqlite3_column_int64(sql_stmt, 1);
904 SQLITE3_FINALIZE(sql_stmt);
906 return MS_MEDIA_ERR_NONE;
909 int _media_svc_change_validity_item_batch(const char *storage_id, const char *path, int des_validity, int src_validity, uid_t uid)
911 int ret = MS_MEDIA_ERR_NONE;
913 char *sql = sqlite3_mprintf("UPDATE '%s' SET validity=%d WHERE validity=%d AND path LIKE '%q%%'", storage_id, des_validity, src_validity, path);
915 ret = _media_svc_sql_query(sql, uid);
916 SQLITE3_SAFE_FREE(sql);
921 int _media_svc_insert_item_pass1(sqlite3 *handle, const char *storage_id, media_svc_content_info_s *content_info, int is_burst, bool stack_query, uid_t uid)
923 int ret = MS_MEDIA_ERR_NONE;
924 char *burst_id = NULL;
926 char * db_fields = (char *)"media_uuid, folder_uuid, path, file_name, media_type, mime_type, size, added_time, modified_time, is_drm, storage_type, timeline, burst_id, storage_uuid";
928 char *sql = sqlite3_mprintf("INSERT INTO '%s' (%s) VALUES (%Q, %Q, %Q, %Q, %d, %Q, %lld, %d, %d, %d, %d, %d, %Q, %Q);",
929 storage_id, db_fields,
930 content_info->media_uuid,
931 content_info->folder_uuid,
933 content_info->file_name,
934 content_info->media_type,
935 content_info->mime_type,
937 content_info->added_time,
938 content_info->modified_time,
939 content_info->is_drm,
940 content_info->storage_type,
941 content_info->timeline,
946 SQLITE3_SAFE_FREE(burst_id);
948 media_svc_debug("MAKE PASS 1 QUERY END");
951 ret = _media_svc_sql_query(sql, uid);
952 SQLITE3_SAFE_FREE(sql);
953 if (ret != MS_MEDIA_ERR_NONE) {
954 media_svc_error("failed to insert item");
958 media_svc_debug("query : %s", sql);
959 _media_svc_sql_query_add(&g_media_svc_insert_item_query_list, &sql);
961 media_svc_debug("END");
962 return MS_MEDIA_ERR_NONE;
965 int _media_svc_insert_item_pass2(const char *storage_id, media_svc_content_info_s *content_info, int is_burst, bool stack_query, uid_t uid)
967 int ret = MS_MEDIA_ERR_NONE;
969 media_svc_debug_fenter();
971 /*Update Pinyin If Support Pinyin*/
972 if (_media_svc_check_pinyin_support()) {
973 if (STRING_VALID(content_info->file_name))
974 _media_svc_get_pinyin_str(content_info->file_name, &content_info->file_name_pinyin);
975 if (STRING_VALID(content_info->media_meta.title))
976 _media_svc_get_pinyin_str(content_info->media_meta.title, &content_info->media_meta.title_pinyin);
977 if (STRING_VALID(content_info->media_meta.album))
978 _media_svc_get_pinyin_str(content_info->media_meta.album, &content_info->media_meta.album_pinyin);
979 if (STRING_VALID(content_info->media_meta.artist))
980 _media_svc_get_pinyin_str(content_info->media_meta.artist, &content_info->media_meta.artist_pinyin);
981 if (STRING_VALID(content_info->media_meta.album_artist))
982 _media_svc_get_pinyin_str(content_info->media_meta.album_artist, &content_info->media_meta.album_artist_pinyin);
983 if (STRING_VALID(content_info->media_meta.genre))
984 _media_svc_get_pinyin_str(content_info->media_meta.genre, &content_info->media_meta.genre_pinyin);
985 if (STRING_VALID(content_info->media_meta.composer))
986 _media_svc_get_pinyin_str(content_info->media_meta.composer, &content_info->media_meta.composer_pinyin);
987 if (STRING_VALID(content_info->media_meta.copyright))
988 _media_svc_get_pinyin_str(content_info->media_meta.copyright, &content_info->media_meta.copyright_pinyin);
989 if (STRING_VALID(content_info->media_meta.description))
990 _media_svc_get_pinyin_str(content_info->media_meta.description, &content_info->media_meta.description_pinyin);
993 char *sql = sqlite3_mprintf("UPDATE '%s' SET \
994 thumbnail_path=%Q, title=%Q, album_id=%d, album=%Q, artist=%Q, album_artist=%Q, genre=%Q, composer=%Q, year=%Q, \
995 recorded_date=%Q, copyright=%Q, track_num=%Q, description=%Q, bitrate=%d, bitpersample=%d, samplerate=%d, channel=%d, \
996 duration=%d, longitude=%.6f, latitude=%.6f, altitude=%.6f, width=%d, height=%d, datetaken=%Q, orientation=%d, exposure_time=%Q,\
997 fnumber=%.6f, iso=%d, model=%Q, rating=%d, weather=%Q, file_name_pinyin=%Q, title_pinyin=%Q, album_pinyin=%Q, \
998 artist_pinyin=%Q, album_artist_pinyin=%Q, genre_pinyin=%Q, composer_pinyin=%Q, copyright_pinyin=%Q, description_pinyin=%Q, is_360=%d WHERE path=%Q;",
1000 /* content_info->folder_uuid, */
1001 content_info->thumbnail_path,
1002 content_info->media_meta.title,
1003 content_info->album_id,
1004 content_info->media_meta.album,
1005 content_info->media_meta.artist,
1006 content_info->media_meta.album_artist,
1007 content_info->media_meta.genre,
1008 content_info->media_meta.composer,
1009 content_info->media_meta.year,
1010 content_info->media_meta.recorded_date,
1011 content_info->media_meta.copyright,
1012 content_info->media_meta.track_num,
1013 content_info->media_meta.description,
1014 content_info->media_meta.bitrate,
1015 content_info->media_meta.bitpersample,
1016 content_info->media_meta.samplerate,
1017 content_info->media_meta.channel,
1018 content_info->media_meta.duration,
1019 content_info->media_meta.longitude,
1020 content_info->media_meta.latitude,
1021 content_info->media_meta.altitude,
1022 content_info->media_meta.width,
1023 content_info->media_meta.height,
1024 content_info->media_meta.datetaken,
1025 content_info->media_meta.orientation,
1026 content_info->media_meta.exposure_time,
1027 content_info->media_meta.fnumber,
1028 content_info->media_meta.iso,
1029 content_info->media_meta.model,
1030 content_info->media_meta.rating,
1031 content_info->media_meta.weather,
1032 content_info->file_name_pinyin,
1033 content_info->media_meta.title_pinyin,
1034 content_info->media_meta.album_pinyin,
1035 content_info->media_meta.artist_pinyin,
1036 content_info->media_meta.album_artist_pinyin,
1037 content_info->media_meta.genre_pinyin,
1038 content_info->media_meta.composer_pinyin,
1039 content_info->media_meta.copyright_pinyin,
1040 content_info->media_meta.description_pinyin,
1041 content_info->media_meta.is_360,
1046 ret = _media_svc_sql_query(sql, uid);
1047 SQLITE3_SAFE_FREE(sql);
1048 if (ret != MS_MEDIA_ERR_NONE) {
1050 media_svc_error("failed to insert item");
1054 /*media_svc_debug("query : %s", sql);*/
1055 _media_svc_sql_query_add(&g_media_svc_update_item_query_list, &sql);
1058 media_svc_debug_fleave();
1060 return MS_MEDIA_ERR_NONE;