Update get_path functions
[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 <unistd.h>
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"
32
33 #define MEDIA_SVC_MAX_COMMIT_SIZE 200
34
35 typedef struct {
36         char thumbnail_path[MEDIA_SVC_PATHNAME_SIZE];
37 } media_svc_thumbnailpath_s;
38
39 static __thread GList *g_media_svc_item_validity_query_list = NULL;
40 static __thread GList *g_media_svc_insert_item_query_list = NULL;
41 __thread GList *g_media_svc_move_item_query_list = NULL;
42 static __thread GList *g_media_svc_update_item_query_list = NULL;
43 static __thread GList *g_media_svc_update_list = NULL;
44 static __thread int g_media_svc_update_list_count = 0;
45
46 static int __media_svc_count_invalid_records_with_thumbnail(sqlite3 *handle, const char *storage_id, media_svc_storage_type_e storage_type, int *count);
47 static int __media_svc_get_invalid_records_with_thumbnail(sqlite3 *handle, const char *storage_id, media_svc_storage_type_e storage_type, int count, media_svc_thumbnailpath_s *thumb_path);
48 static int __media_svc_count_invalid_folder_records_with_thumbnail(sqlite3 *handle, const char *storage_id, const char *folder_path, const char *folder_uuid, bool is_recursive, int *count);
49 static int __media_svc_get_invalid_folder_records_with_thumbnail(sqlite3 *handle, const char *storage_id, const char *folder_path, const char *folder_uuid, bool is_recursive, int count, media_svc_thumbnailpath_s *thumb_path);
50
51 static int __media_svc_count_invalid_records_with_thumbnail(sqlite3 *handle, const char *storage_id, media_svc_storage_type_e storage_type, int *count)
52 {
53         int ret = MS_MEDIA_ERR_NONE;
54         sqlite3_stmt *sql_stmt = NULL;
55         char *sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE validity=0 AND storage_type=%d AND thumbnail_path IS NOT NULL",
56                                         storage_id, storage_type);
57
58         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
59
60         if (ret != MS_MEDIA_ERR_NONE) {
61                 media_svc_error("error when __media_svc_count_invalid_records_with_thumbnail. err = [%d]", ret);
62                 return ret;
63         }
64
65         *count = sqlite3_column_int(sql_stmt, 0);
66
67         SQLITE3_FINALIZE(sql_stmt);
68
69         return MS_MEDIA_ERR_NONE;
70
71 }
72
73 static int __media_svc_get_invalid_records_with_thumbnail(sqlite3 *handle, const char *storage_id, media_svc_storage_type_e storage_type,
74                                                         int count, media_svc_thumbnailpath_s * thumb_path)
75 {
76         int ret = MS_MEDIA_ERR_NONE;
77         sqlite3_stmt *sql_stmt = NULL;
78         int idx = 0;
79
80         char *sql = sqlite3_mprintf("SELECT thumbnail_path from (select thumbnail_path, validity from '%s' WHERE storage_type=%d AND thumbnail_path IS NOT NULL GROUP BY thumbnail_path HAVING count() = 1) WHERE validity=0",
81                                         MEDIA_SVC_DB_TABLE_MEDIA, storage_type);
82
83         media_svc_debug("[SQL query] : %s", sql);
84
85         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
86         if (ret != MS_MEDIA_ERR_NONE) {
87                 media_svc_error("error when __media_svc_get_invalid_records_with_thumbnail. err = [%d]", ret);
88                 return ret;
89         }
90
91         while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
92                 _strncpy_safe(thumb_path[idx].thumbnail_path, (const char *)sqlite3_column_text(sql_stmt, 0), sizeof(thumb_path[idx]));
93                 /*media_svc_debug("thumb_path[%d]=[%s]", idx, thumb_path[idx].thumbnail_path); */
94                 idx++;
95         }
96
97         SQLITE3_FINALIZE(sql_stmt);
98
99         return MS_MEDIA_ERR_NONE;
100 }
101
102 static int __media_svc_count_invalid_folder_records_with_thumbnail(sqlite3 *handle, const char *storage_id, const char *folder_path, const char *folder_uuid, bool is_recursive, int *count)
103 {
104         int ret = MS_MEDIA_ERR_NONE;
105         sqlite3_stmt *sql_stmt = NULL;
106         char *sql = NULL;
107
108         if (is_recursive)
109                 sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE validity=0 AND path LIKE '%q/%%' AND thumbnail_path IS NOT NULL", storage_id, folder_path);
110         else
111                 sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE validity=0 AND folder_uuid = '%q' AND thumbnail_path IS NOT NULL", storage_id, folder_uuid);
112
113         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
114
115         if (ret != MS_MEDIA_ERR_NONE) {
116                 media_svc_error("error when __media_svc_count_invalid_folder_records_with_thumbnail. err = [%d]", ret);
117                 return ret;
118         }
119
120         *count = sqlite3_column_int(sql_stmt, 0);
121
122         SQLITE3_FINALIZE(sql_stmt);
123
124         return MS_MEDIA_ERR_NONE;
125
126 }
127
128 static int __media_svc_get_invalid_folder_records_with_thumbnail(sqlite3 *handle, const char *storage_id, const char *folder_path, const char *folder_uuid, bool is_recursive,
129                                                         int count, media_svc_thumbnailpath_s * thumb_path)
130 {
131         int ret = MS_MEDIA_ERR_NONE;
132         sqlite3_stmt *sql_stmt = NULL;
133         int idx = 0;
134         char *sql = NULL;
135
136         if (is_recursive)
137                 sql = sqlite3_mprintf("SELECT thumbnail_path from (select thumbnail_path, validity from '%s' WHERE path LIKE '%q/%%' AND thumbnail_path IS NOT NULL GROUP BY thumbnail_path HAVING count() = 1) WHERE validity=0", storage_id, folder_path);
138         else
139                 sql = sqlite3_mprintf("SELECT thumbnail_path from (select thumbnail_path, validity from '%s' WHERE folder_uuid = '%q' AND thumbnail_path IS NOT NULL GROUP BY thumbnail_path HAVING count() = 1) WHERE validity=0", storage_id, folder_uuid);
140
141         media_svc_debug("[SQL query] : %s", sql);
142
143         ret = _media_svc_sql_prepare_to_step_simple(handle, sql, &sql_stmt);
144         if (ret != MS_MEDIA_ERR_NONE) {
145                 media_svc_error("error when __media_svc_get_invalid_folder_records_with_thumbnail. err = [%d]", ret);
146                 return ret;
147         }
148
149         while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
150                 _strncpy_safe(thumb_path[idx].thumbnail_path, (const char *)sqlite3_column_text(sql_stmt, 0), sizeof(thumb_path[idx]));
151                 idx++;
152         }
153
154         SQLITE3_FINALIZE(sql_stmt);
155
156         return MS_MEDIA_ERR_NONE;
157 }
158
159 int _media_svc_count_record_with_path(sqlite3 *handle, const char *storage_id, const char *path, int *count)
160 {
161         int ret = MS_MEDIA_ERR_NONE;
162         sqlite3_stmt *sql_stmt = NULL;
163
164         char *sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE path='%q'", storage_id, path);
165
166         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
167
168         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
169
170         *count = sqlite3_column_int(sql_stmt, 0);
171
172         SQLITE3_FINALIZE(sql_stmt);
173
174         return MS_MEDIA_ERR_NONE;
175 }
176
177 char *_media_svc_get_thumb_default_path(uid_t uid)
178 {
179         int ret = 0;
180         char *result_path = NULL;
181
182         ret = tzplatform_set_user(uid);
183         if (ret != 0) {
184                 media_svc_error("Invalid UID[%d]", uid);
185                 return NULL;
186         } else {
187                 const char *result = tzplatform_mkpath(TZ_USER_SHARE, "media/.thumb/thumb_default.png");
188                 result_path = strndup(result, strlen(result));
189                 tzplatform_reset_user();
190         }
191
192         return result_path;
193 }
194
195 int _media_svc_insert_item_with_data(sqlite3 *handle, const char *storage_id, media_svc_content_info_s *content_info, int is_burst, bool stack_query, uid_t uid)
196 {
197         int ret = MS_MEDIA_ERR_NONE;
198         char *burst_id = NULL;
199
200         const char *db_fields = "media_uuid, path, file_name, media_type, mime_type, size, added_time, modified_time, folder_uuid, thumbnail_path, \
201                 title, album_id, album, artist, album_artist, genre, composer, year, recorded_date, copyright, track_num, description, category, keyword, location_tag, \
202                 content_name, age_rating, author, provider, last_played_time, played_count, favourite, bitrate, bitpersample, samplerate, channel, duration, \
203                 longitude, latitude, altitude, exposure_time, fnumber, iso, model, width, height, datetaken, orientation, rating, is_drm, storage_type, burst_id, timeline, weather, sync_status, \
204                 file_name_pinyin, title_pinyin, album_pinyin, artist_pinyin, album_artist_pinyin, genre_pinyin, composer_pinyin, copyright_pinyin, description_pinyin, storage_uuid, is_360";
205
206         const char *db_fields_basic = "media_uuid, path, file_name, media_type, mime_type, size, added_time, modified_time, folder_uuid, thumbnail_path, \
207                 album_id, last_played_time, played_count, favourite, is_drm, storage_type, burst_id, timeline, sync_status, file_name_pinyin, storage_uuid, is_360";
208
209         /* This sql is due to sqlite3_mprintf's wrong operation when using floating point in the text format */
210         /* This code will be removed when sqlite3_mprintf works clearly */
211         char *test_sql = sqlite3_mprintf("%f, %f, %f", content_info->media_meta.longitude, content_info->media_meta.latitude, content_info->media_meta.altitude);
212         sqlite3_free(test_sql);
213
214         if (is_burst) {
215                 int burst_id_int = 0;
216                 ret = _media_svc_get_burst_id(handle, storage_id, &burst_id_int);
217                 if (ret != MS_MEDIA_ERR_NONE)
218                         burst_id = NULL;
219
220                 if (burst_id_int > 0) {
221                         media_svc_debug("Burst id : %d", burst_id_int);
222                         burst_id = sqlite3_mprintf("%d", burst_id_int);
223                 }
224
225                 /* Get thumbnail for burst shot */
226                 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
227                 int width = 0;
228                 int height = 0;
229
230                 ret = _media_svc_request_thumbnail_with_origin_size(content_info->path, thumb_path, sizeof(thumb_path), &width, &height, uid);
231                 if (ret == MS_MEDIA_ERR_NONE) {
232                         ret = __media_svc_malloc_and_strncpy(&(content_info->thumbnail_path), thumb_path);
233                         if (ret != MS_MEDIA_ERR_NONE)
234                                 content_info->thumbnail_path = NULL;
235                 }
236
237                 if (content_info->media_meta.width <= 0)
238                         content_info->media_meta.width = width;
239
240                 if (content_info->media_meta.height <= 0)
241                         content_info->media_meta.height = height;
242         }
243
244         /*Update Pinyin If Support Pinyin*/
245         if (_media_svc_check_pinyin_support()) {
246                 if (STRING_VALID(content_info->file_name))
247                         _media_svc_get_pinyin_str(content_info->file_name, &content_info->file_name_pinyin);
248                 if (STRING_VALID(content_info->media_meta.title))
249                         _media_svc_get_pinyin_str(content_info->media_meta.title, &content_info->media_meta.title_pinyin);
250                 if (STRING_VALID(content_info->media_meta.album))
251                         _media_svc_get_pinyin_str(content_info->media_meta.album, &content_info->media_meta.album_pinyin);
252                 if (STRING_VALID(content_info->media_meta.artist))
253                         _media_svc_get_pinyin_str(content_info->media_meta.artist, &content_info->media_meta.artist_pinyin);
254                 if (STRING_VALID(content_info->media_meta.album_artist))
255                         _media_svc_get_pinyin_str(content_info->media_meta.album_artist, &content_info->media_meta.album_artist_pinyin);
256                 if (STRING_VALID(content_info->media_meta.genre))
257                         _media_svc_get_pinyin_str(content_info->media_meta.genre, &content_info->media_meta.genre_pinyin);
258                 if (STRING_VALID(content_info->media_meta.composer))
259                         _media_svc_get_pinyin_str(content_info->media_meta.composer, &content_info->media_meta.composer_pinyin);
260                 if (STRING_VALID(content_info->media_meta.copyright))
261                         _media_svc_get_pinyin_str(content_info->media_meta.copyright, &content_info->media_meta.copyright_pinyin);
262                 if (STRING_VALID(content_info->media_meta.description))
263                         _media_svc_get_pinyin_str(content_info->media_meta.description, &content_info->media_meta.description_pinyin);
264         }
265
266         char *sql = sqlite3_mprintf("INSERT INTO '%s' (%s) VALUES (%Q, %Q, %Q, %d, %Q, %lld, %d, %d, %Q, \
267                 %Q, %Q, %d, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %d, %d, %d, \
268                 %d, %d, %d, %d, %d, %.6f, %.6f, %.6f, %Q, %.6f, %d, %Q, %d, %d, %Q, %d, \
269                 %d, %d, %d, %Q, %d, %Q, %d, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %d);",
270                                 content_info->storage_uuid, db_fields,
271                                 content_info->media_uuid,
272                                 content_info->path,
273                                 content_info->file_name,
274                                 content_info->media_type,
275                                 content_info->mime_type,
276                                 content_info->size,
277                                 content_info->added_time,
278                                 content_info->modified_time,
279                                 content_info->folder_uuid,                      /* 1st line */
280                                 content_info->thumbnail_path,
281                                 content_info->media_meta.title,
282                                 content_info->album_id,
283                                 content_info->media_meta.album,
284                                 content_info->media_meta.artist,
285                                 content_info->media_meta.album_artist,
286                                 content_info->media_meta.genre,
287                                 content_info->media_meta.composer,
288                                 content_info->media_meta.year,
289                                 content_info->media_meta.recorded_date,
290                                 content_info->media_meta.copyright,
291                                 content_info->media_meta.track_num,
292                                 content_info->media_meta.description,
293                                 content_info->media_meta.category,
294                                 content_info->media_meta.keyword,
295                                 content_info->media_meta.location_tag,
296                                 content_info->media_meta.content_name,
297                                 content_info->media_meta.age_rating,
298                                 content_info->media_meta.author,
299                                 content_info->media_meta.provider,
300                                 content_info->last_played_time,
301                                 content_info->played_count,
302                                 content_info->favourate,                /* 2nd line */
303                                 content_info->media_meta.bitrate,
304                                 content_info->media_meta.bitpersample,
305                                 content_info->media_meta.samplerate,
306                                 content_info->media_meta.channel,
307                                 content_info->media_meta.duration,
308                                 content_info->media_meta.longitude,
309                                 content_info->media_meta.latitude,
310                                 content_info->media_meta.altitude,
311                                 content_info->media_meta.exposure_time,
312                                 content_info->media_meta.fnumber,
313                                 content_info->media_meta.iso,
314                                 content_info->media_meta.model,
315                                 content_info->media_meta.width,
316                                 content_info->media_meta.height,
317                                 content_info->media_meta.datetaken,
318                                 content_info->media_meta.orientation,           /* 3rd line */
319                                 content_info->media_meta.rating,
320                                 content_info->is_drm,
321                                 content_info->storage_type,
322                                 burst_id,
323                                 content_info->timeline,
324                                 content_info->media_meta.weather,
325                                 content_info->sync_status,
326                                 content_info->file_name_pinyin,
327                                 content_info->media_meta.title_pinyin,
328                                 content_info->media_meta.album_pinyin,
329                                 content_info->media_meta.artist_pinyin,
330                                 content_info->media_meta.album_artist_pinyin,
331                                 content_info->media_meta.genre_pinyin,
332                                 content_info->media_meta.composer_pinyin,
333                                 content_info->media_meta.copyright_pinyin,
334                                 content_info->media_meta.description_pinyin,
335                                 content_info->storage_uuid,
336                                 content_info->media_meta.is_360         /* 4th line */
337                         );
338
339         /* Check query length */
340         if (sql != NULL && strlen(sql) >= SQLITE3_QUERY_MAX_LEN) {
341                 media_svc_debug("Query length is over %d byte.. So, remake query using only basic information..", SQLITE3_QUERY_MAX_LEN);
342                 SQLITE3_SAFE_FREE(sql);
343
344                 sql = sqlite3_mprintf("INSERT INTO '%s' (%s) VALUES (%Q, %Q, %Q, %d, %Q, %lld, %d, %d, %Q, %Q, %d, %d, %d, %d, %d, %d, %Q, %d, %d, %Q, %Q, %d);",
345                                                 content_info->storage_uuid, db_fields_basic,
346                                                 content_info->media_uuid,
347                                                 content_info->path,
348                                                 content_info->file_name,
349                                                 content_info->media_type,
350                                                 content_info->mime_type,
351                                                 content_info->size,
352                                                 content_info->added_time,
353                                                 content_info->modified_time,
354                                                 content_info->folder_uuid,
355                                                 content_info->thumbnail_path,
356                                                 content_info->album_id,
357                                                 content_info->last_played_time,
358                                                 content_info->played_count,
359                                                 content_info->favourate,
360                                                 content_info->is_drm,
361                                                 content_info->storage_type,
362                                                 burst_id,
363                                                 content_info->timeline,
364                                                 content_info->sync_status,
365                                                 content_info->file_name_pinyin,
366                                                 content_info->storage_uuid,
367                                                 content_info->media_meta.is_360         /* 1st line */
368                                                 );
369         }
370
371         if (burst_id) {
372                 sqlite3_free(burst_id);
373                 burst_id = NULL;
374         }
375
376         if (!stack_query) {
377                 ret = _media_svc_sql_query(sql, uid);
378                 sqlite3_free(sql);
379                 if (ret != MS_MEDIA_ERR_NONE) {
380                         media_svc_error("failed to insert item");
381                         return ret;
382                 }
383         } else {
384                 media_svc_debug("query : %s", sql);
385                 _media_svc_sql_query_add(&g_media_svc_insert_item_query_list, &sql);
386         }
387
388         return MS_MEDIA_ERR_NONE;
389 }
390
391 int _media_svc_update_meta_with_data(media_svc_content_info_s *content_info)
392 {
393         int ret = MS_MEDIA_ERR_NONE;
394
395         /* This sql is due to sqlite3_mprintf's wrong operation when using floating point in the text format */
396         /* This code will be removed when sqlite3_mprintf works clearly */
397         char *test_sql = sqlite3_mprintf("%f, %f, %f", content_info->media_meta.longitude, content_info->media_meta.latitude, content_info->media_meta.altitude);
398         sqlite3_free(test_sql);
399
400         /*Update Pinyin If Support Pinyin*/
401         if (_media_svc_check_pinyin_support()) {
402                 if (STRING_VALID(content_info->file_name))
403                         _media_svc_get_pinyin_str(content_info->file_name, &content_info->file_name_pinyin);
404                 if (STRING_VALID(content_info->media_meta.title))
405                         _media_svc_get_pinyin_str(content_info->media_meta.title, &content_info->media_meta.title_pinyin);
406                 if (STRING_VALID(content_info->media_meta.album))
407                         _media_svc_get_pinyin_str(content_info->media_meta.album, &content_info->media_meta.album_pinyin);
408                 if (STRING_VALID(content_info->media_meta.artist))
409                         _media_svc_get_pinyin_str(content_info->media_meta.artist, &content_info->media_meta.artist_pinyin);
410                 if (STRING_VALID(content_info->media_meta.album_artist))
411                         _media_svc_get_pinyin_str(content_info->media_meta.album_artist, &content_info->media_meta.album_artist_pinyin);
412                 if (STRING_VALID(content_info->media_meta.genre))
413                         _media_svc_get_pinyin_str(content_info->media_meta.genre, &content_info->media_meta.genre_pinyin);
414                 if (STRING_VALID(content_info->media_meta.composer))
415                         _media_svc_get_pinyin_str(content_info->media_meta.composer, &content_info->media_meta.composer_pinyin);
416                 if (STRING_VALID(content_info->media_meta.copyright))
417                         _media_svc_get_pinyin_str(content_info->media_meta.copyright, &content_info->media_meta.copyright_pinyin);
418                 if (STRING_VALID(content_info->media_meta.description))
419                         _media_svc_get_pinyin_str(content_info->media_meta.description, &content_info->media_meta.description_pinyin);
420         }
421
422         char *sql = sqlite3_mprintf("UPDATE %s SET title=%Q, album=%Q, artist=%Q, album_artist=%Q, genre=%Q, composer=%Q, copyright=%Q, description=%Q, \
423                 file_name_pinyin=%Q, title_pinyin=%Q, album_pinyin=%Q, artist_pinyin=%Q, album_artist_pinyin=%Q, genre_pinyin=%Q, composer_pinyin=%Q, copyright_pinyin=%Q, description_pinyin=%Q \
424                 WHERE path=%Q;",
425                                 MEDIA_SVC_DB_TABLE_MEDIA,
426                                 content_info->media_meta.title,
427                                 content_info->media_meta.album,
428                                 content_info->media_meta.artist,
429                                 content_info->media_meta.album_artist,
430                                 content_info->media_meta.genre,
431                                 content_info->media_meta.composer,
432                                 content_info->media_meta.copyright,
433                                 content_info->media_meta.description,
434                                 content_info->file_name_pinyin,
435                                 content_info->media_meta.title_pinyin,
436                                 content_info->media_meta.album_pinyin,
437                                 content_info->media_meta.artist_pinyin,
438                                 content_info->media_meta.album_artist_pinyin,
439                                 content_info->media_meta.genre_pinyin,
440                                 content_info->media_meta.composer_pinyin,
441                                 content_info->media_meta.copyright_pinyin,
442                                 content_info->media_meta.description_pinyin,
443                                 content_info->path
444                                 );
445
446         if (sql != NULL) {
447                 media_svc_debug("query : %s", sql);
448                 _media_svc_sql_query_add(&g_media_svc_update_item_query_list, &sql);
449         } else {
450                 media_svc_error("sqlite3_mprintf failed");
451                 ret = MS_MEDIA_ERR_OUT_OF_MEMORY;
452         }
453
454         return ret;
455 }
456
457 int _media_svc_update_item_with_data(const char *storage_id, media_svc_content_info_s *content_info, uid_t uid)
458 {
459         int ret = MS_MEDIA_ERR_NONE;
460
461         /* This sql is due to sqlite3_mprintf's wrong operation when using floating point in the text format */
462         /* This code will be removed when sqlite3_mprintf works clearly */
463         char *test_sql = sqlite3_mprintf("%f, %f, %f", content_info->media_meta.longitude, content_info->media_meta.latitude, content_info->media_meta.altitude);
464         sqlite3_free(test_sql);
465
466         /*Update Pinyin If Support Pinyin*/
467         if (_media_svc_check_pinyin_support()) {
468                 if (STRING_VALID(content_info->file_name))
469                         _media_svc_get_pinyin_str(content_info->file_name, &content_info->file_name_pinyin);
470                 if (STRING_VALID(content_info->media_meta.title))
471                         _media_svc_get_pinyin_str(content_info->media_meta.title, &content_info->media_meta.title_pinyin);
472                 if (STRING_VALID(content_info->media_meta.album))
473                         _media_svc_get_pinyin_str(content_info->media_meta.album, &content_info->media_meta.album_pinyin);
474                 if (STRING_VALID(content_info->media_meta.artist))
475                         _media_svc_get_pinyin_str(content_info->media_meta.artist, &content_info->media_meta.artist_pinyin);
476                 if (STRING_VALID(content_info->media_meta.album_artist))
477                         _media_svc_get_pinyin_str(content_info->media_meta.album_artist, &content_info->media_meta.album_artist_pinyin);
478                 if (STRING_VALID(content_info->media_meta.genre))
479                         _media_svc_get_pinyin_str(content_info->media_meta.genre, &content_info->media_meta.genre_pinyin);
480                 if (STRING_VALID(content_info->media_meta.composer))
481                         _media_svc_get_pinyin_str(content_info->media_meta.composer, &content_info->media_meta.composer_pinyin);
482                 if (STRING_VALID(content_info->media_meta.copyright))
483                         _media_svc_get_pinyin_str(content_info->media_meta.copyright, &content_info->media_meta.copyright_pinyin);
484                 if (STRING_VALID(content_info->media_meta.description))
485                         _media_svc_get_pinyin_str(content_info->media_meta.description, &content_info->media_meta.description_pinyin);
486         }
487
488         char *sql = sqlite3_mprintf("UPDATE '%s' SET size=%lld, modified_time=%d, thumbnail_path=%Q, title=%Q, album_id=%d, album=%Q, artist=%Q, album_artist=%Q, genre=%Q, \
489                 composer=%Q, year=%Q, recorded_date=%Q, copyright=%Q, track_num=%Q, description=%Q, bitrate=%d, bitpersample=%d, samplerate=%d, channel=%d, duration=%d, \
490                 longitude=%f, latitude=%f, altitude=%f, exposure_time=%Q, fnumber=%f, iso=%d, model=%Q, width=%d, height=%d, datetaken=%Q, orientation=%d WHERE path=%Q;",
491                                 storage_id,
492                                 content_info->size,
493                                 content_info->modified_time,
494                                 content_info->thumbnail_path,
495                                 content_info->media_meta.title,
496                                 content_info->album_id,
497                                 content_info->media_meta.album,
498                                 content_info->media_meta.artist,
499                                 content_info->media_meta.album_artist,
500                                 content_info->media_meta.genre,
501                                 content_info->media_meta.composer,
502                                 content_info->media_meta.year,
503                                 content_info->media_meta.recorded_date,
504                                 content_info->media_meta.copyright,
505                                 content_info->media_meta.track_num,
506                                 content_info->media_meta.description,
507                                 content_info->media_meta.bitrate,
508                                 content_info->media_meta.bitpersample,
509                                 content_info->media_meta.samplerate,
510                                 content_info->media_meta.channel,
511                                 content_info->media_meta.duration,
512                                 content_info->media_meta.longitude,
513                                 content_info->media_meta.latitude,
514                                 content_info->media_meta.altitude,
515                                 content_info->media_meta.exposure_time,
516                                 content_info->media_meta.fnumber,
517                                 content_info->media_meta.iso,
518                                 content_info->media_meta.model,
519                                 content_info->media_meta.width,
520                                 content_info->media_meta.height,
521                                 content_info->media_meta.datetaken,
522                                 content_info->media_meta.orientation,
523                                 content_info->path
524                                 );
525
526         ret = _media_svc_sql_query(sql, uid);
527         sqlite3_free(sql);
528
529         return ret;
530 }
531 int _media_svc_get_thumbnail_path_by_path(sqlite3 *handle, const char *storage_id, const char *path, char *thumbnail_path)
532 {
533         int ret = MS_MEDIA_ERR_NONE;
534         sqlite3_stmt *sql_stmt = NULL;
535
536         char *sql = sqlite3_mprintf("SELECT thumbnail_path FROM '%s' WHERE path='%q'", storage_id, path);
537
538         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
539
540         if (ret != MS_MEDIA_ERR_NONE) {
541                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
542                         media_svc_debug("there is no thumbnail.");
543                 else
544                         media_svc_error("error when _media_svc_get_thumbnail_path_by_path. err = [%d]", ret);
545
546                 return ret;
547         }
548
549         _strncpy_safe(thumbnail_path, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_PATHNAME_SIZE);
550
551         SQLITE3_FINALIZE(sql_stmt);
552
553         return MS_MEDIA_ERR_NONE;
554 }
555
556 int _media_svc_get_media_type_by_path(sqlite3 *handle, const char *storage_id, const char *path, int *media_type)
557 {
558         int ret = MS_MEDIA_ERR_NONE;
559         sqlite3_stmt *sql_stmt = NULL;
560
561         char *sql = sqlite3_mprintf("SELECT media_type FROM '%s' WHERE path='%q'", storage_id, path);
562
563         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
564
565         if (ret != MS_MEDIA_ERR_NONE) {
566                 media_svc_error("error when _media_svc_get_media_type_by_path. err = [%d]", ret);
567                 return ret;
568         }
569
570         *media_type = sqlite3_column_int(sql_stmt, 0);
571
572         SQLITE3_FINALIZE(sql_stmt);
573
574         return MS_MEDIA_ERR_NONE;
575 }
576
577 int _media_svc_delete_item_by_path(const char *storage_id, const char *path, bool stack_query, uid_t uid)
578 {
579         int ret = MS_MEDIA_ERR_NONE;
580         char *sql = sqlite3_mprintf("DELETE FROM '%s' WHERE path='%q';", storage_id, path);
581
582         if (!stack_query) {
583                 ret = _media_svc_sql_query(sql, uid);
584                 sqlite3_free(sql);
585                 if (ret != MS_MEDIA_ERR_NONE) {
586                         media_svc_error("failed to delete item");
587                         return ret;
588                 }
589         } else {
590                 media_svc_debug("query : %s", sql);
591                 _media_svc_sql_query_add(&g_media_svc_insert_item_query_list, &sql);
592         }
593
594         return ret;
595 }
596
597 int _media_svc_truncate_table(const char *storage_id, media_svc_storage_type_e storage_type, uid_t uid)
598 {
599         int ret = MS_MEDIA_ERR_NONE;
600
601         char *sql = sqlite3_mprintf("DELETE FROM '%s' where storage_type=%d;", storage_id, storage_type);
602
603         ret = _media_svc_sql_query(sql, uid);
604         sqlite3_free(sql);
605
606         return ret;
607 }
608
609 int _media_svc_delete_invalid_items(sqlite3 *handle, const char *storage_id, media_svc_storage_type_e storage_type, uid_t uid)
610 {
611         int idx = 0;
612         media_svc_thumbnailpath_s *thumbpath_record = NULL;
613         int invalid_count = 0;
614         int ret = MS_MEDIA_ERR_NONE;
615
616         ret = __media_svc_count_invalid_records_with_thumbnail(handle, storage_id, storage_type, &invalid_count);
617         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
618
619         media_svc_debug("invalid count: %d", invalid_count);
620
621         if (invalid_count > 0) {
622                 thumbpath_record = (media_svc_thumbnailpath_s *)calloc(invalid_count, sizeof(media_svc_thumbnailpath_s));
623                 if (thumbpath_record == NULL) {
624                         media_svc_error("fail to memory allocation");
625                         return MS_MEDIA_ERR_OUT_OF_MEMORY;
626                 }
627
628                 ret = __media_svc_get_invalid_records_with_thumbnail(handle, storage_id, storage_type, invalid_count, thumbpath_record);
629                 if (ret != MS_MEDIA_ERR_NONE) {
630                         media_svc_error("error when get thumbnail record");
631                         SAFE_FREE(thumbpath_record);
632                         return ret;
633                 }
634         } else {
635                 media_svc_debug("There is no item with thumbnail");
636         }
637
638         char *sql = sqlite3_mprintf("DELETE FROM '%s' WHERE validity = 0;", storage_id);
639
640         ret = _media_svc_sql_query(sql, uid);
641         sqlite3_free(sql);
642         if (ret != MS_MEDIA_ERR_NONE) {
643                 SAFE_FREE(thumbpath_record);
644                 return ret;
645         }
646
647         /*Delete thumbnails*/
648         char *default_thumbnail_path = _media_svc_get_thumb_default_path(uid);
649         for (idx = 0; idx < invalid_count; idx++) {
650                 if ((strlen(thumbpath_record[idx].thumbnail_path) > 0) && (STRING_VALID(default_thumbnail_path)) && (strncmp(thumbpath_record[idx].thumbnail_path, default_thumbnail_path, strlen(default_thumbnail_path)) != 0)) {
651                         ret = _media_svc_remove_file(thumbpath_record[idx].thumbnail_path);
652                         if (ret != MS_MEDIA_ERR_NONE)
653                                 media_svc_error("fail to remove thumbnail file.");
654                 }
655         }
656
657         SAFE_FREE(thumbpath_record);
658         SAFE_FREE(default_thumbnail_path);
659
660         return MS_MEDIA_ERR_NONE;
661 }
662
663 int _media_svc_delete_invalid_folder_items(sqlite3 *handle, const char *storage_id, const char *folder_path, bool is_recursive, uid_t uid)
664 {
665         int idx = 0;
666         media_svc_thumbnailpath_s *thumbpath_record = NULL;
667         int invalid_count = 0;
668         int ret = MS_MEDIA_ERR_NONE;
669         char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
670         char *sql = NULL;
671
672         /*get folder uuid from DB*/
673         ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, folder_path, folder_uuid, uid);
674         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
675
676         ret = __media_svc_count_invalid_folder_records_with_thumbnail(handle, storage_id, folder_path, folder_uuid, is_recursive, &invalid_count);
677         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
678
679         media_svc_debug("invalid count: %d", invalid_count);
680
681         if (invalid_count > 0) {
682                 thumbpath_record = (media_svc_thumbnailpath_s *)calloc(invalid_count, sizeof(media_svc_thumbnailpath_s));
683                 if (thumbpath_record == NULL) {
684                         media_svc_error("fail to memory allocation");
685                         return MS_MEDIA_ERR_OUT_OF_MEMORY;
686                 }
687
688                 ret = __media_svc_get_invalid_folder_records_with_thumbnail(handle, storage_id, folder_path, folder_uuid, is_recursive, invalid_count, thumbpath_record);
689                 if (ret != MS_MEDIA_ERR_NONE) {
690                         media_svc_error("error when get thumbnail record");
691                         SAFE_FREE(thumbpath_record);
692                         return ret;
693                 }
694         } else {
695                 media_svc_debug("There is no item with thumbnail");
696         }
697
698         if (is_recursive)
699                 sql = sqlite3_mprintf("DELETE FROM '%s' WHERE validity = 0 AND path LIKE '%q/%%';", storage_id, folder_path);
700         else
701                 sql = sqlite3_mprintf("DELETE FROM '%s' WHERE validity = 0 AND folder_uuid='%q';", storage_id, folder_uuid);
702
703         ret = _media_svc_sql_query(sql, uid);
704         sqlite3_free(sql);
705         if (ret != MS_MEDIA_ERR_NONE) {
706                 SAFE_FREE(thumbpath_record);
707                 return ret;
708         }
709
710         /*Delete thumbnails*/
711         if (thumbpath_record != NULL)    {
712                 char *default_thumbnail_path = _media_svc_get_thumb_default_path(uid);
713                 if (STRING_VALID(default_thumbnail_path)) {
714                         for (idx = 0; idx < invalid_count; idx++) {
715                                 if ((strlen(thumbpath_record[idx].thumbnail_path) > 0) && (strncmp(thumbpath_record[idx].thumbnail_path, default_thumbnail_path, strlen(default_thumbnail_path)) != 0)) {
716                                         ret = _media_svc_remove_file(thumbpath_record[idx].thumbnail_path);
717                                         if (ret != MS_MEDIA_ERR_NONE)
718                                                 media_svc_error("fail to remove thumbnail file.");
719                                 }
720                         }
721                 }
722
723                 SAFE_FREE(thumbpath_record);
724                 SAFE_FREE(default_thumbnail_path);
725         }
726
727         return MS_MEDIA_ERR_NONE;
728 }
729
730 int _media_svc_update_item_validity(const char *storage_id, const char *path, int validity, bool stack_query, uid_t uid)
731 {
732         int ret = MS_MEDIA_ERR_NONE;
733
734         char *sql = sqlite3_mprintf("UPDATE '%s' SET validity=%d WHERE path= '%q';", storage_id, validity, path);
735
736         if (!stack_query) {
737                 ret = _media_svc_sql_query(sql, uid);
738                 sqlite3_free(sql);
739         } else {
740                 _media_svc_sql_query_add(&g_media_svc_item_validity_query_list, &sql);
741         }
742
743         return ret;
744 }
745
746 int _media_svc_update_thumbnail_path(const char *storage_id, const char *path, const char *thumb_path, uid_t uid)
747 {
748         int ret = MS_MEDIA_ERR_NONE;
749
750         char *sql = sqlite3_mprintf("UPDATE '%s' SET thumbnail_path=%Q WHERE path= %Q;", storage_id, thumb_path, path);
751
752         ret = _media_svc_sql_query(sql, uid);
753         sqlite3_free(sql);
754
755         return ret;
756 }
757
758 int _media_svc_update_storage_item_validity(const char *storage_id, media_svc_storage_type_e storage_type, int validity, uid_t uid)
759 {
760         int ret = MS_MEDIA_ERR_NONE;
761
762         char *sql = sqlite3_mprintf("UPDATE '%s' SET validity=%d WHERE storage_type=%d;", storage_id, validity, storage_type);
763
764         ret = _media_svc_sql_query(sql, uid);
765         sqlite3_free(sql);
766
767         return ret;
768 }
769
770 int _media_svc_update_folder_item_validity(sqlite3 *handle, const char *storage_id, const char *folder_path, int validity, uid_t uid)
771 {
772         int ret = MS_MEDIA_ERR_NONE;
773         char *sql = NULL;
774         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
775         sqlite3_stmt *sql_stmt = NULL;
776
777         /*Get folder ID*/
778         sql = sqlite3_mprintf("SELECT folder_uuid FROM '%s' WHERE path='%q' AND storage_uuid='%q'", MEDIA_SVC_DB_TABLE_FOLDER, folder_path, storage_id);
779
780         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
781         if (ret != MS_MEDIA_ERR_NONE) {
782                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
783                         media_svc_debug("folder not exist");
784                 else
785                         media_svc_error("error when get folder_id. err = [%d]", ret);
786
787                 return ret;
788         }
789
790         _strncpy_safe(folder_uuid, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE + 1);
791         SQLITE3_FINALIZE(sql_stmt);
792
793         /*Update folder item validity*/
794         sql = sqlite3_mprintf("UPDATE '%s' SET validity=%d WHERE folder_uuid='%q';", storage_id, validity, folder_uuid);
795
796         ret = _media_svc_sql_query(sql, uid);
797         sqlite3_free(sql);
798
799         return ret;
800 }
801
802 int _media_svc_update_recursive_folder_item_validity(const char *storage_id, const char *folder_path, int validity, uid_t uid)
803 {
804         int ret = MS_MEDIA_ERR_NONE;
805
806         /*Update folder item validity*/
807         char *sql = sqlite3_mprintf("UPDATE '%s' SET validity=%d WHERE (storage_type = 0 OR storage_type = 1) AND path LIKE '%q/%%';", storage_id, validity, folder_path);
808
809         ret = _media_svc_sql_query(sql, uid);
810         sqlite3_free(sql);
811
812         return ret;
813 }
814
815 int _media_svc_update_item_by_path(const char *storage_id, const char *src_path, media_svc_storage_type_e dest_storage, const char *dest_path,
816                                 const char *file_name, int modified_time, const char *folder_uuid, const char *thumb_path, bool stack_query, uid_t uid)
817 {
818         /* update path, filename, modified_time, folder_uuid, thumbnail_path, */
819         /* played_count, last_played_time, last_played_position, favourite, storaget_type*/
820
821         int ret = MS_MEDIA_ERR_NONE;
822         char *sql = NULL;
823
824         if (thumb_path != NULL) {
825                 sql = sqlite3_mprintf("UPDATE '%s' SET path=%Q, file_name=%Q, modified_time=%d, folder_uuid=%Q, thumbnail_path=%Q, storage_type=%d, \
826                                         played_count=0, last_played_time=0, last_played_position=0 WHERE path=%Q;",
827                                         storage_id, dest_path, file_name, modified_time, folder_uuid, thumb_path, dest_storage, src_path);
828         } else {
829                 sql = sqlite3_mprintf("UPDATE '%s' SET path=%Q, file_name=%Q, modified_time=%d, folder_uuid=%Q, storage_type=%d, \
830                                         played_count=0, last_played_time=0, last_played_position=0 WHERE path=%Q;",
831                                         storage_id, dest_path, file_name, modified_time, folder_uuid, dest_storage, src_path);
832         }
833
834         if (!stack_query) {
835                 ret = _media_svc_sql_query(sql, uid);
836                 sqlite3_free(sql);
837         } else {
838                 _media_svc_sql_query_add(&g_media_svc_move_item_query_list, &sql);
839         }
840
841         return ret;
842 }
843
844 int _media_svc_list_query_do(media_svc_query_type_e query_type, uid_t uid)
845 {
846         int ret = MS_MEDIA_ERR_NONE;
847
848         ret = _media_svc_sql_begin_trans(uid);
849         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
850
851         if (query_type == MEDIA_SVC_QUERY_SET_ITEM_VALIDITY)
852                 ret = _media_svc_sql_query_list(&g_media_svc_item_validity_query_list, uid);
853         else if (query_type == MEDIA_SVC_QUERY_MOVE_ITEM)
854                 ret = _media_svc_sql_query_list(&g_media_svc_move_item_query_list, uid);
855         else if (query_type == MEDIA_SVC_QUERY_INSERT_ITEM)
856                 ret = _media_svc_sql_query_list(&g_media_svc_insert_item_query_list, uid);
857         else if (query_type == MEDIA_SVC_QUERY_UPDATE_ITEM)
858                 ret = _media_svc_sql_query_list(&g_media_svc_update_item_query_list, uid);
859         else if (query_type == MEDIA_SVC_QUERY_INSERT_FOLDER)
860                 ret = _media_svc_sql_query_list(_media_svc_get_folder_list_ptr(), uid);
861         else if (query_type == MEDIA_SVC_QUERY_UPDATE_COMMON)
862                 ret = _media_svc_sql_query_list(&g_media_svc_update_list, uid);
863         else
864                 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
865
866         if (ret != MS_MEDIA_ERR_NONE) {
867                 media_svc_error("_media_svc_list_query_do failed. start rollback");
868                 _media_svc_sql_rollback_trans(uid);
869                 return ret;
870         }
871
872         ret = _media_svc_sql_end_trans(uid);
873         if (ret != MS_MEDIA_ERR_NONE) {
874                 media_svc_error("mb_svc_sqlite3_commit_trans failed.. Now start to rollback");
875                 _media_svc_sql_rollback_trans(uid);
876                 return ret;
877         }
878
879         return MS_MEDIA_ERR_NONE;
880 }
881
882 int _media_svc_append_query_list(const char *query, uid_t uid)
883 {
884         int ret = MS_MEDIA_ERR_NONE;
885
886         g_media_svc_update_list = g_list_append(g_media_svc_update_list, (gpointer)query);
887
888         g_media_svc_update_list_count++;
889
890         if (g_media_svc_update_list_count >= MEDIA_SVC_MAX_COMMIT_SIZE) {
891                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_COMMON, uid);
892                 g_media_svc_update_list_count = 0;
893         }
894
895         return ret;
896 }
897
898 int _media_svc_get_burst_id(sqlite3 *handle, const char *storage_id, int *id)
899 {
900         int ret = MS_MEDIA_ERR_NONE;
901         int cur_id = -1;
902         sqlite3_stmt *sql_stmt = NULL;
903         char *sql = sqlite3_mprintf("SELECT max(CAST(burst_id AS INTEGER)) FROM '%s'", storage_id);
904
905         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
906
907         if (ret != MS_MEDIA_ERR_NONE) {
908                 media_svc_error("error when _media_svc_get_burst_id. err = [%d]", ret);
909                 return ret;
910         }
911
912         cur_id = sqlite3_column_int(sql_stmt, 0);
913         *id = ++cur_id;
914         SQLITE3_FINALIZE(sql_stmt);
915
916         return MS_MEDIA_ERR_NONE;
917 }
918
919 int _media_svc_get_noti_info(sqlite3 *handle, const char *storage_id, const char *path, int update_item, media_svc_noti_item **item)
920 {
921         int ret = MS_MEDIA_ERR_NONE;
922         sqlite3_stmt *sql_stmt = NULL;
923         char *sql = NULL;
924         int is_root_dir = FALSE;
925
926         if (item == NULL) {
927                 media_svc_error("invalid parameter");
928                 return MS_MEDIA_ERR_INVALID_PARAMETER;
929         }
930
931         if (update_item == MS_MEDIA_ITEM_FILE)
932                 sql = sqlite3_mprintf("SELECT media_uuid, media_type, mime_type FROM '%s' WHERE path=%Q", storage_id, path);
933         else if (update_item == MS_MEDIA_ITEM_DIRECTORY)
934                 sql = sqlite3_mprintf("SELECT folder_uuid FROM '%s' WHERE path=%Q AND storage_uuid='%s'", MEDIA_SVC_DB_TABLE_FOLDER, path, storage_id);
935         else {
936                 media_svc_error("_media_svc_get_noti_info failed : update item");
937                 return MS_MEDIA_ERR_INVALID_PARAMETER;
938         }
939
940         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
941
942         if (ret != MS_MEDIA_ERR_NONE) {
943                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD && update_item == MS_MEDIA_ITEM_DIRECTORY) {
944                         media_svc_debug("This is root directory of media");
945                         sql_stmt = NULL;
946                         is_root_dir = TRUE;
947                 } else {
948                         media_svc_error("error when _media_svc_get_noti_info. err = [%d]", ret);
949                         return ret;
950                 }
951         }
952
953         *item = calloc(1, sizeof(media_svc_noti_item));
954         if (*item == NULL) {
955                 media_svc_error("_media_svc_get_noti_info failed : calloc");
956                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
957         }
958
959         if (update_item == MS_MEDIA_ITEM_FILE) {
960                 (*item)->media_uuid = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
961                 (*item)->media_type = sqlite3_column_int(sql_stmt, 1);
962                 (*item)->mime_type = g_strdup((const char *)sqlite3_column_text(sql_stmt, 2));
963         } else if (update_item == MS_MEDIA_ITEM_DIRECTORY) {
964                 if (is_root_dir)
965                         (*item)->media_uuid = NULL;
966                 else
967                         (*item)->media_uuid = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
968         }
969
970         SQLITE3_FINALIZE(sql_stmt);
971
972         return MS_MEDIA_ERR_NONE;
973 }
974
975 int _media_svc_count_invalid_folder_items(sqlite3 *handle, const char *storage_id, const char *folder_path, int *count)
976 {
977         int ret = MS_MEDIA_ERR_NONE;
978         sqlite3_stmt *sql_stmt = NULL;
979
980         char *sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE validity=0 AND path LIKE '%q/%%'", storage_id, folder_path);
981
982         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
983
984         if (ret != MS_MEDIA_ERR_NONE) {
985                 media_svc_error("error when _media_svc_count_invalid_folder_items. err = [%d]", ret);
986                 return ret;
987         }
988
989         *count = sqlite3_column_int(sql_stmt, 0);
990
991         SQLITE3_FINALIZE(sql_stmt);
992
993         return MS_MEDIA_ERR_NONE;
994 }
995
996 int _media_svc_get_thumbnail_count(sqlite3 *handle, const char *storage_id, const char *thumb_path, int *count)
997 {
998         int ret = MS_MEDIA_ERR_NONE;
999         sqlite3_stmt *sql_stmt = NULL;
1000         char *sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE thumbnail_path=%Q", storage_id, thumb_path);
1001
1002         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
1003
1004         if (ret != MS_MEDIA_ERR_NONE) {
1005                 media_svc_error("error when _media_svc_get_thumbnail_count. err = [%d]", ret);
1006                 return ret;
1007         }
1008
1009         *count = sqlite3_column_int(sql_stmt, 0);
1010
1011         SQLITE3_FINALIZE(sql_stmt);
1012
1013         return MS_MEDIA_ERR_NONE;
1014 }
1015
1016 int _media_svc_get_fileinfo_by_path(sqlite3 *handle, const char *storage_id, const char *path, time_t *modified_time, unsigned long long *size)
1017 {
1018         int ret = MS_MEDIA_ERR_NONE;
1019         sqlite3_stmt *sql_stmt = NULL;
1020         char *sql = sqlite3_mprintf("SELECT modified_time, size FROM '%s' WHERE path='%q'", storage_id, path);
1021
1022         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
1023
1024         if (ret != MS_MEDIA_ERR_NONE) {
1025                 media_svc_error("error when _media_svc_get_fileinfo_by_path. err = [%d]", ret);
1026                 return ret;
1027         }
1028
1029         *modified_time = (int)sqlite3_column_int(sql_stmt, 0);
1030         *size = (unsigned long long)sqlite3_column_int64(sql_stmt, 1);
1031
1032         SQLITE3_FINALIZE(sql_stmt);
1033
1034         return MS_MEDIA_ERR_NONE;
1035 }
1036
1037 int _media_svc_change_validity_item_batch(const char *storage_id, const char *path, int des_validity, int src_validity, uid_t uid)
1038 {
1039         int ret = MS_MEDIA_ERR_NONE;
1040
1041         char *sql = sqlite3_mprintf("UPDATE '%s' SET validity=%d WHERE validity=%d AND path LIKE '%q%%'", storage_id, des_validity, src_validity, path);
1042
1043         ret = _media_svc_sql_query(sql, uid);
1044         sqlite3_free(sql);
1045
1046         return ret;
1047 }
1048
1049 int _media_svc_insert_item_pass1(sqlite3 *handle, const char *storage_id, media_svc_content_info_s *content_info, int is_burst, bool stack_query, uid_t uid)
1050 {
1051         int ret = MS_MEDIA_ERR_NONE;
1052         char *burst_id = NULL;
1053
1054         char * db_fields = (char *)"media_uuid, folder_uuid, path, file_name, media_type, mime_type, size, added_time, modified_time, is_drm, storage_type, timeline, burst_id, storage_uuid";
1055
1056         char *sql = sqlite3_mprintf("INSERT INTO '%s' (%s) VALUES (%Q, %Q, %Q, %Q, %d, %Q, %lld, %d, %d, %d, %d, %d, %Q, %Q);",
1057                 storage_id, db_fields,
1058                 content_info->media_uuid,
1059                 content_info->folder_uuid,
1060                 content_info->path,
1061                 content_info->file_name,
1062                 content_info->media_type,
1063                 content_info->mime_type,
1064                 content_info->size,
1065                 content_info->added_time,
1066                 content_info->modified_time,
1067                 content_info->is_drm,
1068                 content_info->storage_type,
1069                 content_info->timeline,
1070                 burst_id,
1071                 storage_id
1072                 );
1073 #if 0
1074         if (burst_id) {
1075                 sqlite3_free(burst_id);
1076                 burst_id = NULL;
1077         }
1078 #endif
1079         media_svc_debug("MAKE PASS 1 QUERY END");
1080
1081         if (!stack_query) {
1082                 ret = _media_svc_sql_query(sql, uid);
1083                 sqlite3_free(sql);
1084                 if (ret != MS_MEDIA_ERR_NONE) {
1085                         media_svc_error("failed to insert item");
1086                         return ret;
1087                 }
1088         } else {
1089                 media_svc_debug("query : %s", sql);
1090                 _media_svc_sql_query_add(&g_media_svc_insert_item_query_list, &sql);
1091         }
1092         media_svc_debug("END");
1093         return MS_MEDIA_ERR_NONE;
1094 }
1095
1096 int _media_svc_insert_item_pass2(const char *storage_id, media_svc_content_info_s *content_info, int is_burst, bool stack_query, uid_t uid)
1097 {
1098         int ret = MS_MEDIA_ERR_NONE;
1099
1100         media_svc_debug_fenter();
1101
1102         /*Update Pinyin If Support Pinyin*/
1103         if (_media_svc_check_pinyin_support()) {
1104                 if (STRING_VALID(content_info->file_name))
1105                         _media_svc_get_pinyin_str(content_info->file_name, &content_info->file_name_pinyin);
1106                 if (STRING_VALID(content_info->media_meta.title))
1107                         _media_svc_get_pinyin_str(content_info->media_meta.title, &content_info->media_meta.title_pinyin);
1108                 if (STRING_VALID(content_info->media_meta.album))
1109                         _media_svc_get_pinyin_str(content_info->media_meta.album, &content_info->media_meta.album_pinyin);
1110                 if (STRING_VALID(content_info->media_meta.artist))
1111                         _media_svc_get_pinyin_str(content_info->media_meta.artist, &content_info->media_meta.artist_pinyin);
1112                 if (STRING_VALID(content_info->media_meta.album_artist))
1113                         _media_svc_get_pinyin_str(content_info->media_meta.album_artist, &content_info->media_meta.album_artist_pinyin);
1114                 if (STRING_VALID(content_info->media_meta.genre))
1115                         _media_svc_get_pinyin_str(content_info->media_meta.genre, &content_info->media_meta.genre_pinyin);
1116                 if (STRING_VALID(content_info->media_meta.composer))
1117                         _media_svc_get_pinyin_str(content_info->media_meta.composer, &content_info->media_meta.composer_pinyin);
1118                 if (STRING_VALID(content_info->media_meta.copyright))
1119                         _media_svc_get_pinyin_str(content_info->media_meta.copyright, &content_info->media_meta.copyright_pinyin);
1120                 if (STRING_VALID(content_info->media_meta.description))
1121                         _media_svc_get_pinyin_str(content_info->media_meta.description, &content_info->media_meta.description_pinyin);
1122         }
1123
1124         char *sql = sqlite3_mprintf("UPDATE '%s' SET \
1125                 thumbnail_path=%Q, title=%Q, album_id=%d, album=%Q, artist=%Q, album_artist=%Q, genre=%Q, composer=%Q, year=%Q, \
1126                 recorded_date=%Q, copyright=%Q, track_num=%Q, description=%Q, bitrate=%d, bitpersample=%d, samplerate=%d, channel=%d, \
1127                 duration=%d, longitude=%.6f, latitude=%.6f, altitude=%.6f, width=%d, height=%d, datetaken=%Q, orientation=%d, exposure_time=%Q,\
1128                 fnumber=%.6f, iso=%d, model=%Q, rating=%d, weather=%Q, file_name_pinyin=%Q, title_pinyin=%Q, album_pinyin=%Q, \
1129                 artist_pinyin=%Q, album_artist_pinyin=%Q, genre_pinyin=%Q, composer_pinyin=%Q, copyright_pinyin=%Q, description_pinyin=%Q, is_360=%d WHERE path=%Q;",
1130                 storage_id,
1131                 /* content_info->folder_uuid, */
1132                 content_info->thumbnail_path,
1133                 content_info->media_meta.title,
1134                 content_info->album_id,
1135                 content_info->media_meta.album,
1136                 content_info->media_meta.artist,
1137                 content_info->media_meta.album_artist,
1138                 content_info->media_meta.genre,
1139                 content_info->media_meta.composer,
1140                 content_info->media_meta.year,
1141                 content_info->media_meta.recorded_date,
1142                 content_info->media_meta.copyright,
1143                 content_info->media_meta.track_num,
1144                 content_info->media_meta.description,
1145                 content_info->media_meta.bitrate,
1146                 content_info->media_meta.bitpersample,
1147                 content_info->media_meta.samplerate,
1148                 content_info->media_meta.channel,
1149                 content_info->media_meta.duration,
1150                 content_info->media_meta.longitude,
1151                 content_info->media_meta.latitude,
1152                 content_info->media_meta.altitude,
1153                 content_info->media_meta.width,
1154                 content_info->media_meta.height,
1155                 content_info->media_meta.datetaken,
1156                 content_info->media_meta.orientation,
1157                 content_info->media_meta.exposure_time,
1158                 content_info->media_meta.fnumber,
1159                 content_info->media_meta.iso,
1160                 content_info->media_meta.model,
1161                 content_info->media_meta.rating,
1162                 content_info->media_meta.weather,
1163                 content_info->file_name_pinyin,
1164                 content_info->media_meta.title_pinyin,
1165                 content_info->media_meta.album_pinyin,
1166                 content_info->media_meta.artist_pinyin,
1167                 content_info->media_meta.album_artist_pinyin,
1168                 content_info->media_meta.genre_pinyin,
1169                 content_info->media_meta.composer_pinyin,
1170                 content_info->media_meta.copyright_pinyin,
1171                 content_info->media_meta.description_pinyin,
1172                 content_info->media_meta.is_360,
1173                 content_info->path
1174                 );
1175
1176         if (!stack_query) {
1177                 ret = _media_svc_sql_query(sql, uid);
1178                 sqlite3_free(sql);
1179                 if (ret != MS_MEDIA_ERR_NONE) {
1180
1181                         media_svc_error("failed to insert item");
1182                         return ret;
1183                 }
1184         } else {
1185                 /*media_svc_debug("query : %s", sql);*/
1186                 _media_svc_sql_query_add(&g_media_svc_update_item_query_list, &sql);
1187         }
1188
1189         media_svc_debug_fleave();
1190
1191         return MS_MEDIA_ERR_NONE;
1192 }