Modify scanner progress
[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 <media-util-err.h>
26 #include "media-svc-media.h"
27 #include "media-svc-media-folder.h"
28 #include "media-svc-debug.h"
29 #include "media-svc-util.h"
30 #include "media-svc-db-utils.h"
31 #include "media-svc-noti.h"
32
33 #define MEDIA_SVC_MAX_COMMIT_SIZE 200
34
35 static __thread GList *g_media_svc_item_validity_query_list = NULL;
36 static __thread GList *g_media_svc_insert_item_query_list = NULL;
37 static __thread GList *g_media_svc_update_item_query_list = NULL;
38 static __thread GList *g_media_svc_update_list = NULL;
39
40 int _media_svc_count_record_with_path(sqlite3 *handle, const char *storage_id, const char *path, int *count)
41 {
42         int ret = MS_MEDIA_ERR_NONE;
43         sqlite3_stmt *sql_stmt = NULL;
44         char *sql = NULL;
45
46         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
47         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
48
49         sql = sqlite3_mprintf("SELECT count(*) FROM '%q' WHERE MEDIA_path='%q'", storage_id, path);
50         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
51         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
52
53         *count = sqlite3_column_int(sql_stmt, 0);
54
55         SQLITE3_FINALIZE(sql_stmt);
56
57         return MS_MEDIA_ERR_NONE;
58 }
59
60 int _media_svc_get_modified_time(sqlite3 *handle, const char *storage_id, const char *path, int *modified_time)
61 {
62         int ret = MS_MEDIA_ERR_NONE;
63         sqlite3_stmt *sql_stmt = NULL;
64         char *sql = NULL;
65
66         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
67         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
68
69         sql = sqlite3_mprintf("SELECT media_modified_time FROM '%q' WHERE MEDIA_path='%q'", storage_id, path);
70         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
71         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
72
73         *modified_time = sqlite3_column_int(sql_stmt, 0);
74
75         SQLITE3_FINALIZE(sql_stmt);
76
77         return MS_MEDIA_ERR_NONE;
78 }
79
80 int _media_svc_insert_item_with_data(sqlite3 *handle, bool is_direct, const char *storage_id, media_svc_content_info_s *content_info, bool stack_query, uid_t uid)
81 {
82         int ret = MS_MEDIA_ERR_NONE;
83
84         const char *db_fields = "media_id, media_path, media_display_name, media_type, media_mime_type, media_size, media_added_time, media_modified_time, folder_id, media_thumbnail_path, \
85                 media_title, album_id, media_album, media_artist, media_album_artist, media_genre, media_composer, media_year, media_recorded_date, media_copyright, media_track_num, media_description, \
86                 media_favourite, media_bitrate, media_bitpersample, media_samplerate, media_channel, media_duration, \
87                 media_longitude, media_latitude, media_altitude, exposure_time, fnumber, iso, model, media_width, media_height, media_datetaken, media_orientation, media_rating, media_is_drm, media_storage_type, media_timeline, sync_status, \
88                 media_file_name_pinyin, media_title_pinyin, media_album_pinyin, media_artist_pinyin, media_album_artist_pinyin, media_genre_pinyin, media_composer_pinyin, media_copyright_pinyin, media_description_pinyin, storage_uuid, media_360";
89
90         const char *db_fields_basic = "media_id, media_path, media_display_name, media_type, media_mime_type, media_size, media_added_time, media_modified_time, folder_id, media_thumbnail_path, \
91                 album_id, media_favourite, media_is_drm, media_storage_type, media_timeline, sync_status, media_file_name_pinyin, storage_uuid, media_360";
92
93         /* This sql is due to sqlite3_mprintf's wrong operation when using floating point in the text format */
94         /* This code will be removed when sqlite3_mprintf works clearly */
95         char *test_sql = sqlite3_mprintf("%f, %f, %f", content_info->media_meta.longitude, content_info->media_meta.latitude, content_info->media_meta.altitude);
96         SQLITE3_SAFE_FREE(test_sql);
97
98         /*Update Pinyin If Support Pinyin*/
99         if (_media_svc_check_pinyin_support()) {
100                 if (STRING_VALID(content_info->file_name))
101                         _media_svc_get_pinyin_str(content_info->file_name, &content_info->file_name_pinyin);
102                 if (STRING_VALID(content_info->media_meta.title))
103                         _media_svc_get_pinyin_str(content_info->media_meta.title, &content_info->media_meta.title_pinyin);
104                 if (STRING_VALID(content_info->media_meta.album))
105                         _media_svc_get_pinyin_str(content_info->media_meta.album, &content_info->media_meta.album_pinyin);
106                 if (STRING_VALID(content_info->media_meta.artist))
107                         _media_svc_get_pinyin_str(content_info->media_meta.artist, &content_info->media_meta.artist_pinyin);
108                 if (STRING_VALID(content_info->media_meta.album_artist))
109                         _media_svc_get_pinyin_str(content_info->media_meta.album_artist, &content_info->media_meta.album_artist_pinyin);
110                 if (STRING_VALID(content_info->media_meta.genre))
111                         _media_svc_get_pinyin_str(content_info->media_meta.genre, &content_info->media_meta.genre_pinyin);
112                 if (STRING_VALID(content_info->media_meta.composer))
113                         _media_svc_get_pinyin_str(content_info->media_meta.composer, &content_info->media_meta.composer_pinyin);
114                 if (STRING_VALID(content_info->media_meta.copyright))
115                         _media_svc_get_pinyin_str(content_info->media_meta.copyright, &content_info->media_meta.copyright_pinyin);
116                 if (STRING_VALID(content_info->media_meta.description))
117                         _media_svc_get_pinyin_str(content_info->media_meta.description, &content_info->media_meta.description_pinyin);
118         }
119
120         char *sql = sqlite3_mprintf("INSERT INTO '%q' (%q) VALUES (%Q, %Q, %Q, %d, %Q, %lld, %d, %d, %Q, \
121                 %Q, %Q, %d, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %d, \
122                 %d, %d, %d, %d, %d, %.6f, %.6f, %.6f, %Q, %.6f, %d, %Q, %d, %d, %Q, %d, \
123                 %d, %d, %d, %d, %d, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %d);",
124                                 content_info->storage_uuid, db_fields,
125                                 content_info->media_uuid,
126                                 content_info->path,
127                                 content_info->file_name,
128                                 content_info->media_type,
129                                 content_info->mime_type,
130                                 content_info->size,
131                                 content_info->added_time,
132                                 content_info->modified_time,
133                                 content_info->folder_uuid,                      /* 1st line */
134                                 content_info->thumbnail_path,
135                                 content_info->media_meta.title,
136                                 content_info->album_id,
137                                 content_info->media_meta.album,
138                                 content_info->media_meta.artist,
139                                 content_info->media_meta.album_artist,
140                                 content_info->media_meta.genre,
141                                 content_info->media_meta.composer,
142                                 content_info->media_meta.year,
143                                 content_info->media_meta.recorded_date,
144                                 content_info->media_meta.copyright,
145                                 content_info->media_meta.track_num,
146                                 content_info->media_meta.description,
147                                 content_info->favourate,                /* 2nd line */
148                                 content_info->media_meta.bitrate,
149                                 content_info->media_meta.bitpersample,
150                                 content_info->media_meta.samplerate,
151                                 content_info->media_meta.channel,
152                                 content_info->media_meta.duration,
153                                 content_info->media_meta.longitude,
154                                 content_info->media_meta.latitude,
155                                 content_info->media_meta.altitude,
156                                 content_info->media_meta.exposure_time,
157                                 content_info->media_meta.fnumber,
158                                 content_info->media_meta.iso,
159                                 content_info->media_meta.model,
160                                 content_info->media_meta.width,
161                                 content_info->media_meta.height,
162                                 content_info->media_meta.datetaken,
163                                 content_info->media_meta.orientation,           /* 3rd line */
164                                 content_info->media_meta.rating,
165                                 content_info->is_drm,
166                                 content_info->storage_type,
167                                 content_info->timeline,
168                                 content_info->sync_status,
169                                 content_info->file_name_pinyin,
170                                 content_info->media_meta.title_pinyin,
171                                 content_info->media_meta.album_pinyin,
172                                 content_info->media_meta.artist_pinyin,
173                                 content_info->media_meta.album_artist_pinyin,
174                                 content_info->media_meta.genre_pinyin,
175                                 content_info->media_meta.composer_pinyin,
176                                 content_info->media_meta.copyright_pinyin,
177                                 content_info->media_meta.description_pinyin,
178                                 content_info->storage_uuid,
179                                 content_info->media_meta.is_360         /* 4th line */
180                         );
181
182         /* Check query length */
183         if (sql != NULL && strlen(sql) >= (MEDIA_SVC_QUERY_LEN_MAX - 1) && is_direct == false) {
184                 media_svc_debug("Query length is over %d byte.. So, remake query using only basic information..", MEDIA_SVC_QUERY_LEN_MAX - 1);
185                 SQLITE3_SAFE_FREE(sql);
186
187                 sql = sqlite3_mprintf("INSERT INTO '%q' (%q) VALUES (%Q, %Q, %Q, %d, %Q, %lld, %d, %d, %Q, %Q, %d, %d, %d, %d, %d, %d, %d, %d, %Q, %Q, %d);",
188                                                 content_info->storage_uuid, db_fields_basic,
189                                                 content_info->media_uuid,
190                                                 content_info->path,
191                                                 content_info->file_name,
192                                                 content_info->media_type,
193                                                 content_info->mime_type,
194                                                 content_info->size,
195                                                 content_info->added_time,
196                                                 content_info->modified_time,
197                                                 content_info->folder_uuid,
198                                                 content_info->thumbnail_path,
199                                                 content_info->album_id,
200                                                 content_info->favourate,
201                                                 content_info->is_drm,
202                                                 content_info->storage_type,
203                                                 content_info->timeline,
204                                                 content_info->sync_status,
205                                                 content_info->file_name_pinyin,
206                                                 content_info->storage_uuid,
207                                                 content_info->media_meta.is_360
208                                                 );
209         }
210
211         if (!stack_query) {
212                 if (is_direct)
213                         ret = _media_svc_sql_query_direct(sql, uid);
214                 else
215                         ret = _media_svc_sql_query(sql, uid);
216
217                 SQLITE3_SAFE_FREE(sql);
218                 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "failed to insert item");
219         } else {
220                 media_svc_sec_debug("Query[%s]", sql);
221                 _media_svc_sql_query_add(&g_media_svc_insert_item_query_list, &sql);
222         }
223
224         return MS_MEDIA_ERR_NONE;
225 }
226
227 int _media_svc_update_meta_with_data(media_svc_content_info_s *content_info)
228 {
229         int ret = MS_MEDIA_ERR_NONE;
230
231         /* This sql is due to sqlite3_mprintf's wrong operation when using floating point in the text format */
232         /* This code will be removed when sqlite3_mprintf works clearly */
233         char *test_sql = sqlite3_mprintf("%f, %f, %f", content_info->media_meta.longitude, content_info->media_meta.latitude, content_info->media_meta.altitude);
234         SQLITE3_SAFE_FREE(test_sql);
235
236         /*Update Pinyin If Support Pinyin*/
237         if (_media_svc_check_pinyin_support()) {
238                 if (STRING_VALID(content_info->file_name))
239                         _media_svc_get_pinyin_str(content_info->file_name, &content_info->file_name_pinyin);
240                 if (STRING_VALID(content_info->media_meta.title))
241                         _media_svc_get_pinyin_str(content_info->media_meta.title, &content_info->media_meta.title_pinyin);
242                 if (STRING_VALID(content_info->media_meta.album))
243                         _media_svc_get_pinyin_str(content_info->media_meta.album, &content_info->media_meta.album_pinyin);
244                 if (STRING_VALID(content_info->media_meta.artist))
245                         _media_svc_get_pinyin_str(content_info->media_meta.artist, &content_info->media_meta.artist_pinyin);
246                 if (STRING_VALID(content_info->media_meta.album_artist))
247                         _media_svc_get_pinyin_str(content_info->media_meta.album_artist, &content_info->media_meta.album_artist_pinyin);
248                 if (STRING_VALID(content_info->media_meta.genre))
249                         _media_svc_get_pinyin_str(content_info->media_meta.genre, &content_info->media_meta.genre_pinyin);
250                 if (STRING_VALID(content_info->media_meta.composer))
251                         _media_svc_get_pinyin_str(content_info->media_meta.composer, &content_info->media_meta.composer_pinyin);
252                 if (STRING_VALID(content_info->media_meta.copyright))
253                         _media_svc_get_pinyin_str(content_info->media_meta.copyright, &content_info->media_meta.copyright_pinyin);
254                 if (STRING_VALID(content_info->media_meta.description))
255                         _media_svc_get_pinyin_str(content_info->media_meta.description, &content_info->media_meta.description_pinyin);
256         }
257
258         char *sql = sqlite3_mprintf("UPDATE '%q' SET media_title=%Q, media_album=%Q, media_artist=%Q, media_album_artist=%Q, media_genre=%Q, media_composer=%Q, media_copyright=%Q, media_description=%Q, \
259                 media_file_name_pinyin=%Q, media_title_pinyin=%Q, media_album_pinyin=%Q, media_artist_pinyin=%Q, media_album_artist_pinyin=%Q, media_genre_pinyin=%Q, media_composer_pinyin=%Q, media_copyright_pinyin=%Q, media_description_pinyin=%Q \
260                 WHERE path=%Q;",
261                                 content_info->storage_uuid,
262                                 content_info->media_meta.title,
263                                 content_info->media_meta.album,
264                                 content_info->media_meta.artist,
265                                 content_info->media_meta.album_artist,
266                                 content_info->media_meta.genre,
267                                 content_info->media_meta.composer,
268                                 content_info->media_meta.copyright,
269                                 content_info->media_meta.description,
270                                 content_info->file_name_pinyin,
271                                 content_info->media_meta.title_pinyin,
272                                 content_info->media_meta.album_pinyin,
273                                 content_info->media_meta.artist_pinyin,
274                                 content_info->media_meta.album_artist_pinyin,
275                                 content_info->media_meta.genre_pinyin,
276                                 content_info->media_meta.composer_pinyin,
277                                 content_info->media_meta.copyright_pinyin,
278                                 content_info->media_meta.description_pinyin,
279                                 content_info->path
280                                 );
281
282         if (sql != NULL) {
283                 media_svc_debug("query[%s]", sql);
284                 _media_svc_sql_query_add(&g_media_svc_update_item_query_list, &sql);
285         } else {
286                 media_svc_error("sqlite3_mprintf failed");
287                 ret = MS_MEDIA_ERR_OUT_OF_MEMORY;
288         }
289
290         return ret;
291 }
292
293 int _media_svc_update_item_with_data(bool is_direct, const char *storage_id, media_svc_content_info_s *content_info, uid_t uid)
294 {
295         int ret = MS_MEDIA_ERR_NONE;
296
297         /* This sql is due to sqlite3_mprintf's wrong operation when using floating point in the text format */
298         /* This code will be removed when sqlite3_mprintf works clearly */
299         char *test_sql = sqlite3_mprintf("%f, %f, %f", content_info->media_meta.longitude, content_info->media_meta.latitude, content_info->media_meta.altitude);
300         SQLITE3_SAFE_FREE(test_sql);
301
302         /*Update Pinyin If Support Pinyin*/
303         if (_media_svc_check_pinyin_support()) {
304                 if (STRING_VALID(content_info->file_name))
305                         _media_svc_get_pinyin_str(content_info->file_name, &content_info->file_name_pinyin);
306                 if (STRING_VALID(content_info->media_meta.title))
307                         _media_svc_get_pinyin_str(content_info->media_meta.title, &content_info->media_meta.title_pinyin);
308                 if (STRING_VALID(content_info->media_meta.album))
309                         _media_svc_get_pinyin_str(content_info->media_meta.album, &content_info->media_meta.album_pinyin);
310                 if (STRING_VALID(content_info->media_meta.artist))
311                         _media_svc_get_pinyin_str(content_info->media_meta.artist, &content_info->media_meta.artist_pinyin);
312                 if (STRING_VALID(content_info->media_meta.album_artist))
313                         _media_svc_get_pinyin_str(content_info->media_meta.album_artist, &content_info->media_meta.album_artist_pinyin);
314                 if (STRING_VALID(content_info->media_meta.genre))
315                         _media_svc_get_pinyin_str(content_info->media_meta.genre, &content_info->media_meta.genre_pinyin);
316                 if (STRING_VALID(content_info->media_meta.composer))
317                         _media_svc_get_pinyin_str(content_info->media_meta.composer, &content_info->media_meta.composer_pinyin);
318                 if (STRING_VALID(content_info->media_meta.copyright))
319                         _media_svc_get_pinyin_str(content_info->media_meta.copyright, &content_info->media_meta.copyright_pinyin);
320                 if (STRING_VALID(content_info->media_meta.description))
321                         _media_svc_get_pinyin_str(content_info->media_meta.description, &content_info->media_meta.description_pinyin);
322         }
323
324         char *sql = sqlite3_mprintf("UPDATE '%q' SET media_size=%lld, media_modified_time=%d, media_thumbnail_path=%Q, media_title=%Q, album_id=%d, media_album=%Q, media_artist=%Q, media_album_artist=%Q, media_genre=%Q, \
325                 media_composer=%Q, media_year=%Q, media_recorded_date=%Q, media_copyright=%Q, media_track_num=%Q, media_description=%Q, media_bitrate=%d, media_bitpersample=%d, media_samplerate=%d, media_channel=%d, media_duration=%d, \
326                 media_longitude=%f, media_latitude=%f, media_altitude=%f, exposure_time=%Q, fnumber=%f, iso=%d, model=%Q, media_width=%d, media_height=%d, media_datetaken=%Q, media_orientation=%d WHERE media_path=%Q;",
327                                 storage_id,
328                                 content_info->size,
329                                 content_info->modified_time,
330                                 content_info->thumbnail_path,
331                                 content_info->media_meta.title,
332                                 content_info->album_id,
333                                 content_info->media_meta.album,
334                                 content_info->media_meta.artist,
335                                 content_info->media_meta.album_artist,
336                                 content_info->media_meta.genre,
337                                 content_info->media_meta.composer,
338                                 content_info->media_meta.year,
339                                 content_info->media_meta.recorded_date,
340                                 content_info->media_meta.copyright,
341                                 content_info->media_meta.track_num,
342                                 content_info->media_meta.description,
343                                 content_info->media_meta.bitrate,
344                                 content_info->media_meta.bitpersample,
345                                 content_info->media_meta.samplerate,
346                                 content_info->media_meta.channel,
347                                 content_info->media_meta.duration,
348                                 content_info->media_meta.longitude,
349                                 content_info->media_meta.latitude,
350                                 content_info->media_meta.altitude,
351                                 content_info->media_meta.exposure_time,
352                                 content_info->media_meta.fnumber,
353                                 content_info->media_meta.iso,
354                                 content_info->media_meta.model,
355                                 content_info->media_meta.width,
356                                 content_info->media_meta.height,
357                                 content_info->media_meta.datetaken,
358                                 content_info->media_meta.orientation,
359                                 content_info->path
360                                 );
361
362         /* Scanner use only batch insert */
363         if (is_direct) {
364                 media_svc_sec_debug("Query [%s]", sql);
365                 _media_svc_sql_query_add(&g_media_svc_insert_item_query_list, &sql);
366         } else {
367                 ret = _media_svc_sql_query(sql, uid);
368         }
369         SQLITE3_SAFE_FREE(sql);
370
371         return ret;
372 }
373 int _media_svc_get_thumbnail_path_by_path(sqlite3 *handle, const char *path, char *thumbnail_path)
374 {
375         int ret = MS_MEDIA_ERR_NONE;
376         sqlite3_stmt *sql_stmt = NULL;
377         char *sql = NULL;
378
379         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
380
381         sql = sqlite3_mprintf("SELECT media_thumbnail_path FROM '%q' WHERE media_path='%q'", MEDIA_SVC_DB_VIEW_MEDIA, path);
382
383         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
384
385         if (ret != MS_MEDIA_ERR_NONE) {
386                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
387                         media_svc_debug("there is no thumbnail.");
388                 else
389                         media_svc_error("error when _media_svc_get_thumbnail_path_by_path. err = [%d]", ret);
390
391                 return ret;
392         }
393
394         SAFE_STRLCPY(thumbnail_path, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_PATHNAME_SIZE);
395
396         SQLITE3_FINALIZE(sql_stmt);
397
398         return MS_MEDIA_ERR_NONE;
399 }
400
401 int _media_svc_get_media_type_by_path(sqlite3 *handle, const char *storage_id, const char *path, int *media_type)
402 {
403         int ret = MS_MEDIA_ERR_NONE;
404         sqlite3_stmt *sql_stmt = NULL;
405         char *sql = NULL;
406
407         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
408         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
409
410         sql = sqlite3_mprintf("SELECT media_type FROM '%q' WHERE media_path='%q'", storage_id, path);
411
412         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
413
414         if (ret != MS_MEDIA_ERR_NONE) {
415                 media_svc_error("error when _media_svc_get_media_type_by_path. err = [%d]", ret);
416                 return ret;
417         }
418
419         *media_type = sqlite3_column_int(sql_stmt, 0);
420
421         SQLITE3_FINALIZE(sql_stmt);
422
423         return MS_MEDIA_ERR_NONE;
424 }
425
426 int _media_svc_delete_item_by_path(bool is_direct, const char *storage_id, const char *path, bool stack_query, uid_t uid)
427 {
428         int ret = MS_MEDIA_ERR_NONE;
429         char *sql = NULL;
430
431         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
432         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
433
434         sql = sqlite3_mprintf("DELETE FROM '%q' WHERE media_path='%q';", storage_id, path);
435
436         if (!stack_query) {
437                 if (is_direct)
438                         ret = _media_svc_sql_query_direct(sql, uid);
439                 else
440                         ret = _media_svc_sql_query(sql, uid);
441                 SQLITE3_SAFE_FREE(sql);
442                 if (ret != MS_MEDIA_ERR_NONE) {
443                         media_svc_error("failed to delete item");
444                         return ret;
445                 }
446         } else {
447                 media_svc_debug("query : %s", sql);
448                 _media_svc_sql_query_add(&g_media_svc_insert_item_query_list, &sql);
449         }
450
451         return ret;
452 }
453
454 int _media_svc_update_item_validity(const char *storage_id, const char *path, int validity, bool stack_query, uid_t uid)
455 {
456         int ret = MS_MEDIA_ERR_NONE;
457         char *sql = NULL;
458
459         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
460         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
461
462         media_svc_debug("path=[%s], validity=[%d]", path, validity);
463
464         sql = sqlite3_mprintf("UPDATE '%q' SET validity=%d WHERE media_path='%q';", storage_id, validity, path);
465
466         if (!stack_query) {
467                 ret = _media_svc_sql_query_direct(sql, uid);
468                 SQLITE3_SAFE_FREE(sql);
469         } else {
470                 _media_svc_sql_query_add(&g_media_svc_item_validity_query_list, &sql);
471         }
472
473         return ret;
474 }
475
476 int _media_svc_update_thumbnail_path(const char *storage_id, const char *path, const char *thumb_path, uid_t uid)
477 {
478         int ret = MS_MEDIA_ERR_NONE;
479
480         char *sql = sqlite3_mprintf("UPDATE '%q' SET media_thumbnail_path=%Q WHERE media_path=%Q;", storage_id, thumb_path, path);
481
482         ret = _media_svc_sql_query(sql, uid);
483         SQLITE3_SAFE_FREE(sql);
484
485         return ret;
486 }
487
488 int _media_svc_update_item_by_path(const char *org_storage_id, const char *src_path, const char *dst_storage_id, ms_user_storage_type_e dest_storage, const char *dest_path, const char *file_name, int modified_time, const char *folder_uuid, uid_t uid)
489 {
490         /* update path, filename, modified_time, folder_uuid, thumbnail_path, */
491
492         /* NOTICE (This operation is abnormal. Modifications should be cautious.)
493         1. Update source to destination info
494         2. Insert destination table
495         3. Remove source info
496         */
497         int ret = MS_MEDIA_ERR_NONE;
498         char *update = sqlite3_mprintf("UPDATE '%q' SET media_path=%Q, media_display_name=%Q, media_modified_time=%d, folder_id=%Q, media_storage_type=%d, storage_uuid='%q' WHERE media_path=%Q;",
499                                 org_storage_id, dest_path, file_name, modified_time, folder_uuid, dest_storage, dst_storage_id, src_path);
500         char *insert = sqlite3_mprintf("INSERT OR REPLACE INTO '%q' SELECT * FROM '%q' WHERE media_path = '%q';", dst_storage_id, org_storage_id, dest_path);
501         char *remove = sqlite3_mprintf("DELETE FROM '%q' WHERE media_path = '%q';", org_storage_id, dest_path);
502         char *query = NULL;
503
504         if (strcmp(org_storage_id, dst_storage_id) != 0)
505                 query = sqlite3_mprintf("%s %s %s", update, insert, remove);
506         else
507                 query = sqlite3_mprintf("%s %s", update, insert);
508
509         SQLITE3_SAFE_FREE(update);
510         SQLITE3_SAFE_FREE(insert);
511         SQLITE3_SAFE_FREE(remove);
512
513         ret = _media_svc_sql_query(query, uid);
514         SQLITE3_SAFE_FREE(query);
515
516         return ret;
517 }
518
519 int _media_svc_list_query_do(media_svc_query_type_e query_type, uid_t uid)
520 {
521         int ret = MS_MEDIA_ERR_NONE;
522         char *sql = NULL;
523
524         /* For multiples of 200, empty requests are possible */
525         switch (query_type) {
526         case MEDIA_SVC_QUERY_UPDATE_COMMON:
527                 if (g_media_svc_update_list == NULL || g_list_length(g_media_svc_update_list) == 0)
528                         return MS_MEDIA_ERR_NONE;
529
530                 if (g_list_length(g_media_svc_update_list) == 1) {
531                         sql = (char *)g_list_nth_data(g_media_svc_update_list, 0);
532                         if (STRING_VALID(sql)) {
533                                 ret = _media_svc_sql_query(sql, uid);
534                                 SQLITE3_SAFE_FREE(sql);
535                         }
536                         _media_svc_sql_query_release(&g_media_svc_update_list);
537
538                         return ret;
539                 }
540
541                 ret = _media_svc_sql_begin_trans(uid);
542                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
543
544                 ret = _media_svc_sql_query_list(&g_media_svc_update_list, uid);
545                 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Failed to request queries");
546
547                 ret = _media_svc_sql_end_trans(uid);
548                 break;
549         case MEDIA_SVC_QUERY_SET_ITEM_VALIDITY:
550                 ret = _media_svc_sql_query_list_direct(&g_media_svc_item_validity_query_list, uid);
551                 break;
552         case MEDIA_SVC_QUERY_INSERT_ITEM:
553                 ret = _media_svc_sql_query_list_direct(&g_media_svc_insert_item_query_list, uid);
554                 break;
555         case MEDIA_SVC_QUERY_UPDATE_ITEM:
556                 ret = _media_svc_sql_query_list_direct(&g_media_svc_update_item_query_list, uid);
557                 break;
558         case MEDIA_SVC_QUERY_INSERT_FOLDER:
559                 ret = _media_svc_sql_query_list_direct(_media_svc_get_folder_list_ptr(), uid);
560                 break;
561         default:
562                 media_svc_error("Wrong type[%d]", query_type);
563                 return MS_MEDIA_ERR_INVALID_PARAMETER;
564         }
565
566         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Failed to request queries");
567
568         return MS_MEDIA_ERR_NONE;
569 }
570
571 int _media_svc_append_query_list(const char *query, uid_t uid)
572 {
573         int ret = MS_MEDIA_ERR_NONE;
574
575         media_svc_retvm_if(query == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "query is NULL");
576
577         g_media_svc_update_list = g_list_append(g_media_svc_update_list, (gpointer)query);
578
579         if (g_list_length(g_media_svc_update_list) >= MEDIA_SVC_MAX_COMMIT_SIZE)
580                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_COMMON, uid);
581
582         return ret;
583 }
584
585 int _media_svc_get_noti_info(sqlite3 *handle, const char *storage_id, const char *path, media_svc_noti_item **item)
586 {
587         int ret = MS_MEDIA_ERR_NONE;
588         sqlite3_stmt *sql_stmt = NULL;
589         char *sql = NULL;
590
591         media_svc_retvm_if(item == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "item is NULL");
592         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
593         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
594
595         sql = sqlite3_mprintf("SELECT media_id, media_type, media_mime_type FROM '%q' WHERE media_path=%Q", storage_id, path);
596         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
597         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_sql_prepare_to_step() failed [%d]", ret);
598
599         *item = calloc(1, sizeof(media_svc_noti_item));
600         if (*item == NULL) {
601                 media_svc_error("Allocation failed");
602                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
603         }
604
605         (*item)->media_uuid = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
606         (*item)->media_type = sqlite3_column_int(sql_stmt, 1);
607         (*item)->mime_type = g_strdup((const char *)sqlite3_column_text(sql_stmt, 2));
608
609         SQLITE3_FINALIZE(sql_stmt);
610
611         return MS_MEDIA_ERR_NONE;
612 }