4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Hyunjun Ko <zzoon.ko@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.
22 #include "media-thumb-db.h"
23 #include "media-thumb-util.h"
24 #include "media-thumb-debug.h"
30 static __thread MediaDBHandle *db_handle;
32 sqlite3 *_media_thumb_db_get_handle()
37 int _media_thumb_get_type_from_db(sqlite3 *handle,
38 const char *origin_path,
41 int err = MS_MEDIA_ERR_NONE;
42 char *path_string = NULL;
43 char *query_string = NULL;
44 sqlite3_stmt *stmt = NULL;
46 thumb_dbg_slog("Origin path : %s", origin_path);
49 thumb_err("DB handle is NULL");
50 return MS_MEDIA_ERR_INVALID_PARAMETER;
52 if (!STRING_VALID(origin_path)) {
53 thumb_err("Invalid origin_path");
54 return MS_MEDIA_ERR_INVALID_PARAMETER;
57 path_string = sqlite3_mprintf("%s", origin_path);
58 if (!STRING_VALID(path_string)) {
59 thumb_err("Memory allocation is failed");
60 return MS_MEDIA_ERR_OUT_OF_MEMORY;
62 query_string = sqlite3_mprintf(SELECT_TYPE_BY_PATH, path_string);
63 if (!STRING_VALID(query_string)) {
64 thumb_err("Memory allocation is failed");
65 sqlite3_free(path_string);
66 return MS_MEDIA_ERR_OUT_OF_MEMORY;
69 thumb_dbg("Query: %s", query_string);
71 err = sqlite3_prepare_v2(handle, query_string, strlen(query_string), &stmt, NULL);
73 sqlite3_free(query_string);
74 sqlite3_free(path_string);
76 if (SQLITE_OK != err) {
77 thumb_err("prepare error [%s]", sqlite3_errmsg(handle));
78 return MS_MEDIA_ERR_DB_INTERNAL;
81 err = sqlite3_step(stmt);
82 if (err != SQLITE_ROW) {
83 thumb_err("end of row [%s]", sqlite3_errmsg(handle));
84 sqlite3_finalize(stmt);
85 return MS_MEDIA_ERR_DB_NO_RECORD;
88 *type = sqlite3_column_int(stmt, 0);
89 sqlite3_finalize(stmt);
91 return MS_MEDIA_ERR_NONE;
95 int _media_thumb_get_wh_from_db(sqlite3 *handle,
96 const char *origin_path,
100 thumb_dbg_slog("Origin path : %s", origin_path);
102 if (handle == NULL) {
103 thumb_err("DB handle is NULL");
104 return MS_MEDIA_ERR_INVALID_PARAMETER;
106 if (!STRING_VALID(origin_path)) {
107 thumb_err("Invalid origin_path");
108 return MS_MEDIA_ERR_INVALID_PARAMETER;
111 int err = MS_MEDIA_ERR_NONE;
112 char *path_string = NULL;
113 char *query_string = NULL;
114 sqlite3_stmt *stmt = NULL;
116 path_string = sqlite3_mprintf("%s", origin_path);
117 if (!STRING_VALID(path_string)) {
118 thumb_err("Memory allocation is failed");
119 return MS_MEDIA_ERR_OUT_OF_MEMORY;
122 query_string = sqlite3_mprintf(SELECT_WH_BY_PATH, path_string);
123 if (!STRING_VALID(query_string)) {
124 thumb_err("Memory allocation is failed");
125 sqlite3_free(path_string);
126 return MS_MEDIA_ERR_OUT_OF_MEMORY;
129 thumb_dbg_slog("Query: %s", query_string);
131 err = sqlite3_prepare_v2(handle, query_string, strlen(query_string), &stmt, NULL);
133 sqlite3_free(query_string);
134 sqlite3_free(path_string);
136 if (SQLITE_OK != err) {
137 thumb_err("prepare error [%s]", sqlite3_errmsg(handle));
138 return MS_MEDIA_ERR_DB_INTERNAL;
141 err = sqlite3_step(stmt);
142 if (err != SQLITE_ROW) {
143 thumb_err("end of row [%s]", sqlite3_errmsg(handle));
144 sqlite3_finalize(stmt);
145 return MS_MEDIA_ERR_DB_INTERNAL;
148 *width = sqlite3_column_int(stmt, 0);
149 *height = sqlite3_column_int(stmt, 1);
150 sqlite3_finalize(stmt);
152 return MS_MEDIA_ERR_NONE;
155 int _media_thumb_get_thumb_path_from_db(sqlite3 *handle,
156 const char *origin_path,
160 thumb_dbg_slog("Origin path : %s", origin_path);
162 if (handle == NULL) {
163 thumb_err("DB handle is NULL");
164 return MS_MEDIA_ERR_INVALID_PARAMETER;
166 if (!STRING_VALID(origin_path)) {
167 thumb_err("Invalid origin_path");
168 return MS_MEDIA_ERR_INVALID_PARAMETER;
171 int err = MS_MEDIA_ERR_NONE;
172 char *path_string = NULL;
173 char *query_string = NULL;
174 sqlite3_stmt *stmt = NULL;
176 path_string = sqlite3_mprintf("%s", origin_path);
177 if (!STRING_VALID(path_string)) {
178 thumb_err("Memory allocation is failed");
179 return MS_MEDIA_ERR_OUT_OF_MEMORY;
182 query_string = sqlite3_mprintf(SELECT_MEDIA_BY_PATH, path_string);
183 if (!STRING_VALID(query_string)) {
184 thumb_err("Memory allocation is failed");
185 sqlite3_free(path_string);
186 return MS_MEDIA_ERR_OUT_OF_MEMORY;
189 thumb_dbg_slog("Query: %s", query_string);
191 err = sqlite3_prepare_v2(handle, query_string, strlen(query_string), &stmt, NULL);
193 sqlite3_free(query_string);
194 sqlite3_free(path_string);
196 if (SQLITE_OK != err) {
197 thumb_err("prepare error [%s]", sqlite3_errmsg(handle));
198 return MS_MEDIA_ERR_DB_INTERNAL;
201 err = sqlite3_step(stmt);
202 if (err != SQLITE_ROW) {
203 thumb_err("end of row [%s]", sqlite3_errmsg(handle));
204 sqlite3_finalize(stmt);
205 return MS_MEDIA_ERR_DB_INTERNAL;
208 if (sqlite3_column_text(stmt, 0))
209 strncpy(thumb_path, (const char *)sqlite3_column_text(stmt, 0), max_length);
211 thumb_path[0] = '\0';
213 sqlite3_finalize(stmt);
215 return MS_MEDIA_ERR_NONE;
218 int _media_thumb_update_thumb_path_to_db(sqlite3 *handle,
219 const char *origin_path,
223 int err = MS_MEDIA_ERR_NONE;
224 char *path_string = NULL;
225 char *thumbpath_string = NULL;
226 char *query_string = NULL;
228 if (handle == NULL) {
229 thumb_err("DB handle is NULL");
230 return MS_MEDIA_ERR_INVALID_PARAMETER;
232 if (!STRING_VALID(origin_path)) {
233 thumb_err("Invalid origin_path");
234 return MS_MEDIA_ERR_INVALID_PARAMETER;
237 path_string = sqlite3_mprintf("%s", origin_path);
238 if (!STRING_VALID(path_string)) {
239 thumb_err("Memory allocation is failed");
240 return MS_MEDIA_ERR_OUT_OF_MEMORY;
242 thumbpath_string = sqlite3_mprintf("%s", thumb_path);
243 if (!STRING_VALID(thumbpath_string)) {
244 thumb_err("Memory allocation is failed");
245 sqlite3_free(path_string);
246 return MS_MEDIA_ERR_OUT_OF_MEMORY;
248 query_string = sqlite3_mprintf(UPDATE_THUMB_BY_PATH, thumbpath_string, path_string);
249 if (!STRING_VALID(query_string)) {
250 thumb_err("Memory allocation is failed");
251 sqlite3_free(path_string);
252 sqlite3_free(thumbpath_string);
253 return MS_MEDIA_ERR_OUT_OF_MEMORY;
256 err = media_db_request_update_db(query_string, uid);
257 if (err != MS_MEDIA_ERR_NONE) {
258 thumb_err("media_db_request_update_db failed : %d", err);
260 thumb_dbg("Query success");
263 sqlite3_free(path_string);
264 sqlite3_free(thumbpath_string);
265 sqlite3_free(query_string);
270 int _media_thumb_update_wh_to_db(sqlite3 *handle,
271 const char *origin_path,
276 int err = MS_MEDIA_ERR_NONE;
277 char *path_string = NULL;
278 char *query_string = NULL;
280 if (handle == NULL) {
281 thumb_err("DB handle is NULL");
282 return MS_MEDIA_ERR_INVALID_PARAMETER;
284 if (!STRING_VALID(origin_path)) {
285 thumb_err("Invalid origin_path");
286 return MS_MEDIA_ERR_INVALID_PARAMETER;
289 path_string = sqlite3_mprintf("%s", origin_path);
290 if (!STRING_VALID(path_string)) {
291 thumb_err("Memory allocation is failed");
292 return MS_MEDIA_ERR_OUT_OF_MEMORY;
294 query_string = sqlite3_mprintf(UPDATE_WH_BY_PATH, width, height, path_string);
295 if (!STRING_VALID(query_string)) {
296 thumb_err("Memory allocation is failed");
297 sqlite3_free(path_string);
298 return MS_MEDIA_ERR_OUT_OF_MEMORY;
301 err = media_db_request_update_db(query_string, uid);
302 if (err != MS_MEDIA_ERR_NONE) {
303 thumb_err("media_db_request_update_db failed : %d", err);
305 thumb_dbg("Query success");
308 sqlite3_free(path_string);
309 sqlite3_free(query_string);
314 int _media_thumb_update_thumb_path_wh_to_db(sqlite3 *handle,
315 const char *origin_path,
321 int err = MS_MEDIA_ERR_NONE;
322 char *path_string = NULL;
323 char *query_string = NULL;
325 if (handle == NULL) {
326 thumb_err("DB handle is NULL");
327 return MS_MEDIA_ERR_INVALID_PARAMETER;
329 if (!STRING_VALID(origin_path)) {
330 thumb_err("Invalid origin_path");
331 return MS_MEDIA_ERR_INVALID_PARAMETER;
334 path_string = sqlite3_mprintf("%s", origin_path);
335 if (!STRING_VALID(path_string)) {
336 thumb_err("Memory allocation is failed");
337 return MS_MEDIA_ERR_OUT_OF_MEMORY;
339 query_string = sqlite3_mprintf(UPDATE_THUMB_WH_BY_PATH, thumb_path, width, height, path_string);
340 if (!STRING_VALID(query_string)) {
341 thumb_err("Memory allocation is failed");
342 sqlite3_free(path_string);
343 return MS_MEDIA_ERR_OUT_OF_MEMORY;
346 err = media_db_request_update_db(query_string, uid);
347 if (err != MS_MEDIA_ERR_NONE) {
348 thumb_err("media_db_request_update_db failed : %d", err);
350 thumb_dbg("Query success");
353 sqlite3_free(path_string);
354 sqlite3_free(query_string);
359 int _media_thumb_db_connect(uid_t uid)
361 int err = MS_MEDIA_ERR_NONE;
363 err = media_db_connect(&db_handle, uid, FALSE);
364 if (err != MS_MEDIA_ERR_NONE) {
365 thumb_err("media_db_connect failed: %d", err);
370 return MS_MEDIA_ERR_NONE;
373 int _media_thumb_db_disconnect()
375 int err = MS_MEDIA_ERR_NONE;
377 err = media_db_disconnect(db_handle);
378 if (err != MS_MEDIA_ERR_NONE) {
379 thumb_err("media_db_disconnect failed: %d", err);
388 int _media_thumb_get_thumb_from_db_with_size(const char *origin_path,
395 int err = MS_MEDIA_ERR_NONE;
397 //err = minfo_get_thumb_path(mb_svc_handle, origin_path, thumb_path, max_length);
398 err = _media_thumb_get_thumb_path_from_db(db_handle, origin_path, thumb_path, max_length);
399 if (err != MS_MEDIA_ERR_NONE) {
400 thumb_warn("Original path doesn't exist in DB");
404 if (strlen(thumb_path) == 0) {
405 thumb_warn("thumb path doesn't exist in DB");
407 return MS_MEDIA_ERR_INTERNAL;
410 thumb_dbg_slog("Thumb path in DB is %s", thumb_path);
412 if (!g_file_test(thumb_path, G_FILE_TEST_EXISTS)) {
413 thumb_warn("thumb path doesn't exist in file system");
415 return MS_MEDIA_ERR_INTERNAL;
417 thumb_dbg("This thumb path already exist");
421 err = _media_thumb_get_wh_from_db(db_handle, origin_path, &orig_w, &orig_h);
422 if (err != MS_MEDIA_ERR_NONE) {
423 thumb_err("_media_thumb_get_wh_from_db failed : %d", err);
425 thumb_err("_media_thumb_get_wh_from_db Success ( w:%d, h:%d )", orig_w, orig_h);
431 return MS_MEDIA_ERR_NONE;
434 int _media_thumb_update_db(const char *origin_path,
440 int err = MS_MEDIA_ERR_NONE;
441 int media_type = THUMB_NONE_TYPE;
443 err = _media_thumb_get_type_from_db(db_handle, origin_path, &media_type);
444 if (err != MS_MEDIA_ERR_NONE) {
445 thumb_err("_media_thumb_get_type_from_db (%s) failed: %d", origin_path, err);
450 err = _media_thumb_update_thumb_path_to_db(db_handle, origin_path, thumb_path);
452 thumb_err("_media_thumb_update_thumb_path_to_db (%s) failed: %d", origin_path, err);
456 if (media_type == THUMB_IMAGE_TYPE && width > 0 && height > 0) {
457 err = _media_thumb_update_wh_to_db(db_handle, origin_path, width, height);
459 thumb_err("_media_thumb_update_wh_to_db (%s) failed: %d", origin_path, err);
464 if (media_type == THUMB_IMAGE_TYPE && width > 0 && height > 0) {
465 err = _media_thumb_update_thumb_path_wh_to_db(db_handle, origin_path, thumb_path, width, height, uid);
466 if (err != MS_MEDIA_ERR_NONE) {
467 thumb_err("_media_thumb_update_wh_to_db (%s) failed: %d", origin_path, err);
471 err = _media_thumb_update_thumb_path_to_db(db_handle, origin_path, thumb_path, uid);
472 if (err != MS_MEDIA_ERR_NONE) {
473 thumb_err("_media_thumb_update_thumb_path_to_db (%s) failed: %d", origin_path, err);
479 thumb_dbg("_media_thumb_update_db success");
481 return MS_MEDIA_ERR_NONE;