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