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