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