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