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