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