Just remove unused code
[platform/core/multimedia/libmedia-thumbnail.git] / src / util / media-thumb-db.c
1 /*
2  * libmedia-thumbnail
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Hyunjun Ko <zzoon.ko@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 "media-thumb-db.h"
23 #include "media-thumb-util.h"
24 #include "media-thumb-debug.h"
25
26 #include <glib.h>
27 #include <string.h>
28 #include <unistd.h>
29
30 static __thread MediaDBHandle *db_handle;
31
32 sqlite3 *_media_thumb_db_get_handle()
33 {
34         return db_handle;
35 }
36
37 int _media_thumb_get_type_from_db(sqlite3 *handle,
38                                                                         const char *origin_path,
39                                                                         int *type)
40 {
41         int err = MS_MEDIA_ERR_NONE;
42         char *path_string = NULL;
43         char *query_string = NULL;
44         sqlite3_stmt *stmt = NULL;
45
46         thumb_dbg_slog("Origin path : %s", origin_path);
47
48         if (handle == NULL) {
49                 thumb_err("DB handle is NULL");
50                 return MS_MEDIA_ERR_INVALID_PARAMETER;
51         }
52         if (!STRING_VALID(origin_path)) {
53                 thumb_err("Invalid origin_path");
54                 return MS_MEDIA_ERR_INVALID_PARAMETER;
55         }
56
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;
61         }
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;
67         }
68
69         thumb_dbg("Query: %s", query_string);
70
71         err = sqlite3_prepare_v2(handle, query_string, strlen(query_string), &stmt, NULL);
72
73         sqlite3_free(query_string);
74         sqlite3_free(path_string);
75
76         if (SQLITE_OK != err) {
77                 thumb_err("prepare error [%s]", sqlite3_errmsg(handle));
78                 return MS_MEDIA_ERR_DB_INTERNAL;
79         }
80
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;
86         }
87
88         *type = sqlite3_column_int(stmt, 0);
89         sqlite3_finalize(stmt);
90
91         return MS_MEDIA_ERR_NONE;
92 }
93
94
95 int _media_thumb_get_wh_from_db(sqlite3 *handle,
96                                                                         const char *origin_path,
97                                                                         int *width,
98                                                                         int *height)
99 {
100         thumb_dbg_slog("Origin path : %s", origin_path);
101
102         if (handle == NULL) {
103                 thumb_err("DB handle is NULL");
104                 return MS_MEDIA_ERR_INVALID_PARAMETER;
105         }
106         if (!STRING_VALID(origin_path)) {
107                 thumb_err("Invalid origin_path");
108                 return MS_MEDIA_ERR_INVALID_PARAMETER;
109         }
110
111         int err = MS_MEDIA_ERR_NONE;
112         char *path_string = NULL;
113         char *query_string = NULL;
114         sqlite3_stmt *stmt = NULL;
115
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;
120         }
121
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;
127         }
128
129         thumb_dbg_slog("Query: %s", query_string);
130
131         err = sqlite3_prepare_v2(handle, query_string, strlen(query_string), &stmt, NULL);
132
133         sqlite3_free(query_string);
134         sqlite3_free(path_string);
135
136         if (SQLITE_OK != err) {
137                 thumb_err("prepare error [%s]", sqlite3_errmsg(handle));
138                 return MS_MEDIA_ERR_DB_INTERNAL;
139         }
140
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;
146         }
147
148         *width = sqlite3_column_int(stmt, 0);
149         *height = sqlite3_column_int(stmt, 1);
150         sqlite3_finalize(stmt);
151
152         return MS_MEDIA_ERR_NONE;
153 }
154
155 int _media_thumb_get_thumb_path_from_db(sqlite3 *handle,
156                                                                         const char *origin_path,
157                                                                         char *thumb_path,
158                                                                         int max_length)
159 {
160         thumb_dbg_slog("Origin path : %s", origin_path);
161
162         if (handle == NULL) {
163                 thumb_err("DB handle is NULL");
164                 return MS_MEDIA_ERR_INVALID_PARAMETER;
165         }
166         if (!STRING_VALID(origin_path)) {
167                 thumb_err("Invalid origin_path");
168                 return MS_MEDIA_ERR_INVALID_PARAMETER;
169         }
170
171         int err = MS_MEDIA_ERR_NONE;
172         char *path_string = NULL;
173         char *query_string = NULL;
174         sqlite3_stmt *stmt = NULL;
175
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;
180         }
181
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;
187         }
188
189         thumb_dbg_slog("Query: %s", query_string);
190
191         err = sqlite3_prepare_v2(handle, query_string, strlen(query_string), &stmt, NULL);
192
193         sqlite3_free(query_string);
194         sqlite3_free(path_string);
195
196         if (SQLITE_OK != err) {
197                 thumb_err("prepare error [%s]", sqlite3_errmsg(handle));
198                 return MS_MEDIA_ERR_DB_INTERNAL;
199         }
200
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;
206         }
207
208         if (sqlite3_column_text(stmt, 0))
209                 strncpy(thumb_path, (const char *)sqlite3_column_text(stmt, 0), max_length);
210         else
211                 thumb_path[0] = '\0';
212
213         sqlite3_finalize(stmt);
214
215         return MS_MEDIA_ERR_NONE;
216 }
217
218 int _media_thumb_update_thumb_path_to_db(sqlite3 *handle,
219                                                                         const char *origin_path,
220                                                                         char *thumb_path,
221                                                                         uid_t uid)
222 {
223         int err = MS_MEDIA_ERR_NONE;
224         char *path_string = NULL;
225         char *thumbpath_string = NULL;
226         char *query_string = NULL;
227
228         if (handle == NULL) {
229                 thumb_err("DB handle is NULL");
230                 return MS_MEDIA_ERR_INVALID_PARAMETER;
231         }
232         if (!STRING_VALID(origin_path)) {
233                 thumb_err("Invalid origin_path");
234                 return MS_MEDIA_ERR_INVALID_PARAMETER;
235         }
236
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;
241         }
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;
247         }
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;
254         }
255
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);
259         } else {
260                 thumb_dbg("Query success");
261         }
262
263         sqlite3_free(path_string);
264         sqlite3_free(thumbpath_string);
265         sqlite3_free(query_string);
266
267         return err;
268 }
269
270 int _media_thumb_update_wh_to_db(sqlite3 *handle,
271                                                                 const char *origin_path,
272                                                                 int width,
273                                                                 int height,
274                                                                 uid_t uid)
275 {
276         int err = MS_MEDIA_ERR_NONE;
277         char *path_string = NULL;
278         char *query_string = NULL;
279
280         if (handle == NULL) {
281                 thumb_err("DB handle is NULL");
282                 return MS_MEDIA_ERR_INVALID_PARAMETER;
283         }
284         if (!STRING_VALID(origin_path)) {
285                 thumb_err("Invalid origin_path");
286                 return MS_MEDIA_ERR_INVALID_PARAMETER;
287         }
288
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;
293         }
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;
299         }
300
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);
304         } else {
305                 thumb_dbg("Query success");
306         }
307
308         sqlite3_free(path_string);
309         sqlite3_free(query_string);
310
311         return err;
312 }
313
314 int _media_thumb_update_thumb_path_wh_to_db(sqlite3 *handle,
315                                                                 const char *origin_path,
316                                                                 char *thumb_path,
317                                                                 int width,
318                                                                 int height,
319                                                                 uid_t uid)
320 {
321         int err = MS_MEDIA_ERR_NONE;
322         char *path_string = NULL;
323         char *query_string = NULL;
324
325         if (handle == NULL) {
326                 thumb_err("DB handle is NULL");
327                 return MS_MEDIA_ERR_INVALID_PARAMETER;
328         }
329         if (!STRING_VALID(origin_path)) {
330                 thumb_err("Invalid origin_path");
331                 return MS_MEDIA_ERR_INVALID_PARAMETER;
332         }
333
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;
338         }
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;
344         }
345
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);
349         } else {
350                 thumb_dbg("Query success");
351         }
352
353         sqlite3_free(path_string);
354         sqlite3_free(query_string);
355
356         return err;
357 }
358
359 int _media_thumb_db_connect(uid_t uid)
360 {
361         int err = MS_MEDIA_ERR_NONE;
362
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);
366                 db_handle = NULL;
367                 return err;
368         }
369
370         return MS_MEDIA_ERR_NONE;
371 }
372
373 int _media_thumb_db_disconnect()
374 {
375         int err = MS_MEDIA_ERR_NONE;
376
377         err = media_db_disconnect(db_handle);
378         if (err != MS_MEDIA_ERR_NONE) {
379                 thumb_err("media_db_disconnect failed: %d", err);
380                 db_handle = NULL;
381                 return err;
382         }
383
384         db_handle = NULL;
385         return err;
386 }
387
388 int _media_thumb_get_thumb_from_db_with_size(const char *origin_path,
389                                                                 char *thumb_path,
390                                                                 int max_length,
391                                                                 int *need_update_db,
392                                                                 int *width,
393                                                                 int *height)
394 {
395         int err = MS_MEDIA_ERR_NONE;
396
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");
401                 return err;
402         }
403
404         if (strlen(thumb_path) == 0) {
405                 thumb_warn("thumb path doesn't exist in DB");
406                 *need_update_db = 1;
407                 return MS_MEDIA_ERR_INTERNAL;
408         }
409
410         thumb_dbg_slog("Thumb path in DB is %s", thumb_path);
411
412         if (!g_file_test(thumb_path, G_FILE_TEST_EXISTS)) {
413                 thumb_warn("thumb path doesn't exist in file system");
414                 *need_update_db = 1;
415                 return MS_MEDIA_ERR_INTERNAL;
416         } else {
417                 thumb_dbg("This thumb path already exist");
418                 int orig_w = 0;
419                 int orig_h = 0;
420
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);
424                 } else {
425                         thumb_err("_media_thumb_get_wh_from_db Success ( w:%d, h:%d )", orig_w, orig_h);
426                         *width = orig_w;
427                         *height = orig_h;
428                 }
429         }
430
431         return MS_MEDIA_ERR_NONE;
432 }
433
434 int _media_thumb_update_db(const char *origin_path,
435                                                                         char *thumb_path,
436                                                                         int width,
437                                                                         int height,
438                                                                         uid_t uid)
439 {
440         int err = MS_MEDIA_ERR_NONE;
441         int media_type = THUMB_NONE_TYPE;
442
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);
446                 return err;
447         }
448
449 #if 0
450         err = _media_thumb_update_thumb_path_to_db(db_handle, origin_path, thumb_path);
451         if (err < 0) {
452                 thumb_err("_media_thumb_update_thumb_path_to_db (%s) failed: %d", origin_path, err);
453                 return err;
454         }
455
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);
458                 if (err < 0) {
459                         thumb_err("_media_thumb_update_wh_to_db (%s) failed: %d", origin_path, err);
460                         return err;
461                 }
462         }
463 #else
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);
468                         return err;
469                 }
470         } else {
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);
474                         return err;
475                 }
476         }
477 #endif
478
479         thumb_dbg("_media_thumb_update_db success");
480
481         return MS_MEDIA_ERR_NONE;
482 }
483