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_scanner_query_list = NULL;
36 static __thread GList *g_media_svc_update_list = NULL;
38 int _media_svc_check_data_by_path(sqlite3 *handle, const char *path)
40 int ret = MS_MEDIA_ERR_NONE;
41 sqlite3_stmt *sql_stmt = NULL;
44 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
46 sql = sqlite3_mprintf("SELECT 1 FROM %q WHERE media_path=%Q", DB_TABLE_MEDIA, path);
47 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
48 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
49 SQLITE3_FINALIZE(sql_stmt);
54 int _media_svc_get_modified_time(sqlite3 *handle, const char *path, int *modified_time)
56 int ret = MS_MEDIA_ERR_NONE;
57 sqlite3_stmt *sql_stmt = NULL;
60 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
62 sql = sqlite3_mprintf("SELECT media_modified_time FROM %q WHERE media_path=%Q", DB_TABLE_MEDIA, path);
63 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
64 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
66 *modified_time = sqlite3_column_int(sql_stmt, 0);
68 SQLITE3_FINALIZE(sql_stmt);
70 return MS_MEDIA_ERR_NONE;
73 int _media_svc_insert_item_with_data(bool is_direct, media_svc_content_info_s *content_info, bool stack_query, uid_t uid)
75 int ret = MS_MEDIA_ERR_NONE;
77 const char *db_fields = "media_id, media_path, media_display_name, media_type, media_mime_type, media_size, media_added_time, media_modified_time, folder_id, media_thumbnail_path, \
78 media_title, album_id, media_album, media_artist, media_album_artist, media_genre, media_composer, media_year, media_recorded_date, media_copyright, media_track_num, media_description, \
79 media_favourite, media_bitrate, media_bitpersample, media_samplerate, media_channel, media_duration, \
80 media_longitude, media_latitude, media_altitude, exposure_time, fnumber, iso, model, media_width, media_height, media_datetaken, media_orientation, media_rating, media_is_drm, media_storage_type, media_timeline, \
81 media_file_name_pinyin, media_title_pinyin, media_album_pinyin, media_artist_pinyin, media_album_artist_pinyin, media_genre_pinyin, media_composer_pinyin, media_copyright_pinyin, media_description_pinyin, storage_uuid, media_360";
83 const char *db_fields_basic = "media_id, media_path, media_display_name, media_type, media_mime_type, media_size, media_added_time, media_modified_time, folder_id, media_thumbnail_path, \
84 album_id, media_favourite, media_is_drm, media_storage_type, media_timeline, media_file_name_pinyin, storage_uuid, media_360";
86 /* This sql is due to sqlite3_mprintf's wrong operation when using floating point in the text format */
87 /* This code will be removed when sqlite3_mprintf works clearly */
88 char *test_sql = sqlite3_mprintf("%f, %f, %f", content_info->media_meta.longitude, content_info->media_meta.latitude, content_info->media_meta.altitude);
89 SQLITE3_SAFE_FREE(test_sql);
91 /*Update Pinyin If Support Pinyin*/
92 if (_media_svc_check_pinyin_support()) {
93 if (STRING_VALID(content_info->file_name))
94 _media_svc_get_pinyin_str(content_info->file_name, &content_info->file_name_pinyin);
95 if (STRING_VALID(content_info->media_meta.title))
96 _media_svc_get_pinyin_str(content_info->media_meta.title, &content_info->media_meta.title_pinyin);
97 if (STRING_VALID(content_info->media_meta.album))
98 _media_svc_get_pinyin_str(content_info->media_meta.album, &content_info->media_meta.album_pinyin);
99 if (STRING_VALID(content_info->media_meta.artist))
100 _media_svc_get_pinyin_str(content_info->media_meta.artist, &content_info->media_meta.artist_pinyin);
101 if (STRING_VALID(content_info->media_meta.album_artist))
102 _media_svc_get_pinyin_str(content_info->media_meta.album_artist, &content_info->media_meta.album_artist_pinyin);
103 if (STRING_VALID(content_info->media_meta.genre))
104 _media_svc_get_pinyin_str(content_info->media_meta.genre, &content_info->media_meta.genre_pinyin);
105 if (STRING_VALID(content_info->media_meta.composer))
106 _media_svc_get_pinyin_str(content_info->media_meta.composer, &content_info->media_meta.composer_pinyin);
107 if (STRING_VALID(content_info->media_meta.copyright))
108 _media_svc_get_pinyin_str(content_info->media_meta.copyright, &content_info->media_meta.copyright_pinyin);
109 if (STRING_VALID(content_info->media_meta.description))
110 _media_svc_get_pinyin_str(content_info->media_meta.description, &content_info->media_meta.description_pinyin);
113 char *sql = sqlite3_mprintf("INSERT INTO %q(%q) VALUES (%Q, %Q, %Q, %d, %Q, %lld, %d, %d, %Q, \
114 %Q, %Q, %d, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %d, \
115 %d, %d, %d, %d, %d, %.6f, %.6f, %.6f, %Q, %.6f, %d, %Q, %d, %d, %Q, %d, \
116 %d, %d, %d, %d, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %d);",
117 DB_TABLE_MEDIA, db_fields,
118 content_info->media_uuid,
120 content_info->file_name,
121 content_info->media_type,
122 content_info->mime_type,
124 content_info->added_time,
125 content_info->modified_time,
126 content_info->folder_uuid, /* 1st line */
127 content_info->thumbnail_path,
128 content_info->media_meta.title,
129 content_info->album_id,
130 content_info->media_meta.album,
131 content_info->media_meta.artist,
132 content_info->media_meta.album_artist,
133 content_info->media_meta.genre,
134 content_info->media_meta.composer,
135 content_info->media_meta.year,
136 content_info->media_meta.recorded_date,
137 content_info->media_meta.copyright,
138 content_info->media_meta.track_num,
139 content_info->media_meta.description,
140 content_info->favourate, /* 2nd line */
141 content_info->media_meta.bitrate,
142 content_info->media_meta.bitpersample,
143 content_info->media_meta.samplerate,
144 content_info->media_meta.channel,
145 content_info->media_meta.duration,
146 content_info->media_meta.longitude,
147 content_info->media_meta.latitude,
148 content_info->media_meta.altitude,
149 content_info->media_meta.exposure_time,
150 content_info->media_meta.fnumber,
151 content_info->media_meta.iso,
152 content_info->media_meta.model,
153 content_info->media_meta.width,
154 content_info->media_meta.height,
155 content_info->media_meta.datetaken,
156 content_info->media_meta.orientation, /* 3rd line */
157 content_info->media_meta.rating,
158 content_info->is_drm,
159 content_info->storage_type,
160 content_info->timeline,
161 content_info->file_name_pinyin,
162 content_info->media_meta.title_pinyin,
163 content_info->media_meta.album_pinyin,
164 content_info->media_meta.artist_pinyin,
165 content_info->media_meta.album_artist_pinyin,
166 content_info->media_meta.genre_pinyin,
167 content_info->media_meta.composer_pinyin,
168 content_info->media_meta.copyright_pinyin,
169 content_info->media_meta.description_pinyin,
170 content_info->storage_uuid,
171 content_info->media_meta.is_360 /* 4th line */
174 /* Check query length */
175 if (sql != NULL && strlen(sql) >= (MEDIA_SVC_QUERY_LEN_MAX - 1) && is_direct == false) {
176 media_svc_debug("Query length is over %d byte.. So, remake query using only basic information..", MEDIA_SVC_QUERY_LEN_MAX - 1);
177 SQLITE3_SAFE_FREE(sql);
179 sql = sqlite3_mprintf("INSERT INTO %q(%q) VALUES (%Q, %Q, %Q, %d, %Q, %lld, %d, %d, %Q, %Q, %d, %d, %d, %d, %d, %d, %d, %Q, %Q, %d);",
180 DB_TABLE_MEDIA, db_fields_basic,
181 content_info->media_uuid,
183 content_info->file_name,
184 content_info->media_type,
185 content_info->mime_type,
187 content_info->added_time,
188 content_info->modified_time,
189 content_info->folder_uuid,
190 content_info->thumbnail_path,
191 content_info->album_id,
192 content_info->favourate,
193 content_info->is_drm,
194 content_info->storage_type,
195 content_info->timeline,
196 content_info->file_name_pinyin,
197 content_info->storage_uuid,
198 content_info->media_meta.is_360
204 ret = _media_svc_sql_query_direct(sql, uid);
206 ret = _media_svc_sql_query(sql, uid);
208 SQLITE3_SAFE_FREE(sql);
209 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "failed to insert item");
211 media_svc_sec_debug("Query[%s]", sql);
212 _media_svc_sql_query_add(&g_media_svc_scanner_query_list, &sql);
215 return MS_MEDIA_ERR_NONE;
218 int _media_svc_update_meta_with_data(media_svc_content_info_s *content_info)
220 int ret = MS_MEDIA_ERR_NONE;
221 media_svc_retvm_if(!content_info, MS_MEDIA_ERR_INTERNAL, "content_info is NULL");
223 /* This sql is due to sqlite3_mprintf's wrong operation when using floating point in the text format */
224 /* This code will be removed when sqlite3_mprintf works clearly */
225 char *test_sql = sqlite3_mprintf("%f, %f, %f", content_info->media_meta.longitude, content_info->media_meta.latitude, content_info->media_meta.altitude);
226 SQLITE3_SAFE_FREE(test_sql);
228 char *sql = sqlite3_mprintf("UPDATE %q SET media_title=%Q, media_album=%Q, media_artist=%Q, media_album_artist=%Q, media_genre=%Q, media_composer=%Q, media_copyright=%Q, media_description=%Q WHERE media_path=%Q;",
230 content_info->media_meta.title,
231 content_info->media_meta.album,
232 content_info->media_meta.artist,
233 content_info->media_meta.album_artist,
234 content_info->media_meta.genre,
235 content_info->media_meta.composer,
236 content_info->media_meta.copyright,
237 content_info->media_meta.description,
242 media_svc_debug("query[%s]", sql);
243 _media_svc_sql_query_add(&g_media_svc_scanner_query_list, &sql);
245 media_svc_error("sqlite3_mprintf failed");
246 ret = MS_MEDIA_ERR_OUT_OF_MEMORY;
252 int _media_svc_update_item_with_data(bool is_direct, media_svc_content_info_s *content_info, uid_t uid)
254 int ret = MS_MEDIA_ERR_NONE;
256 /* This sql is due to sqlite3_mprintf's wrong operation when using floating point in the text format */
257 /* This code will be removed when sqlite3_mprintf works clearly */
258 char *test_sql = sqlite3_mprintf("%f, %f, %f", content_info->media_meta.longitude, content_info->media_meta.latitude, content_info->media_meta.altitude);
259 SQLITE3_SAFE_FREE(test_sql);
261 /*Update Pinyin If Support Pinyin*/
262 if (_media_svc_check_pinyin_support()) {
263 if (STRING_VALID(content_info->file_name))
264 _media_svc_get_pinyin_str(content_info->file_name, &content_info->file_name_pinyin);
265 if (STRING_VALID(content_info->media_meta.title))
266 _media_svc_get_pinyin_str(content_info->media_meta.title, &content_info->media_meta.title_pinyin);
267 if (STRING_VALID(content_info->media_meta.album))
268 _media_svc_get_pinyin_str(content_info->media_meta.album, &content_info->media_meta.album_pinyin);
269 if (STRING_VALID(content_info->media_meta.artist))
270 _media_svc_get_pinyin_str(content_info->media_meta.artist, &content_info->media_meta.artist_pinyin);
271 if (STRING_VALID(content_info->media_meta.album_artist))
272 _media_svc_get_pinyin_str(content_info->media_meta.album_artist, &content_info->media_meta.album_artist_pinyin);
273 if (STRING_VALID(content_info->media_meta.genre))
274 _media_svc_get_pinyin_str(content_info->media_meta.genre, &content_info->media_meta.genre_pinyin);
275 if (STRING_VALID(content_info->media_meta.composer))
276 _media_svc_get_pinyin_str(content_info->media_meta.composer, &content_info->media_meta.composer_pinyin);
277 if (STRING_VALID(content_info->media_meta.copyright))
278 _media_svc_get_pinyin_str(content_info->media_meta.copyright, &content_info->media_meta.copyright_pinyin);
279 if (STRING_VALID(content_info->media_meta.description))
280 _media_svc_get_pinyin_str(content_info->media_meta.description, &content_info->media_meta.description_pinyin);
283 char *sql = sqlite3_mprintf("UPDATE %q SET media_size=%lld, media_modified_time=%d, media_thumbnail_path=%Q, media_title=%Q, album_id=%d, media_album=%Q, media_artist=%Q, media_album_artist=%Q, media_genre=%Q, \
284 media_composer=%Q, media_year=%Q, media_recorded_date=%Q, media_copyright=%Q, media_track_num=%Q, media_description=%Q, media_bitrate=%d, media_bitpersample=%d, media_samplerate=%d, media_channel=%d, media_duration=%d, \
285 media_longitude=%f, media_latitude=%f, media_altitude=%f, exposure_time=%Q, fnumber=%f, iso=%d, model=%Q, media_width=%d, media_height=%d, media_datetaken=%Q, media_orientation=%d, validity=1 WHERE media_path=%Q;",
288 content_info->modified_time,
289 content_info->thumbnail_path,
290 content_info->media_meta.title,
291 content_info->album_id,
292 content_info->media_meta.album,
293 content_info->media_meta.artist,
294 content_info->media_meta.album_artist,
295 content_info->media_meta.genre,
296 content_info->media_meta.composer,
297 content_info->media_meta.year,
298 content_info->media_meta.recorded_date,
299 content_info->media_meta.copyright,
300 content_info->media_meta.track_num,
301 content_info->media_meta.description,
302 content_info->media_meta.bitrate,
303 content_info->media_meta.bitpersample,
304 content_info->media_meta.samplerate,
305 content_info->media_meta.channel,
306 content_info->media_meta.duration,
307 content_info->media_meta.longitude,
308 content_info->media_meta.latitude,
309 content_info->media_meta.altitude,
310 content_info->media_meta.exposure_time,
311 content_info->media_meta.fnumber,
312 content_info->media_meta.iso,
313 content_info->media_meta.model,
314 content_info->media_meta.width,
315 content_info->media_meta.height,
316 content_info->media_meta.datetaken,
317 content_info->media_meta.orientation,
321 /* Scanner use only batch insert */
323 media_svc_sec_debug("Query [%s]", sql);
324 _media_svc_sql_query_add(&g_media_svc_scanner_query_list, &sql);
326 ret = _media_svc_sql_query(sql, uid);
327 SQLITE3_SAFE_FREE(sql);
332 int _media_svc_get_thumbnail_path_by_path(sqlite3 *handle, const char *path, char *thumbnail_path)
334 int ret = MS_MEDIA_ERR_NONE;
335 sqlite3_stmt *sql_stmt = NULL;
338 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
340 sql = sqlite3_mprintf("SELECT media_thumbnail_path FROM %q WHERE media_path='%q'", DB_TABLE_MEDIA, path);
342 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
344 if (ret != MS_MEDIA_ERR_NONE) {
345 if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
346 media_svc_debug("there is no thumbnail.");
348 media_svc_error("error when _media_svc_get_thumbnail_path_by_path. err = [%d]", ret);
353 SAFE_STRLCPY(thumbnail_path, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_PATHNAME_SIZE);
355 SQLITE3_FINALIZE(sql_stmt);
357 return MS_MEDIA_ERR_NONE;
360 int _media_svc_delete_item_by_path(const char *path, uid_t uid)
362 int ret = MS_MEDIA_ERR_NONE;
365 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
367 sql = sqlite3_mprintf("DELETE FROM %q WHERE media_path=%Q;", DB_TABLE_MEDIA, path);
369 ret = _media_svc_sql_query(sql, uid);
370 SQLITE3_SAFE_FREE(sql);
371 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "failed to delete item");
376 int _media_svc_update_item_validity(const char *path, int validity, bool stack_query, uid_t uid)
378 int ret = MS_MEDIA_ERR_NONE;
381 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
383 media_svc_debug("path=[%s], validity=[%d]", path, validity);
385 sql = sqlite3_mprintf("UPDATE %q SET validity=%d WHERE media_path='%q';", DB_TABLE_MEDIA, validity, path);
388 ret = _media_svc_sql_query_direct(sql, uid);
389 SQLITE3_SAFE_FREE(sql);
391 _media_svc_sql_query_add(&g_media_svc_scanner_query_list, &sql);
397 int _media_svc_update_thumbnail_path(const char *path, const char *thumb_path, uid_t uid)
399 int ret = MS_MEDIA_ERR_NONE;
401 char *sql = sqlite3_mprintf("UPDATE %q SET media_thumbnail_path=%Q WHERE media_path=%Q;", DB_TABLE_MEDIA, thumb_path, path);
403 ret = _media_svc_sql_query(sql, uid);
404 SQLITE3_SAFE_FREE(sql);
409 int _media_svc_update_item_by_path(const char *src_path, const char *dst_storage_id, ms_user_storage_type_e dest_storage, const char *dest_path, const char *file_name, int modified_time, const char *folder_uuid, uid_t uid)
411 int ret = MS_MEDIA_ERR_NONE;
413 char *query = sqlite3_mprintf("UPDATE %q SET media_path=%Q, media_display_name=%Q, media_modified_time=%d, folder_id=%Q, media_storage_type=%d, storage_uuid='%q' WHERE media_path=%Q;",
414 DB_TABLE_MEDIA, dest_path, file_name, modified_time, folder_uuid, dest_storage, dst_storage_id, src_path);
416 ret = _media_svc_sql_query(query, uid);
417 SQLITE3_SAFE_FREE(query);
422 int _media_svc_list_query_do(media_svc_query_type_e query_type, uid_t uid)
424 int ret = MS_MEDIA_ERR_NONE;
426 /* For multiples of 200, empty requests are possible */
427 switch (query_type) {
428 case MEDIA_SVC_QUERY_SCANNER:
429 ret = _media_svc_sql_query_list_direct(&g_media_svc_scanner_query_list, uid);
431 case MEDIA_SVC_QUERY_UPDATE_COMMON:
432 if (g_media_svc_update_list == NULL || g_list_length(g_media_svc_update_list) == 0)
433 return MS_MEDIA_ERR_NONE;
435 ret = _media_svc_sql_query_list(&g_media_svc_update_list, uid);
438 media_svc_error("Wrong type[%d]", query_type);
439 return MS_MEDIA_ERR_INVALID_PARAMETER;
442 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Failed to request queries");
444 return MS_MEDIA_ERR_NONE;
447 int _media_svc_append_query_list(const char *query, uid_t uid)
449 int ret = MS_MEDIA_ERR_NONE;
451 media_svc_retvm_if(query == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "query is NULL");
453 g_media_svc_update_list = g_list_append(g_media_svc_update_list, (gpointer)query);
455 if (g_list_length(g_media_svc_update_list) >= MEDIA_SVC_MAX_COMMIT_SIZE)
456 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_COMMON, uid);
461 int _media_svc_get_media(sqlite3 *handle, const char *sql, GPtrArray **path_list)
463 int ret = MS_MEDIA_ERR_NONE;
464 sqlite3_stmt *sql_stmt = NULL;
467 media_svc_retvm_if(!sql, MS_MEDIA_ERR_INVALID_PARAMETER, "query is NULL");
468 media_svc_retvm_if(!path_list, MS_MEDIA_ERR_INVALID_PARAMETER, "array is NULL");
470 ret = _media_svc_sql_prepare_to_step_simple(handle, sql, &sql_stmt);
471 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_sql_prepare_to_step_simple() failed [%d]", ret);
472 while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
473 path = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
474 g_ptr_array_add(*path_list, path);
477 SQLITE3_FINALIZE(sql_stmt);
482 int _media_svc_get_noti_info(sqlite3 *handle, const char *path, media_svc_noti_item **item)
484 int ret = MS_MEDIA_ERR_NONE;
485 sqlite3_stmt *sql_stmt = NULL;
488 media_svc_retvm_if(item == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "item is NULL");
489 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
491 sql = sqlite3_mprintf("SELECT media_id, media_type, media_mime_type FROM %q WHERE media_path=%Q", DB_TABLE_MEDIA, path);
492 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
493 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_sql_prepare_to_step() failed [%d]", ret);
495 *item = g_new0(media_svc_noti_item, 1);
497 (*item)->media_uuid = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
498 (*item)->media_type = sqlite3_column_int(sql_stmt, 1);
499 (*item)->mime_type = g_strdup((const char *)sqlite3_column_text(sql_stmt, 2));
501 SQLITE3_FINALIZE(sql_stmt);
503 return MS_MEDIA_ERR_NONE;