4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
22 #include <sys/types.h>
23 #include <media-util-err.h>
24 #include "media-svc-media.h"
25 #include "media-svc-media-folder.h"
26 #include "media-svc-debug.h"
27 #include "media-svc-util.h"
28 #include "media-svc-db-utils.h"
29 #include "media-svc-noti.h"
31 #define MEDIA_SVC_MAX_COMMIT_SIZE 200
33 static __thread GList *g_media_svc_scanner_query_list = NULL;
34 static __thread GList *g_media_svc_update_list = NULL;
36 int _media_svc_check_data_by_path(sqlite3 *handle, const char *path)
38 int ret = MS_MEDIA_ERR_NONE;
39 sqlite3_stmt *sql_stmt = NULL;
42 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
44 sql = sqlite3_mprintf("SELECT 1 FROM %q WHERE media_path=%Q", DB_TABLE_MEDIA, path);
45 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
46 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
47 SQLITE3_FINALIZE(sql_stmt);
52 int _media_svc_get_modified_time(sqlite3 *handle, const char *path, int *modified_time)
54 int ret = MS_MEDIA_ERR_NONE;
55 sqlite3_stmt *sql_stmt = NULL;
58 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
60 sql = sqlite3_mprintf("SELECT media_modified_time FROM %q WHERE media_path=%Q", DB_TABLE_MEDIA, path);
61 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
62 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
64 *modified_time = sqlite3_column_int(sql_stmt, 0);
66 SQLITE3_FINALIZE(sql_stmt);
68 return MS_MEDIA_ERR_NONE;
71 int _media_svc_insert_item_with_data(bool is_direct, media_svc_content_info_s *content_info, bool stack_query, uid_t uid)
73 int ret = MS_MEDIA_ERR_NONE;
75 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, media_title, album_id, media_album, media_artist, media_album_artist, media_genre, media_year, media_track_num, media_width, media_height, media_datetaken, media_orientation, storage_uuid";
77 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, album_id, storage_uuid";
79 char *sql = sqlite3_mprintf("INSERT INTO %q(%q) VALUES (%Q, %Q, %Q, %d, %Q, %lld, %d, %d, %lld, %Q, %Q, %d, %Q, %Q, %Q, %Q, %Q, %Q, %d, %d, %Q, %d, %Q);",
80 DB_TABLE_MEDIA, db_fields,
81 content_info->media_uuid,
83 content_info->file_name,
84 content_info->media_type,
85 content_info->mime_type,
87 content_info->added_time,
88 content_info->modified_time,
89 content_info->folder_id,
90 content_info->thumbnail_path,
91 content_info->media_meta.title,
92 content_info->album_id,
93 content_info->media_meta.album,
94 content_info->media_meta.artist,
95 content_info->media_meta.album_artist,
96 content_info->media_meta.genre,
97 content_info->media_meta.year,
98 content_info->media_meta.track_num,
99 content_info->media_meta.width,
100 content_info->media_meta.height,
101 content_info->media_meta.datetaken,
102 content_info->media_meta.orientation,
103 content_info->storage_uuid);
105 /* Check query length */
106 if (sql != NULL && strlen(sql) >= (MEDIA_SVC_QUERY_LEN_MAX - 1) && is_direct == false) {
107 media_svc_debug("Query length is over %d byte.. So, remake query using only basic information..", MEDIA_SVC_QUERY_LEN_MAX - 1);
108 SQLITE3_SAFE_FREE(sql);
110 sql = sqlite3_mprintf("INSERT INTO %q(%q) VALUES (%Q, %Q, %Q, %d, %Q, %lld, %d, %d, %lld, %d, %Q);",
111 DB_TABLE_MEDIA, db_fields_basic,
112 content_info->media_uuid,
114 content_info->file_name,
115 content_info->media_type,
116 content_info->mime_type,
118 content_info->added_time,
119 content_info->modified_time,
120 content_info->folder_id,
121 content_info->album_id,
122 content_info->storage_uuid
128 ret = _media_svc_sql_query_direct(sql, uid);
130 ret = _media_svc_sql_query(sql, uid);
132 SQLITE3_SAFE_FREE(sql);
133 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "failed to insert item");
135 media_svc_sec_debug("Query[%s]", sql);
136 _media_svc_sql_query_add(&g_media_svc_scanner_query_list, &sql);
139 return MS_MEDIA_ERR_NONE;
142 int _media_svc_update_item_with_data(bool is_direct, media_svc_content_info_s *content_info, uid_t uid)
144 int ret = MS_MEDIA_ERR_NONE;
146 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, media_year=%Q, media_track_num=%Q, media_width=%d, media_height=%d, media_datetaken=%Q, media_orientation=%d, validity=1 WHERE media_path=%Q;",
149 content_info->modified_time,
150 content_info->thumbnail_path,
151 content_info->media_meta.title,
152 content_info->album_id,
153 content_info->media_meta.album,
154 content_info->media_meta.artist,
155 content_info->media_meta.album_artist,
156 content_info->media_meta.genre,
157 content_info->media_meta.year,
158 content_info->media_meta.track_num,
159 content_info->media_meta.width,
160 content_info->media_meta.height,
161 content_info->media_meta.datetaken,
162 content_info->media_meta.orientation,
166 /* Scanner use only batch insert */
168 media_svc_sec_debug("Query [%s]", sql);
169 _media_svc_sql_query_add(&g_media_svc_scanner_query_list, &sql);
171 ret = _media_svc_sql_query(sql, uid);
172 SQLITE3_SAFE_FREE(sql);
177 int _media_svc_get_thumbnail_path_by_path(sqlite3 *handle, const char *path, char *thumbnail_path)
179 int ret = MS_MEDIA_ERR_NONE;
180 sqlite3_stmt *sql_stmt = NULL;
183 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
185 sql = sqlite3_mprintf("SELECT media_thumbnail_path FROM %q WHERE media_path='%q'", DB_TABLE_MEDIA, path);
187 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
189 if (ret != MS_MEDIA_ERR_NONE) {
190 if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
191 media_svc_debug("there is no thumbnail.");
193 media_svc_error("error when _media_svc_get_thumbnail_path_by_path. err = [%d]", ret);
198 SAFE_STRLCPY(thumbnail_path, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_PATHNAME_SIZE);
200 SQLITE3_FINALIZE(sql_stmt);
202 return MS_MEDIA_ERR_NONE;
205 int _media_svc_delete_item_by_path(const char *path, uid_t uid)
207 int ret = MS_MEDIA_ERR_NONE;
210 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
212 sql = sqlite3_mprintf("DELETE FROM %q WHERE media_path=%Q;", DB_TABLE_MEDIA, path);
214 ret = _media_svc_sql_query(sql, uid);
215 SQLITE3_SAFE_FREE(sql);
216 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "failed to delete item");
221 int _media_svc_update_item_validity(const char *path, int validity, bool stack_query, uid_t uid)
223 int ret = MS_MEDIA_ERR_NONE;
226 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
228 media_svc_debug("path=[%s], validity=[%d]", path, validity);
230 sql = sqlite3_mprintf("UPDATE %q SET validity=%d WHERE media_path='%q';", DB_TABLE_MEDIA, validity, path);
233 ret = _media_svc_sql_query_direct(sql, uid);
234 SQLITE3_SAFE_FREE(sql);
236 _media_svc_sql_query_add(&g_media_svc_scanner_query_list, &sql);
242 int _media_svc_update_thumbnail_path(const char *path, const char *thumb_path, uid_t uid)
244 int ret = MS_MEDIA_ERR_NONE;
246 char *sql = sqlite3_mprintf("UPDATE %q SET media_thumbnail_path=%Q WHERE media_path=%Q;", DB_TABLE_MEDIA, thumb_path, path);
248 ret = _media_svc_sql_query(sql, uid);
249 SQLITE3_SAFE_FREE(sql);
254 int _media_svc_update_item_by_path(const char *src_path, const char *dst_storage_id, const char *dest_path, const char *file_name, int modified_time, long long int folder_id, uid_t uid)
256 int ret = MS_MEDIA_ERR_NONE;
258 char *query = sqlite3_mprintf("UPDATE %q SET media_path=%Q, media_display_name=%Q, media_modified_time=%d, folder_id=%lld, storage_uuid='%q' WHERE media_path=%Q;",
259 DB_TABLE_MEDIA, dest_path, file_name, modified_time, folder_id, dst_storage_id, src_path);
261 ret = _media_svc_sql_query(query, uid);
262 SQLITE3_SAFE_FREE(query);
267 int _media_svc_list_query_do(media_svc_query_type_e query_type, uid_t uid)
269 int ret = MS_MEDIA_ERR_NONE;
271 /* For multiples of 200, empty requests are possible */
272 switch (query_type) {
273 case MEDIA_SVC_QUERY_SCANNER:
274 ret = _media_svc_sql_query_list_direct(&g_media_svc_scanner_query_list, uid);
276 case MEDIA_SVC_QUERY_UPDATE_COMMON:
277 if (g_media_svc_update_list == NULL || g_list_length(g_media_svc_update_list) == 0)
278 return MS_MEDIA_ERR_NONE;
280 ret = _media_svc_sql_query_list(&g_media_svc_update_list, uid);
283 media_svc_error("Wrong type[%d]", query_type);
284 return MS_MEDIA_ERR_INVALID_PARAMETER;
287 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Failed to request queries");
289 return MS_MEDIA_ERR_NONE;
292 int _media_svc_append_query_list(const char *query, uid_t uid)
294 int ret = MS_MEDIA_ERR_NONE;
296 media_svc_retvm_if(query == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "query is NULL");
298 g_media_svc_update_list = g_list_append(g_media_svc_update_list, (gpointer)query);
300 if (g_list_length(g_media_svc_update_list) >= MEDIA_SVC_MAX_COMMIT_SIZE)
301 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_COMMON, uid);
306 int _media_svc_get_media(sqlite3 *handle, const char *sql, GList **path_list)
308 int ret = MS_MEDIA_ERR_NONE;
309 sqlite3_stmt *sql_stmt = NULL;
311 media_svc_retvm_if(!sql, MS_MEDIA_ERR_INVALID_PARAMETER, "query is NULL");
312 media_svc_retvm_if(!path_list, MS_MEDIA_ERR_INVALID_PARAMETER, "array is NULL");
314 ret = _media_svc_sql_prepare_to_step_simple(handle, sql, &sql_stmt);
315 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_sql_prepare_to_step_simple() failed [%d]", ret);
317 while (sqlite3_step(sql_stmt) == SQLITE_ROW)
318 *path_list = g_list_append(*path_list, g_strdup((const char *)sqlite3_column_text(sql_stmt, 0)));
320 SQLITE3_FINALIZE(sql_stmt);
325 int _media_svc_get_noti_info(sqlite3 *handle, const char *path, media_svc_noti_item **item)
327 int ret = MS_MEDIA_ERR_NONE;
328 sqlite3_stmt *sql_stmt = NULL;
331 media_svc_retvm_if(item == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "item is NULL");
332 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
334 sql = sqlite3_mprintf("SELECT media_id, media_type, media_mime_type FROM %q WHERE media_path=%Q", DB_TABLE_MEDIA, path);
335 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
336 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_sql_prepare_to_step() failed [%d]", ret);
338 *item = g_new0(media_svc_noti_item, 1);
340 (*item)->media_uuid = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
341 (*item)->media_type = sqlite3_column_int(sql_stmt, 1);
342 (*item)->mime_type = g_strdup((const char *)sqlite3_column_text(sql_stmt, 2));
344 SQLITE3_FINALIZE(sql_stmt);
346 return MS_MEDIA_ERR_NONE;