1. Thumbnail extracting is enabled for burst shot
[platform/core/multimedia/libmedia-service.git] / src / common / media-svc-media.c
1 /*
2  * libmedia-service
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Hyunjun Ko <zzoon.ko@samsung.com>, Haejeong Kim <backto.kim@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 <string.h>
23 #include "media-svc-media.h"
24 #include "media-svc-media-folder.h"
25 #include "media-svc-error.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"
30
31 typedef struct{
32         char thumbnail_path[MEDIA_SVC_PATHNAME_SIZE];
33 }media_svc_thumbnailpath_s;
34
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
39 static int __media_svc_count_invalid_records_with_thumbnail(sqlite3 *handle, media_svc_storage_type_e storage_type, int *count);
40 static int __media_svc_get_invalid_records_with_thumbnail(sqlite3 *handle, media_svc_storage_type_e storage_type,
41                                                         int count, media_svc_thumbnailpath_s * thumb_path);
42 static int __media_svc_count_invalid_folder_records_with_thumbnail(sqlite3 *handle, const char *folder_path, int *count);
43 static int __media_svc_get_invalid_folder_records_with_thumbnail(sqlite3 *handle, const char *folder_path,
44                                                         int count, media_svc_thumbnailpath_s * thumb_path);
45
46 static int __media_svc_count_invalid_records_with_thumbnail(sqlite3 *handle, media_svc_storage_type_e storage_type, int *count)
47 {
48         int ret = MEDIA_INFO_ERROR_NONE;
49         sqlite3_stmt *sql_stmt = NULL;
50         char *sql = sqlite3_mprintf("SELECT count(*) FROM %s WHERE validity=0 AND storage_type=%d AND thumbnail_path IS NOT NULL",
51                                         MEDIA_SVC_DB_TABLE_MEDIA, storage_type);
52
53         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
54
55         if (ret != MEDIA_INFO_ERROR_NONE) {
56                 media_svc_error("error when __media_svc_count_invalid_records_with_thumbnail. err = [%d]", ret);
57                 return ret;
58         }
59
60         *count = sqlite3_column_int(sql_stmt, 0);
61
62         SQLITE3_FINALIZE(sql_stmt);
63
64         return MEDIA_INFO_ERROR_NONE;
65
66 }
67
68 static int __media_svc_get_invalid_records_with_thumbnail(sqlite3 *handle, media_svc_storage_type_e storage_type,
69                                                         int count, media_svc_thumbnailpath_s * thumb_path)
70 {
71         int err = -1;
72         int idx = 0;
73         sqlite3_stmt *sql_stmt = NULL;
74
75         char *sql = sqlite3_mprintf("select thumbnail_path from %s WHERE validity=0 AND storage_type=%d AND thumbnail_path IS NOT NULL",
76                                         MEDIA_SVC_DB_TABLE_MEDIA, storage_type);
77
78         media_svc_debug("[SQL query] : %s", sql);
79
80         err = sqlite3_prepare_v2(handle, sql, -1, &sql_stmt, NULL);
81         sqlite3_free(sql);
82         if (err != SQLITE_OK) {
83                 media_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
84                 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
85         }
86
87         while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
88                 _strncpy_safe(thumb_path[idx].thumbnail_path, (const char *)sqlite3_column_text(sql_stmt, 0), sizeof(thumb_path[idx]));
89                 //media_svc_debug("thumb_path[%d]=[%s]", idx, thumb_path[idx].thumbnail_path);
90                 idx++;
91         }
92
93         SQLITE3_FINALIZE(sql_stmt);
94
95         return MEDIA_INFO_ERROR_NONE;
96 }
97
98 static int __media_svc_count_invalid_folder_records_with_thumbnail(sqlite3 *handle, const char *folder_path, int *count)
99 {
100         int ret = MEDIA_INFO_ERROR_NONE;
101         sqlite3_stmt *sql_stmt = NULL;
102         char *sql = sqlite3_mprintf("SELECT count(*) FROM %s WHERE validity=0 AND path LIKE '%q/%%' AND thumbnail_path IS NOT NULL",
103                                         MEDIA_SVC_DB_TABLE_MEDIA, folder_path);
104
105         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
106
107         if (ret != MEDIA_INFO_ERROR_NONE) {
108                 media_svc_error("error when __media_svc_count_invalid_folder_records_with_thumbnail. err = [%d]", ret);
109                 return ret;
110         }
111
112         *count = sqlite3_column_int(sql_stmt, 0);
113
114         SQLITE3_FINALIZE(sql_stmt);
115
116         return MEDIA_INFO_ERROR_NONE;
117
118 }
119
120 static int __media_svc_get_invalid_folder_records_with_thumbnail(sqlite3 *handle, const char *folder_path,
121                                                         int count, media_svc_thumbnailpath_s * thumb_path)
122 {
123         int err = -1;
124         int idx = 0;
125         sqlite3_stmt *sql_stmt = NULL;
126
127         char *sql = sqlite3_mprintf("select thumbnail_path from %s WHERE validity=0 AND path LIKE '%q/%%' AND thumbnail_path IS NOT NULL",
128                                         MEDIA_SVC_DB_TABLE_MEDIA, folder_path);
129
130         media_svc_debug("[SQL query] : %s", sql);
131
132         err = sqlite3_prepare_v2(handle, sql, -1, &sql_stmt, NULL);
133         sqlite3_free(sql);
134         if (err != SQLITE_OK) {
135                 media_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
136                 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
137         }
138
139         while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
140                 _strncpy_safe(thumb_path[idx].thumbnail_path, (const char *)sqlite3_column_text(sql_stmt, 0), sizeof(thumb_path[idx]));
141                 idx++;
142         }
143
144         SQLITE3_FINALIZE(sql_stmt);
145
146         return MEDIA_INFO_ERROR_NONE;
147 }
148
149 int _media_svc_count_record_with_path(sqlite3 *handle, const char *path, int *count)
150 {
151         int ret = MEDIA_INFO_ERROR_NONE;
152         sqlite3_stmt *sql_stmt = NULL;
153
154         char *sql = sqlite3_mprintf("SELECT count(*) FROM %s WHERE path='%q'", MEDIA_SVC_DB_TABLE_MEDIA, path);
155
156         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
157
158         media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
159
160         *count = sqlite3_column_int(sql_stmt, 0);
161
162         SQLITE3_FINALIZE(sql_stmt);
163
164         return MEDIA_INFO_ERROR_NONE;
165 }
166
167 int _media_svc_insert_item_with_data(sqlite3 *handle, media_svc_content_info_s *content_info, int is_burst, bool stack_query)
168 {
169         media_svc_debug("");
170         int err = -1;
171         char *burst_id = NULL;
172
173         char * db_fields = "media_uuid, path, file_name, media_type, mime_type, size, added_time, modified_time, folder_uuid, \
174                                         thumbnail_path, title, album_id, album, artist, genre, composer, year, recorded_date, copyright, track_num, description,\
175                                         bitrate, samplerate, channel, duration, longitude, latitude, altitude, width, height, datetaken, orientation,\
176                                         rating, is_drm, storage_type, burst_id";
177
178         /* This sql is due to sqlite3_mprintf's wrong operation when using floating point in the text format */
179         /* This code will be removed when sqlite3_mprintf works clearly */
180         char *test_sql = sqlite3_mprintf("%f, %f, %f", content_info->media_meta.longitude, content_info->media_meta.latitude, content_info->media_meta.altitude);
181         sqlite3_free(test_sql);
182
183         if (is_burst) {
184                 int burst_id_int = 0;
185                 err = _media_svc_get_burst_id(handle, &burst_id_int);
186                 if (err < 0) {
187                         burst_id = NULL;
188                 }
189
190                 if (burst_id_int > 0) {
191                         media_svc_debug("Burst id : %d", burst_id_int);
192                         burst_id = sqlite3_mprintf("%d", burst_id_int);
193                 }
194
195                 /* Get thumbnail for burst shot */
196                 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
197                 int width = 0;
198                 int height = 0;
199
200                 err = thumbnail_request_from_db_with_size(content_info->path, thumb_path, sizeof(thumb_path), &width, &height);
201                 if (err < 0) {
202                         media_svc_error("thumbnail_request_from_db failed: %d", err);
203                 } else {
204                         media_svc_debug("thumbnail_request_from_db success: %s", thumb_path);
205                         err = __media_svc_malloc_and_strncpy(&(content_info->thumbnail_path), thumb_path);
206                         if (err < 0) {
207                                 content_info->thumbnail_path = NULL;
208                         }
209                 }
210
211                 if (content_info->media_meta.width <= 0)
212                         content_info->media_meta.width = width;
213
214                 if (content_info->media_meta.height <= 0)
215                         content_info->media_meta.height = height;
216         }
217
218         char *sql = sqlite3_mprintf("INSERT INTO %s (%s) VALUES (%Q, %Q, %Q, %d, %Q, %lld, %d, %d, %Q, \
219                                                                                                         %Q, %Q, %d, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, \
220                                                                                                         %d, %d, %d, %d, %.2f, %.2f, %.2f, %d, %d, %Q, %d, \
221                                                                                                         %d, %d, %d, %Q);",
222                 MEDIA_SVC_DB_TABLE_MEDIA, db_fields,
223                 content_info->media_uuid,
224                 content_info->path,
225                 content_info->file_name,
226                 content_info->media_type,
227                 content_info->mime_type,
228                 content_info->size,
229                 content_info->added_time,
230                 content_info->modified_time,
231                 content_info->folder_uuid,
232                 content_info->thumbnail_path,           //
233                 content_info->media_meta.title,
234                 content_info->album_id,
235                 content_info->media_meta.album,
236                 content_info->media_meta.artist,
237                 content_info->media_meta.genre,
238                 content_info->media_meta.composer,
239                 content_info->media_meta.year,
240                 content_info->media_meta.recorded_date,
241                 content_info->media_meta.copyright,
242                 content_info->media_meta.track_num,
243                 content_info->media_meta.description,   //
244                 content_info->media_meta.bitrate,
245                 content_info->media_meta.samplerate,
246                 content_info->media_meta.channel,
247                 content_info->media_meta.duration,
248                 content_info->media_meta.longitude,
249                 content_info->media_meta.latitude,
250                 content_info->media_meta.altitude,
251                 content_info->media_meta.width,
252                 content_info->media_meta.height,
253                 content_info->media_meta.datetaken,
254                 content_info->media_meta.orientation,
255                 content_info->media_meta.rating,
256                 content_info->is_drm,
257                 content_info->storage_type,
258                 burst_id);
259
260         if (burst_id) sqlite3_free(burst_id);
261         burst_id = NULL;
262
263         if(!stack_query) {
264                 err = _media_svc_sql_query(handle, sql);
265                 sqlite3_free(sql);
266                 if (err != SQLITE_OK) {
267                         media_svc_error("failed to insert item");
268
269                         return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
270                 }
271         } else {
272                 media_svc_debug("query : %s", sql);
273                 _media_svc_sql_query_add(&g_media_svc_insert_item_query_list, &sql);
274         }
275
276         return MEDIA_INFO_ERROR_NONE;
277 }
278
279 int _media_svc_update_item_with_data(sqlite3 *handle, media_svc_content_info_s *content_info)
280 {
281         int err = -1;
282
283         /* This sql is due to sqlite3_mprintf's wrong operation when using floating point in the text format */
284         /* This code will be removed when sqlite3_mprintf works clearly */
285         char *test_sql = sqlite3_mprintf("%f, %f, %f", content_info->media_meta.longitude, content_info->media_meta.latitude, content_info->media_meta.altitude);
286         sqlite3_free(test_sql);
287
288         char *sql = sqlite3_mprintf("UPDATE %s SET \
289                 size=%lld, modified_time=%d, thumbnail_path=%Q, title=%Q, album_id=%d, album=%Q, artist=%Q, genre=%Q, \
290                 composer=%Q, year=%Q, recorded_date=%Q, copyright=%Q, track_num=%Q, description=%Q, \
291                 bitrate=%d, samplerate=%d, channel=%d, duration=%d, longitude=%f, latitude=%f, altitude=%f, width=%d, height=%d, datetaken=%Q, \
292                                                                                                         orientation=%d WHERE path=%Q",
293                 MEDIA_SVC_DB_TABLE_MEDIA,
294                 content_info->size,
295                 content_info->modified_time,
296                 content_info->thumbnail_path,
297                 content_info->media_meta.title,
298                 content_info->album_id,
299                 content_info->media_meta.album,
300                 content_info->media_meta.artist,
301                 content_info->media_meta.genre,
302                 content_info->media_meta.composer,
303                 content_info->media_meta.year,
304                 content_info->media_meta.recorded_date,
305                 content_info->media_meta.copyright,
306                 content_info->media_meta.track_num,
307                 content_info->media_meta.description,
308                 content_info->media_meta.bitrate,
309                 content_info->media_meta.samplerate,
310                 content_info->media_meta.channel,
311                 content_info->media_meta.duration,
312                 content_info->media_meta.longitude,
313                 content_info->media_meta.latitude,
314                 content_info->media_meta.altitude,
315                 content_info->media_meta.width,
316                 content_info->media_meta.height,
317                 content_info->media_meta.datetaken,
318                 content_info->media_meta.orientation,
319                 content_info->path
320                 );
321
322         err = _media_svc_sql_query(handle, sql);
323         sqlite3_free(sql);
324         if (err != SQLITE_OK) {
325                 media_svc_error("failed to update item");
326
327                 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
328         }
329
330         return MEDIA_INFO_ERROR_NONE;
331 }
332 int _media_svc_get_thumbnail_path_by_path(sqlite3 *handle, const char *path, char *thumbnail_path)
333 {
334         int ret = MEDIA_INFO_ERROR_NONE;
335         sqlite3_stmt *sql_stmt = NULL;
336
337         char *sql = sqlite3_mprintf("SELECT thumbnail_path FROM %s WHERE path='%q'", MEDIA_SVC_DB_TABLE_MEDIA, path);
338
339         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
340
341         if (ret != MEDIA_INFO_ERROR_NONE) {
342                 if(ret == MEDIA_INFO_ERROR_DATABASE_NO_RECORD) {
343                         media_svc_debug("there is no thumbnail.");
344                 }
345                 else {
346                         media_svc_error("error when _media_svc_get_thumbnail_path_by_path. err = [%d]", ret);
347                 }
348                 return ret;
349         }
350
351         _strncpy_safe(thumbnail_path, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_PATHNAME_SIZE);
352
353         SQLITE3_FINALIZE(sql_stmt);
354
355         return MEDIA_INFO_ERROR_NONE;
356 }
357
358 int _media_svc_get_media_type_by_path(sqlite3 *handle, const char *path, int *media_type)
359 {
360         int ret = MEDIA_INFO_ERROR_NONE;
361         sqlite3_stmt *sql_stmt = NULL;
362
363         char *sql = sqlite3_mprintf("SELECT media_type FROM %s WHERE path='%q'", MEDIA_SVC_DB_TABLE_MEDIA, path);
364
365         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
366
367         if (ret != MEDIA_INFO_ERROR_NONE) {
368                 media_svc_error("error when _media_svc_get_media_type_by_path. err = [%d]", ret);
369                 return ret;
370         }
371
372         *media_type = sqlite3_column_int(sql_stmt, 0);
373
374         SQLITE3_FINALIZE(sql_stmt);
375
376         return MEDIA_INFO_ERROR_NONE;
377 }
378
379 int _media_svc_delete_item_by_path(sqlite3 *handle, const char *path)
380 {
381         int err = -1;
382         char *sql = sqlite3_mprintf("DELETE FROM %s WHERE validity=1 AND path='%q'", MEDIA_SVC_DB_TABLE_MEDIA, path);
383
384         err = _media_svc_sql_query(handle, sql);
385         sqlite3_free(sql);
386         if (err != SQLITE_OK) {
387                 media_svc_error("It failed to delete item (%d)", err);
388                 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
389         }
390
391         return MEDIA_INFO_ERROR_NONE;
392 }
393
394 int _media_svc_truncate_table(sqlite3 *handle, media_svc_storage_type_e storage_type)
395 {
396         int err = -1;
397         char *sql = sqlite3_mprintf("DELETE FROM %s WHERE storage_type=%d", MEDIA_SVC_DB_TABLE_MEDIA, storage_type);
398
399         err = _media_svc_sql_query(handle, sql);
400         sqlite3_free(sql);
401         if (err != SQLITE_OK) {
402                 media_svc_error("It failed to truncate table (%d)", err);
403                 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
404         }
405
406         return MEDIA_INFO_ERROR_NONE;
407
408 }
409
410 int _media_svc_delete_invalid_items(sqlite3 *handle, media_svc_storage_type_e storage_type)
411 {
412         int idx = 0;
413         media_svc_thumbnailpath_s *thumbpath_record = NULL;
414         int err = -1;
415         int invalid_count = 0;
416         int ret = MEDIA_INFO_ERROR_NONE;
417
418         ret = __media_svc_count_invalid_records_with_thumbnail(handle, storage_type, &invalid_count);
419         media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
420
421         media_svc_debug("invalid count: %d\n", invalid_count);
422
423         if (invalid_count > 0) {
424                 thumbpath_record = (media_svc_thumbnailpath_s *)calloc( invalid_count, sizeof(media_svc_thumbnailpath_s));
425                 if (thumbpath_record == NULL) {
426                         media_svc_error("fail to memory allocation");
427                         return MEDIA_INFO_ERROR_OUT_OF_MEMORY;
428                 }
429
430                 ret = __media_svc_get_invalid_records_with_thumbnail(handle, storage_type, invalid_count, thumbpath_record);
431                 if (ret != MEDIA_INFO_ERROR_NONE) {
432                         media_svc_error("error when get thumbnail record");
433                         SAFE_FREE(thumbpath_record);
434                         return ret;
435                 }
436         } else {
437                 media_svc_debug("There is no item with thumbnail");
438         }
439
440         char *sql = sqlite3_mprintf("DELETE FROM %s WHERE validity = 0 AND storage_type=%d", MEDIA_SVC_DB_TABLE_MEDIA, storage_type);
441         err = _media_svc_sql_query(handle, sql);
442         sqlite3_free(sql);
443         if (err != SQLITE_OK) {
444                 media_svc_error("To delete invalid items is failed(%d)", err);
445                 SAFE_FREE(thumbpath_record);
446                 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
447         }
448
449         /*Delete thumbnails*/
450         for (idx = 0; idx < invalid_count; idx++) {
451                 if (strlen(thumbpath_record[idx].thumbnail_path) > 0) {
452                         if (_media_svc_remove_file(thumbpath_record[idx].thumbnail_path) == FALSE) {
453                                 media_svc_error("fail to remove thumbnail file.");
454                                 //SAFE_FREE(thumbpath_record);
455                                 //return MEDIA_INFO_ERROR_INTERNAL;
456                         }
457                 }
458         }
459
460         SAFE_FREE(thumbpath_record);
461
462         return MEDIA_INFO_ERROR_NONE;
463 }
464
465 int _media_svc_delete_invalid_folder_items(sqlite3 *handle, const char *folder_path)
466 {
467         int idx = 0;
468         media_svc_thumbnailpath_s *thumbpath_record = NULL;
469         int err = -1;
470         int invalid_count = 0;
471         int ret = MEDIA_INFO_ERROR_NONE;
472
473         ret = __media_svc_count_invalid_folder_records_with_thumbnail(handle, folder_path, &invalid_count);
474         media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
475
476         media_svc_debug("invalid count: %d\n", invalid_count);
477
478         if (invalid_count > 0) {
479                 thumbpath_record = (media_svc_thumbnailpath_s *)calloc( invalid_count, sizeof(media_svc_thumbnailpath_s));
480                 if (thumbpath_record == NULL) {
481                         media_svc_error("fail to memory allocation");
482                         return MEDIA_INFO_ERROR_OUT_OF_MEMORY;
483                 }
484
485                 ret = __media_svc_get_invalid_folder_records_with_thumbnail(handle, folder_path, invalid_count, thumbpath_record);
486                 if (ret != MEDIA_INFO_ERROR_NONE) {
487                         media_svc_error("error when get thumbnail record");
488                         SAFE_FREE(thumbpath_record);
489                         return ret;
490                 }
491         } else {
492                 media_svc_debug("There is no item with thumbnail");
493         }
494
495         char *sql = sqlite3_mprintf("DELETE FROM %s WHERE validity = 0 AND path LIKE '%q/%%'", MEDIA_SVC_DB_TABLE_MEDIA, folder_path);
496         err = _media_svc_sql_query(handle, sql);
497         sqlite3_free(sql);
498         if (err != SQLITE_OK) {
499                 media_svc_error("To delete invalid items is failed(%d)", err);
500                 SAFE_FREE(thumbpath_record);
501                 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
502         }
503
504         /*Delete thumbnails*/
505         for (idx = 0; idx < invalid_count; idx++) {
506                 if (strlen(thumbpath_record[idx].thumbnail_path) > 0) {
507                         if (_media_svc_remove_file(thumbpath_record[idx].thumbnail_path) == FALSE) {
508                                 media_svc_error("fail to remove thumbnail file [%s].", thumbpath_record[idx].thumbnail_path);
509                                 //SAFE_FREE(thumbpath_record);
510                                 //return MEDIA_INFO_ERROR_INTERNAL;
511                         }
512                 }
513         }
514
515         SAFE_FREE(thumbpath_record);
516
517         return MEDIA_INFO_ERROR_NONE;
518 }
519
520
521 int _media_svc_update_item_validity(sqlite3 *handle, const char *path, int validity, bool stack_query)
522 {
523         int err = -1;
524
525         char *sql = sqlite3_mprintf("UPDATE %s SET validity=%d WHERE path= '%q'", MEDIA_SVC_DB_TABLE_MEDIA, validity, path);
526
527         if(!stack_query) {
528                 err = _media_svc_sql_query(handle, sql);
529                 sqlite3_free(sql);
530                 if (err != SQLITE_OK) {
531                         media_svc_error("To update item as valid is failed(%d)", err);
532                         return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
533                 }
534         } else {
535                 _media_svc_sql_query_add(&g_media_svc_item_validity_query_list, &sql);
536         }
537
538         return MEDIA_INFO_ERROR_NONE;
539 }
540
541 int _media_svc_update_thumbnail_path(sqlite3 *handle, const char *path, const char *thumb_path)
542 {
543         int err = -1;
544
545         char *sql = sqlite3_mprintf("UPDATE %s SET thumbnail_path=%Q WHERE path= %Q", MEDIA_SVC_DB_TABLE_MEDIA, thumb_path, path);
546
547         err = _media_svc_sql_query(handle, sql);
548         sqlite3_free(sql);
549         if (err != SQLITE_OK) {
550                 media_svc_error("To update thumb path failed(%d)", err);
551                 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
552         }
553
554         return MEDIA_INFO_ERROR_NONE;
555 }
556
557 int _media_svc_update_storage_item_validity(sqlite3 *handle, media_svc_storage_type_e storage_type, int validity)
558 {
559         int err = -1;
560         char *sql = sqlite3_mprintf("UPDATE %s SET validity=%d WHERE storage_type=%d", MEDIA_SVC_DB_TABLE_MEDIA, validity, storage_type);
561         err = _media_svc_sql_query(handle, sql);
562         sqlite3_free(sql);
563         if (err != SQLITE_OK) {
564                 media_svc_error("To update item as valid is failed(%d)", err);
565                 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
566         }
567
568         return MEDIA_INFO_ERROR_NONE;
569 }
570
571 int _media_svc_update_folder_item_validity(sqlite3 *handle, const char *folder_path, int validity)
572 {
573         int err = -1;
574         int ret = MEDIA_INFO_ERROR_NONE;
575         char *sql = NULL;
576         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
577         sqlite3_stmt *sql_stmt = NULL;
578
579         /*Get folder ID*/
580         sql = sqlite3_mprintf("SELECT folder_uuid FROM %s WHERE path='%q'", MEDIA_SVC_DB_TABLE_FOLDER, folder_path);
581         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
582         if (ret != MEDIA_INFO_ERROR_NONE) {
583                 media_svc_error("error when get folder_id. err = [%d]", ret);
584                 return ret;
585         }
586
587         _strncpy_safe(folder_uuid, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE+1);
588         SQLITE3_FINALIZE(sql_stmt);
589
590         /*Update folder item validity*/
591         sql = sqlite3_mprintf("UPDATE %s SET validity=%d WHERE folder_uuid='%q'", MEDIA_SVC_DB_TABLE_MEDIA, validity, folder_uuid);
592         err = _media_svc_sql_query(handle, sql);
593         sqlite3_free(sql);
594         if (err != SQLITE_OK) {
595                 media_svc_error("To update folder item as valid is failed(%d)", err);
596                 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
597         }
598
599         return MEDIA_INFO_ERROR_NONE;
600 }
601
602 int _media_svc_update_recursive_folder_item_validity(sqlite3 *handle, const char *folder_path, int validity)
603 {
604         int err = -1;
605
606         /*Update folder item validity*/
607         char *sql = sqlite3_mprintf("UPDATE %s SET validity=%d WHERE path LIKE '%q/%%'", MEDIA_SVC_DB_TABLE_MEDIA, validity, folder_path);
608         err = _media_svc_sql_query(handle, sql);
609         sqlite3_free(sql);
610         if (err != SQLITE_OK) {
611                 media_svc_error("To update recursive folder item validity is failed(%d)", err);
612                 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
613         }
614
615         return MEDIA_INFO_ERROR_NONE;
616 }
617
618 int _media_svc_update_item_by_path(sqlite3 *handle, const char *src_path, media_svc_storage_type_e dest_storage, const char *dest_path,
619                                 const char *file_name, int modified_time, const char *folder_uuid, const char *thumb_path, bool stack_query)
620 {
621         /* update path, filename, modified_time, folder_uuid, thumbnail_path, */
622         /* played_count, last_played_time, last_played_position, favourite, storaget_type*/
623
624         int err = -1;
625         char *sql = NULL;
626
627         if(thumb_path != NULL) {
628                 sql = sqlite3_mprintf("UPDATE %s SET \
629                                         path=%Q, file_name=%Q, modified_time=%d, folder_uuid=%Q, thumbnail_path=%Q, storage_type=%d, \
630                                         played_count=0, last_played_time=0, last_played_position=0 \
631                                         WHERE path=%Q",
632                                         MEDIA_SVC_DB_TABLE_MEDIA, dest_path, file_name, modified_time, folder_uuid, thumb_path, dest_storage, src_path);
633         } else {
634                 sql = sqlite3_mprintf("UPDATE %s SET \
635                                         path=%Q, file_name=%Q, modified_time=%d, folder_uuid=%Q, storage_type=%d, \
636                                         played_count=0, last_played_time=0, last_played_position=0 \
637                                         WHERE path=%Q",
638                                         MEDIA_SVC_DB_TABLE_MEDIA, dest_path, file_name, modified_time, folder_uuid, dest_storage, src_path);
639         }
640
641         if(!stack_query) {
642                 err = _media_svc_sql_query(handle, sql);
643                 sqlite3_free(sql);
644                 if (err != SQLITE_OK) {
645                         media_svc_error("It failed to update metadata (%d)", err);
646                         return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
647                 }
648         } else {
649                 _media_svc_sql_query_add(&g_media_svc_move_item_query_list, &sql);
650         }
651
652         return MEDIA_INFO_ERROR_NONE;
653 }
654
655 int _media_svc_list_query_do(sqlite3 *handle, media_svc_query_type_e query_type)
656 {
657         int ret = MEDIA_INFO_ERROR_NONE;
658
659         ret = _media_svc_sql_begin_trans(handle);
660         media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
661
662         if (query_type == MEDIA_SVC_QUERY_SET_ITEM_VALIDITY)
663                 ret = _media_svc_sql_query_list(handle, &g_media_svc_item_validity_query_list);
664         else if (query_type == MEDIA_SVC_QUERY_MOVE_ITEM)
665                 ret = _media_svc_sql_query_list(handle, &g_media_svc_move_item_query_list);
666         else if (query_type == MEDIA_SVC_QUERY_INSERT_ITEM)
667                 ret = _media_svc_sql_query_list(handle, &g_media_svc_insert_item_query_list);
668         else
669                 ret = MEDIA_INFO_ERROR_INVALID_PARAMETER;
670
671         if (ret != MEDIA_INFO_ERROR_NONE) {
672                 media_svc_error("_media_svc_list_query_do failed. start rollback");
673                 _media_svc_sql_rollback_trans(handle);
674                 return ret;
675         }
676
677         ret = _media_svc_sql_end_trans(handle);
678         if (ret != MEDIA_INFO_ERROR_NONE) {
679                 media_svc_error("mb_svc_sqlite3_commit_trans failed.. Now start to rollback\n");
680                 _media_svc_sql_rollback_trans(handle);
681                 return ret;
682         }
683
684         return MEDIA_INFO_ERROR_NONE;
685 }
686
687 int _media_svc_get_media_id_by_path(sqlite3 *handle, const char *path, char *media_uuid, int max_length)
688 {
689         int ret = MEDIA_INFO_ERROR_NONE;
690         sqlite3_stmt *sql_stmt = NULL;
691         char *sql = sqlite3_mprintf("SELECT media_uuid FROM %s WHERE validity=1 AND path='%q'",
692                                         MEDIA_SVC_DB_TABLE_MEDIA, path);
693
694         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
695
696         if (ret != MEDIA_INFO_ERROR_NONE) {
697                 media_svc_error("error when __media_svc_count_invalid_records_with_thumbnail. err = [%d]", ret);
698                 return ret;
699         }
700
701         strncpy(media_uuid, (const char*)sqlite3_column_text(sql_stmt, 0), max_length);
702         media_uuid[max_length - 1] = '\0';
703
704         SQLITE3_FINALIZE(sql_stmt);
705
706         return MEDIA_INFO_ERROR_NONE;
707 }
708
709 int _media_svc_get_burst_id(sqlite3 *handle, int *id)
710 {
711         int ret = MEDIA_INFO_ERROR_NONE;
712         int cur_id = -1;
713         sqlite3_stmt *sql_stmt = NULL;
714         char *sql = sqlite3_mprintf("SELECT max(CAST(burst_id AS INTEGER)) FROM %s", MEDIA_SVC_DB_TABLE_MEDIA);
715
716         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
717
718         if (ret != MEDIA_INFO_ERROR_NONE) {
719                 media_svc_error("error when _media_svc_get_burst_id. err = [%d]", ret);
720                 return ret;
721         }
722
723         cur_id = sqlite3_column_int(sql_stmt, 0);
724         *id = ++cur_id;
725         SQLITE3_FINALIZE(sql_stmt);
726
727         return MEDIA_INFO_ERROR_NONE;
728 }
729
730 int _media_svc_get_noti_info(sqlite3 *handle, const char *path, int update_item, media_svc_noti_item **item)
731 {
732         int ret = MEDIA_INFO_ERROR_NONE;
733         sqlite3_stmt *sql_stmt = NULL;
734         char *sql = NULL;
735
736         if (item == NULL) {
737                 media_svc_error("_media_svc_get_noti_info failed");
738                 return MEDIA_INFO_ERROR_INVALID_PARAMETER;
739         }
740
741         if (update_item == MS_MEDIA_ITEM_FILE) {
742                 sql = sqlite3_mprintf("SELECT media_uuid, media_type, mime_type FROM %s", MEDIA_SVC_DB_TABLE_MEDIA);
743         } else if (update_item == MS_MEDIA_ITEM_DIRECTORY) {
744                 sql = sqlite3_mprintf("SELECT folder_uuid FROM %s", MEDIA_SVC_DB_TABLE_FOLDER);
745         } else {
746                 media_svc_error("_media_svc_get_noti_info failed : update item");
747                 return MEDIA_INFO_ERROR_INVALID_PARAMETER;
748         }
749
750         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
751
752         if (ret != MEDIA_INFO_ERROR_NONE) {
753                 media_svc_error("error when _media_svc_get_noti_info. err = [%d]", ret);
754                 return ret;
755         }
756
757         *item = calloc(1, sizeof(media_svc_noti_item));
758         if (*item == NULL) {
759                 media_svc_error("_media_svc_get_noti_info failed : calloc");
760                 return MEDIA_INFO_ERROR_OUT_OF_MEMORY;
761         }
762
763         if (update_item == MS_MEDIA_ITEM_FILE) {
764                 if (sqlite3_column_text(sql_stmt, 0))
765                         (*item)->media_uuid = strdup((const char *)sqlite3_column_text(sql_stmt, 0));
766
767                 (*item)->media_type = sqlite3_column_int(sql_stmt, 1);
768
769                 if (sqlite3_column_text(sql_stmt, 2))
770                         (*item)->mime_type = strdup((const char *)sqlite3_column_text(sql_stmt, 2));
771         } else if (update_item == MS_MEDIA_ITEM_DIRECTORY) {
772                 if (sqlite3_column_text(sql_stmt, 0))
773                         (*item)->media_uuid = strdup((const char *)sqlite3_column_text(sql_stmt, 0));
774         }
775
776         SQLITE3_FINALIZE(sql_stmt);
777
778         return MEDIA_INFO_ERROR_NONE;
779 }