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