replace strdup to g_strdup because g_strdup check input param NULL
[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                                         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         for (idx = 0; idx < invalid_count; idx++) {
641                 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)) {
642                         ret = _media_svc_remove_file(thumbpath_record[idx].thumbnail_path);
643                         if (ret != MS_MEDIA_ERR_NONE) {
644                                 media_svc_error("fail to remove thumbnail file.");
645                         }
646                 }
647         }
648
649         SAFE_FREE(thumbpath_record);
650
651         return MS_MEDIA_ERR_NONE;
652 }
653
654 int _media_svc_delete_invalid_folder_items(sqlite3 *handle, const char *storage_id, const char *folder_path, bool is_recursive, uid_t uid)
655 {
656         int idx = 0;
657         media_svc_thumbnailpath_s *thumbpath_record = NULL;
658         int invalid_count = 0;
659         int ret = MS_MEDIA_ERR_NONE;
660         char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
661         char *sql = NULL;
662
663         /*get folder uuid from DB*/
664         ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, folder_path, folder_uuid, uid);
665         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
666
667         ret = __media_svc_count_invalid_folder_records_with_thumbnail(handle, storage_id, folder_path, folder_uuid, is_recursive, &invalid_count);
668         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
669
670         media_svc_debug("invalid count: %d", invalid_count);
671
672         if (invalid_count > 0) {
673                 thumbpath_record = (media_svc_thumbnailpath_s *)calloc(invalid_count, sizeof(media_svc_thumbnailpath_s));
674                 if (thumbpath_record == NULL) {
675                         media_svc_error("fail to memory allocation");
676                         return MS_MEDIA_ERR_OUT_OF_MEMORY;
677                 }
678
679                 ret = __media_svc_get_invalid_folder_records_with_thumbnail(handle, storage_id, folder_path, folder_uuid, is_recursive, invalid_count, thumbpath_record);
680                 if (ret != MS_MEDIA_ERR_NONE) {
681                         media_svc_error("error when get thumbnail record");
682                         SAFE_FREE(thumbpath_record);
683                         return ret;
684                 }
685         } else {
686                 media_svc_debug("There is no item with thumbnail");
687         }
688
689         if (is_recursive) {
690                 sql = sqlite3_mprintf("DELETE FROM '%s' WHERE validity = 0 AND path LIKE '%q/%%'", storage_id, folder_path);
691         } else {
692                 sql = sqlite3_mprintf("DELETE FROM '%s' WHERE validity = 0 AND folder_uuid='%q'", storage_id, folder_uuid);
693         }
694
695         ret = _media_svc_sql_query(handle, sql, uid);
696         sqlite3_free(sql);
697         if (ret != MS_MEDIA_ERR_NONE) {
698                 SAFE_FREE(thumbpath_record);
699                 return ret;
700         }
701
702         /*Delete thumbnails*/
703         if (thumbpath_record != NULL)    {
704                 for (idx = 0; idx < invalid_count; idx++) {
705                         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)) {
706                                 ret = _media_svc_remove_file(thumbpath_record[idx].thumbnail_path);
707                                 if (ret != MS_MEDIA_ERR_NONE) {
708                                         media_svc_error("fail to remove thumbnail file.");
709                                 }
710                         }
711                 }
712
713                 SAFE_FREE(thumbpath_record);
714         }
715
716         return MS_MEDIA_ERR_NONE;
717 }
718
719 int _media_svc_update_item_validity(sqlite3 *handle, const char *storage_id, const char *path, int validity, bool stack_query, uid_t uid)
720 {
721         int ret = MS_MEDIA_ERR_NONE;
722
723         char *sql = sqlite3_mprintf("UPDATE '%s' SET validity=%d WHERE path= '%q'", storage_id, validity, path);
724
725         if (!stack_query) {
726                 ret = _media_svc_sql_query(handle, sql, uid);
727                 sqlite3_free(sql);
728         } else {
729                 _media_svc_sql_query_add(&g_media_svc_item_validity_query_list, &sql);
730         }
731
732         return ret;
733 }
734
735 int _media_svc_update_thumbnail_path(sqlite3 *handle, const char *storage_id, const char *path, const char *thumb_path, uid_t uid)
736 {
737         int ret = MS_MEDIA_ERR_NONE;
738
739         char *sql = sqlite3_mprintf("UPDATE '%s' SET thumbnail_path=%Q WHERE path= %Q", storage_id, thumb_path, path);
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_storage_item_validity(sqlite3 *handle, const char *storage_id, media_svc_storage_type_e storage_type, int validity, uid_t uid)
748 {
749         int ret = MS_MEDIA_ERR_NONE;
750
751         char *sql = sqlite3_mprintf("UPDATE '%s' SET validity=%d WHERE storage_type=%d", storage_id, validity, storage_type);
752
753         ret = _media_svc_sql_query(handle, sql, uid);
754         sqlite3_free(sql);
755
756         return ret;
757 }
758
759 int _media_svc_update_folder_item_validity(sqlite3 *handle, const char *storage_id, const char *folder_path, int validity, uid_t uid)
760 {
761         int ret = MS_MEDIA_ERR_NONE;
762         char *sql = NULL;
763         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
764         sqlite3_stmt *sql_stmt = NULL;
765
766         /*Get folder ID*/
767         sql = sqlite3_mprintf("SELECT folder_uuid FROM '%s' WHERE path='%q' AND storage_uuid='%q'", MEDIA_SVC_DB_TABLE_FOLDER, folder_path, storage_id);
768
769         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
770         if (ret != MS_MEDIA_ERR_NONE) {
771                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
772                         media_svc_debug("folder not exist");
773                 else
774                         media_svc_error("error when get folder_id. err = [%d]", ret);
775
776                 return ret;
777         }
778
779         _strncpy_safe(folder_uuid, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE + 1);
780         SQLITE3_FINALIZE(sql_stmt);
781
782         /*Update folder item validity*/
783         sql = sqlite3_mprintf("UPDATE '%s' SET validity=%d WHERE folder_uuid='%q'", storage_id, validity, folder_uuid);
784
785         ret = _media_svc_sql_query(handle, sql, uid);
786         sqlite3_free(sql);
787
788         return ret;
789 }
790
791 int _media_svc_update_recursive_folder_item_validity(sqlite3 *handle, const char *storage_id, const char *folder_path, int validity, uid_t uid)
792 {
793         int ret = MS_MEDIA_ERR_NONE;
794
795         /*Update folder item validity*/
796         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);
797
798         ret = _media_svc_sql_query(handle, sql, uid);
799         sqlite3_free(sql);
800
801         return ret;
802 }
803
804 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,
805                                 const char *file_name, int modified_time, const char *folder_uuid, const char *thumb_path, bool stack_query, uid_t uid)
806 {
807         /* update path, filename, modified_time, folder_uuid, thumbnail_path, */
808         /* played_count, last_played_time, last_played_position, favourite, storaget_type*/
809
810         int ret = MS_MEDIA_ERR_NONE;
811         char *sql = NULL;
812
813         if (thumb_path != NULL) {
814                 sql = sqlite3_mprintf("UPDATE '%s' SET \
815                                         path=%Q, file_name=%Q, modified_time=%d, folder_uuid=%Q, thumbnail_path=%Q, storage_type=%d, \
816                                         played_count=0, last_played_time=0, last_played_position=0 \
817                                         WHERE path=%Q",
818                                         storage_id, dest_path, file_name, modified_time, folder_uuid, thumb_path, dest_storage, src_path);
819         } else {
820                 sql = sqlite3_mprintf("UPDATE '%s' SET \
821                                         path=%Q, file_name=%Q, modified_time=%d, folder_uuid=%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, dest_storage, src_path);
825         }
826
827         if (!stack_query) {
828                 ret = _media_svc_sql_query(handle, sql, uid);
829                 sqlite3_free(sql);
830         } else {
831                 _media_svc_sql_query_add(&g_media_svc_move_item_query_list, &sql);
832         }
833
834         return ret;
835 }
836
837 int _media_svc_list_query_do(sqlite3 *handle, media_svc_query_type_e query_type, uid_t uid)
838 {
839         int ret = MS_MEDIA_ERR_NONE;
840
841         ret = _media_svc_sql_begin_trans(handle, uid);
842         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
843
844         if (query_type == MEDIA_SVC_QUERY_SET_ITEM_VALIDITY)
845                 ret = _media_svc_sql_query_list(handle, &g_media_svc_item_validity_query_list, uid);
846         else if (query_type == MEDIA_SVC_QUERY_MOVE_ITEM)
847                 ret = _media_svc_sql_query_list(handle, &g_media_svc_move_item_query_list, uid);
848         else if (query_type == MEDIA_SVC_QUERY_INSERT_ITEM)
849                 ret = _media_svc_sql_query_list(handle, &g_media_svc_insert_item_query_list, uid);
850         else if (query_type == MEDIA_SVC_QUERY_UPDATE_ITEM)
851                 ret = _media_svc_sql_query_list(handle, &g_media_svc_update_item_query_list, uid);
852         else if (query_type == MEDIA_SVC_QUERY_INSERT_FOLDER)
853                 ret = _media_svc_sql_query_list(handle,  _media_svc_get_folder_list_ptr(), uid);
854         else
855                 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
856
857         if (ret != MS_MEDIA_ERR_NONE) {
858                 media_svc_error("_media_svc_list_query_do failed. start rollback");
859                 _media_svc_sql_rollback_trans(handle, uid);
860                 return ret;
861         }
862
863         ret = _media_svc_sql_end_trans(handle, uid);
864         if (ret != MS_MEDIA_ERR_NONE) {
865                 media_svc_error("mb_svc_sqlite3_commit_trans failed.. Now start to rollback");
866                 _media_svc_sql_rollback_trans(handle, uid);
867                 return ret;
868         }
869
870         return MS_MEDIA_ERR_NONE;
871 }
872
873 int _media_svc_get_burst_id(sqlite3 *handle, const char *storage_id, int *id)
874 {
875         int ret = MS_MEDIA_ERR_NONE;
876         int cur_id = -1;
877         sqlite3_stmt *sql_stmt = NULL;
878         char *sql = sqlite3_mprintf("SELECT max(CAST(burst_id AS INTEGER)) FROM '%s'", storage_id);
879
880         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
881
882         if (ret != MS_MEDIA_ERR_NONE) {
883                 media_svc_error("error when _media_svc_get_burst_id. err = [%d]", ret);
884                 return ret;
885         }
886
887         cur_id = sqlite3_column_int(sql_stmt, 0);
888         *id = ++cur_id;
889         SQLITE3_FINALIZE(sql_stmt);
890
891         return MS_MEDIA_ERR_NONE;
892 }
893
894 int _media_svc_get_noti_info(sqlite3 *handle, const char *storage_id, const char *path, int update_item, media_svc_noti_item **item)
895 {
896         int ret = MS_MEDIA_ERR_NONE;
897         sqlite3_stmt *sql_stmt = NULL;
898         char *sql = NULL;
899         int is_root_dir = FALSE;
900
901         if (item == NULL) {
902                 media_svc_error("invalid parameter");
903                 return MS_MEDIA_ERR_INVALID_PARAMETER;
904         }
905
906         if (update_item == MS_MEDIA_ITEM_FILE) {
907                 sql = sqlite3_mprintf("SELECT media_uuid, media_type, mime_type FROM '%s' WHERE path=%Q", storage_id, path);
908         } else if (update_item == MS_MEDIA_ITEM_DIRECTORY) {
909                 sql = sqlite3_mprintf("SELECT folder_uuid FROM '%s' WHERE path=%Q AND storage_uuid='%s'", MEDIA_SVC_DB_TABLE_FOLDER, path, storage_id);
910         } else {
911                 media_svc_error("_media_svc_get_noti_info failed : update item");
912                 return MS_MEDIA_ERR_INVALID_PARAMETER;
913         }
914
915         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
916
917         if (ret != MS_MEDIA_ERR_NONE) {
918                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD && update_item == MS_MEDIA_ITEM_DIRECTORY) {
919                         media_svc_debug("This is root directory of media");
920                         sql_stmt = NULL;
921                         is_root_dir = TRUE;
922                 } else {
923                         media_svc_error("error when _media_svc_get_noti_info. err = [%d]", ret);
924                         return ret;
925                 }
926         }
927
928         *item = calloc(1, sizeof(media_svc_noti_item));
929         if (*item == NULL) {
930                 media_svc_error("_media_svc_get_noti_info failed : calloc");
931                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
932         }
933
934         if (update_item == MS_MEDIA_ITEM_FILE) {
935                 (*item)->media_uuid = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
936                 (*item)->media_type = sqlite3_column_int(sql_stmt, 1);
937                 (*item)->mime_type = g_strdup((const char *)sqlite3_column_text(sql_stmt, 2));
938         } else if (update_item == MS_MEDIA_ITEM_DIRECTORY) {
939                 if (is_root_dir) {
940                         (*item)->media_uuid = NULL;
941                 } else {
942                         (*item)->media_uuid = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
943                 }
944         }
945
946         SQLITE3_FINALIZE(sql_stmt);
947
948         return MS_MEDIA_ERR_NONE;
949 }
950
951 int _media_svc_count_invalid_folder_items(sqlite3 *handle, const char *storage_id, const char *folder_path, int *count)
952 {
953         int ret = MS_MEDIA_ERR_NONE;
954         sqlite3_stmt *sql_stmt = NULL;
955
956         char *sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE validity=0 AND path LIKE '%q/%%'", storage_id, folder_path);
957
958         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
959
960         if (ret != MS_MEDIA_ERR_NONE) {
961                 media_svc_error("error when _media_svc_count_invalid_folder_items. err = [%d]", ret);
962                 return ret;
963         }
964
965         *count = sqlite3_column_int(sql_stmt, 0);
966
967         SQLITE3_FINALIZE(sql_stmt);
968
969         return MS_MEDIA_ERR_NONE;
970 }
971
972 int _media_svc_get_thumbnail_count(sqlite3 *handle, const char *storage_id, const char *thumb_path, int *count)
973 {
974         int ret = MS_MEDIA_ERR_NONE;
975         sqlite3_stmt *sql_stmt = NULL;
976         char *sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE thumbnail_path=%Q", storage_id, thumb_path);
977
978         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
979
980         if (ret != MS_MEDIA_ERR_NONE) {
981                 media_svc_error("error when _media_svc_get_thumbnail_count. err = [%d]", ret);
982                 return ret;
983         }
984
985         *count = sqlite3_column_int(sql_stmt, 0);
986
987         SQLITE3_FINALIZE(sql_stmt);
988
989         return MS_MEDIA_ERR_NONE;
990 }
991
992 int _media_svc_get_fileinfo_by_path(sqlite3 *handle, const char *storage_id, const char *path, time_t *modified_time, unsigned long long *size)
993 {
994         int ret = MS_MEDIA_ERR_NONE;
995         sqlite3_stmt *sql_stmt = NULL;
996         char *sql = sqlite3_mprintf("SELECT modified_time, size FROM '%s' WHERE path='%q'", storage_id, path);
997
998         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
999
1000         if (ret != MS_MEDIA_ERR_NONE) {
1001                 media_svc_error("error when _media_svc_get_fileinfo_by_path. err = [%d]", ret);
1002                 return ret;
1003         }
1004
1005         *modified_time = (int)sqlite3_column_int(sql_stmt, 0);
1006         *size = (unsigned long long)sqlite3_column_int64(sql_stmt, 1);
1007
1008         SQLITE3_FINALIZE(sql_stmt);
1009
1010         return MS_MEDIA_ERR_NONE;
1011 }
1012
1013 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)
1014 {
1015         int ret = MS_MEDIA_ERR_NONE;
1016         char *burst_id = NULL;
1017
1018         media_svc_debug("START pass1");
1019
1020         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";
1021 #if 0
1022         if (is_burst) {
1023                 int burst_id_int = 0;
1024                 ret = _media_svc_get_burst_id(handle, storage_id, &burst_id_int);
1025                 if (ret != MS_MEDIA_ERR_NONE) {
1026                         burst_id = NULL;
1027                 }
1028
1029                 if (burst_id_int > 0) {
1030                         media_svc_debug("Burst id : %d", burst_id_int);
1031                         burst_id = sqlite3_mprintf("%d", burst_id_int);
1032                 }
1033
1034                 /* Get thumbnail for burst shot */
1035                 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1036                 int width = 0;
1037                 int height = 0;
1038
1039                 ret = _media_svc_request_thumbnail_with_origin_size(content_info->path, thumb_path, sizeof(thumb_path), &width, &height);
1040                 if(ret == MS_MEDIA_ERR_NONE) {
1041                         ret = __media_svc_malloc_and_strncpy(&(content_info->thumbnail_path), thumb_path);
1042                         if (ret != MS_MEDIA_ERR_NONE) {
1043                                 content_info->thumbnail_path = NULL;
1044                         }
1045                 }
1046
1047                 if (content_info->media_meta.width <= 0)
1048                         content_info->media_meta.width = width;
1049
1050                 if (content_info->media_meta.height <= 0)
1051                         content_info->media_meta.height = height;
1052         }
1053
1054         /* Update Pinyin If Support Pinyin */
1055         if(_media_svc_check_pinyin_support())
1056         {
1057                 if(STRING_VALID(content_info->file_name))
1058                         _media_svc_get_pinyin_str(content_info->file_name, &content_info->file_name_pinyin);
1059         }
1060 #endif
1061
1062         char *sql = sqlite3_mprintf("INSERT INTO '%s' (%s) VALUES (%Q, %Q, %Q, %Q, %d, %Q, %lld, \
1063                                                                 %d, %d, %d, %d, %d, %Q, %Q);",
1064                 storage_id, db_fields,
1065                 content_info->media_uuid,
1066                 content_info->folder_uuid,
1067                 content_info->path,
1068                 content_info->file_name,
1069                 content_info->media_type,
1070                 content_info->mime_type,
1071                 content_info->size,
1072                 content_info->added_time,
1073                 content_info->modified_time,
1074                 content_info->is_drm,
1075                 content_info->storage_type,
1076                 content_info->timeline,
1077                 burst_id,
1078                 storage_id
1079                 );
1080 #if 0
1081         if (burst_id)
1082         {
1083                 sqlite3_free(burst_id);
1084                 burst_id = NULL;
1085         }
1086 #endif
1087         media_svc_debug("MAKE PASS 1 QUERY END");
1088
1089         if(!stack_query) {
1090                 ret = _media_svc_sql_query(handle, sql, uid);
1091                 sqlite3_free(sql);
1092                 if (ret != MS_MEDIA_ERR_NONE) {
1093                         media_svc_error("failed to insert item");
1094                         return ret;
1095                 }
1096         } else {
1097                 media_svc_debug("query : %s", sql);
1098                 _media_svc_sql_query_add(&g_media_svc_insert_item_query_list, &sql);
1099         }
1100         media_svc_debug("END");
1101         return MS_MEDIA_ERR_NONE;
1102 }
1103
1104 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)
1105 {
1106         int ret = MS_MEDIA_ERR_NONE;
1107         //char *burst_id = NULL;
1108
1109         media_svc_debug_fenter();
1110
1111         /*Update Pinyin If Support Pinyin*/
1112         if(_media_svc_check_pinyin_support())
1113         {
1114                 if(STRING_VALID(content_info->file_name))
1115                         _media_svc_get_pinyin_str(content_info->file_name, &content_info->file_name_pinyin);
1116                 if(STRING_VALID(content_info->media_meta.title))
1117                         _media_svc_get_pinyin_str(content_info->media_meta.title, &content_info->media_meta.title_pinyin);
1118                 if(STRING_VALID(content_info->media_meta.album))
1119                         _media_svc_get_pinyin_str(content_info->media_meta.album, &content_info->media_meta.album_pinyin);
1120                 if(STRING_VALID(content_info->media_meta.artist))
1121                         _media_svc_get_pinyin_str(content_info->media_meta.artist, &content_info->media_meta.artist_pinyin);
1122                 if(STRING_VALID(content_info->media_meta.album_artist))
1123                         _media_svc_get_pinyin_str(content_info->media_meta.album_artist, &content_info->media_meta.album_artist_pinyin);
1124                 if(STRING_VALID(content_info->media_meta.genre))
1125                         _media_svc_get_pinyin_str(content_info->media_meta.genre, &content_info->media_meta.genre_pinyin);
1126                 if(STRING_VALID(content_info->media_meta.composer))
1127                         _media_svc_get_pinyin_str(content_info->media_meta.composer, &content_info->media_meta.composer_pinyin);
1128                 if(STRING_VALID(content_info->media_meta.copyright))
1129                         _media_svc_get_pinyin_str(content_info->media_meta.copyright, &content_info->media_meta.copyright_pinyin);
1130                 if(STRING_VALID(content_info->media_meta.description))
1131                         _media_svc_get_pinyin_str(content_info->media_meta.description, &content_info->media_meta.description_pinyin);
1132         }
1133
1134         /*modified month does not exist in Tizen 2.4*/
1135         char *sql = sqlite3_mprintf("UPDATE '%s' SET \
1136                 thumbnail_path=%Q, title=%Q, album_id=%d, album=%Q, artist=%Q, album_artist=%Q, genre=%Q, composer=%Q, year=%Q, \
1137                 recorded_date=%Q, copyright=%Q, track_num=%Q, description=%Q, bitrate=%d, bitpersample=%d, samplerate=%d, channel=%d, \
1138                 duration=%d, longitude=%.6f, latitude=%.6f, altitude=%.6f, width=%d, height=%d, datetaken=%Q, orientation=%d, exposure_time=%Q,\
1139                 fnumber=%.6f, iso=%d, model=%Q, rating=%d, weather=%Q, file_name_pinyin=%Q, title_pinyin=%Q, album_pinyin=%Q, \
1140                 artist_pinyin=%Q, album_artist_pinyin=%Q, genre_pinyin=%Q, composer_pinyin=%Q, copyright_pinyin=%Q, description_pinyin=%Q WHERE path=%Q",
1141                 storage_id,
1142                 //content_info->folder_uuid,
1143                 content_info->thumbnail_path,           //
1144                 content_info->media_meta.title,
1145                 content_info->album_id,
1146                 content_info->media_meta.album,
1147                 content_info->media_meta.artist,
1148                 content_info->media_meta.album_artist,
1149                 content_info->media_meta.genre,
1150                 content_info->media_meta.composer,
1151                 content_info->media_meta.year,
1152                 content_info->media_meta.recorded_date,
1153                 content_info->media_meta.copyright,
1154                 content_info->media_meta.track_num,
1155                 content_info->media_meta.description,   //
1156                 content_info->media_meta.bitrate,
1157                 content_info->media_meta.bitpersample,
1158                 content_info->media_meta.samplerate,
1159                 content_info->media_meta.channel,
1160                 content_info->media_meta.duration,
1161                 content_info->media_meta.longitude,
1162                 content_info->media_meta.latitude,
1163                 content_info->media_meta.altitude,
1164                 content_info->media_meta.width,
1165                 content_info->media_meta.height,
1166                 content_info->media_meta.datetaken,
1167                 content_info->media_meta.orientation,
1168                 content_info->media_meta.exposure_time,
1169                 content_info->media_meta.fnumber,
1170                 content_info->media_meta.iso,
1171                 content_info->media_meta.model,
1172                 content_info->media_meta.rating,
1173                 content_info->media_meta.weather,
1174                 content_info->file_name_pinyin,
1175                 content_info->media_meta.title_pinyin,
1176                 content_info->media_meta.album_pinyin,
1177                 content_info->media_meta.artist_pinyin,
1178                 content_info->media_meta.album_artist_pinyin,
1179                 content_info->media_meta.genre_pinyin,
1180                 content_info->media_meta.composer_pinyin,
1181                 content_info->media_meta.copyright_pinyin,
1182                 content_info->media_meta.description_pinyin,
1183                 content_info->path
1184                 );
1185
1186         if(!stack_query) {
1187                 ret = _media_svc_sql_query(handle, sql, uid);
1188                 sqlite3_free(sql);
1189                 if (ret != MS_MEDIA_ERR_NONE) {
1190
1191                         media_svc_error("failed to insert item");
1192                         return ret;
1193                 }
1194         } else {
1195                 //media_svc_debug("query : %s", sql);
1196                 _media_svc_sql_query_add(&g_media_svc_update_item_query_list, &sql);
1197         }
1198
1199         media_svc_debug_fleave();
1200
1201         return MS_MEDIA_ERR_NONE;
1202 }