Match columns of folder with filter keyword
[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 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 modified_time FROM '%q' WHERE 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, 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_uuid, path, file_name, media_type, mime_type, size, added_time, modified_time, folder_id, thumbnail_path, \
85                 title, album_id, album, artist, album_artist, genre, composer, year, recorded_date, copyright, track_num, description, \
86                 favourite, bitrate, bitpersample, samplerate, channel, duration, \
87                 longitude, latitude, altitude, exposure_time, fnumber, iso, model, width, height, datetaken, orientation, rating, is_drm, storage_type, timeline, sync_status, \
88                 file_name_pinyin, title_pinyin, album_pinyin, artist_pinyin, album_artist_pinyin, genre_pinyin, composer_pinyin, copyright_pinyin, description_pinyin, storage_uuid, is_360";
89
90         const char *db_fields_basic = "media_uuid, path, file_name, media_type, mime_type, size, added_time, modified_time, folder_id, thumbnail_path, \
91                 album_id, favourite, is_drm, storage_type, timeline, sync_status, file_name_pinyin, storage_uuid, is_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)) {
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         /* 1st line */
208                                                 );
209         }
210
211         if (!stack_query) {
212                 ret = _media_svc_sql_query(sql, uid);
213                 SQLITE3_SAFE_FREE(sql);
214                 if (ret != MS_MEDIA_ERR_NONE) {
215                         media_svc_error("failed to insert item");
216                         return ret;
217                 }
218         } else {
219                 media_svc_debug("query : %s", sql);
220                 _media_svc_sql_query_add(&g_media_svc_insert_item_query_list, &sql);
221         }
222
223         return MS_MEDIA_ERR_NONE;
224 }
225
226 int _media_svc_update_meta_with_data(media_svc_content_info_s *content_info)
227 {
228         int ret = MS_MEDIA_ERR_NONE;
229
230         /* This sql is due to sqlite3_mprintf's wrong operation when using floating point in the text format */
231         /* This code will be removed when sqlite3_mprintf works clearly */
232         char *test_sql = sqlite3_mprintf("%f, %f, %f", content_info->media_meta.longitude, content_info->media_meta.latitude, content_info->media_meta.altitude);
233         SQLITE3_SAFE_FREE(test_sql);
234
235         /*Update Pinyin If Support Pinyin*/
236         if (_media_svc_check_pinyin_support()) {
237                 if (STRING_VALID(content_info->file_name))
238                         _media_svc_get_pinyin_str(content_info->file_name, &content_info->file_name_pinyin);
239                 if (STRING_VALID(content_info->media_meta.title))
240                         _media_svc_get_pinyin_str(content_info->media_meta.title, &content_info->media_meta.title_pinyin);
241                 if (STRING_VALID(content_info->media_meta.album))
242                         _media_svc_get_pinyin_str(content_info->media_meta.album, &content_info->media_meta.album_pinyin);
243                 if (STRING_VALID(content_info->media_meta.artist))
244                         _media_svc_get_pinyin_str(content_info->media_meta.artist, &content_info->media_meta.artist_pinyin);
245                 if (STRING_VALID(content_info->media_meta.album_artist))
246                         _media_svc_get_pinyin_str(content_info->media_meta.album_artist, &content_info->media_meta.album_artist_pinyin);
247                 if (STRING_VALID(content_info->media_meta.genre))
248                         _media_svc_get_pinyin_str(content_info->media_meta.genre, &content_info->media_meta.genre_pinyin);
249                 if (STRING_VALID(content_info->media_meta.composer))
250                         _media_svc_get_pinyin_str(content_info->media_meta.composer, &content_info->media_meta.composer_pinyin);
251                 if (STRING_VALID(content_info->media_meta.copyright))
252                         _media_svc_get_pinyin_str(content_info->media_meta.copyright, &content_info->media_meta.copyright_pinyin);
253                 if (STRING_VALID(content_info->media_meta.description))
254                         _media_svc_get_pinyin_str(content_info->media_meta.description, &content_info->media_meta.description_pinyin);
255         }
256
257         char *sql = sqlite3_mprintf("UPDATE '%q' SET title=%Q, album=%Q, artist=%Q, album_artist=%Q, genre=%Q, composer=%Q, copyright=%Q, description=%Q, \
258                 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 \
259                 WHERE path=%Q;",
260                                 content_info->storage_uuid,
261                                 content_info->media_meta.title,
262                                 content_info->media_meta.album,
263                                 content_info->media_meta.artist,
264                                 content_info->media_meta.album_artist,
265                                 content_info->media_meta.genre,
266                                 content_info->media_meta.composer,
267                                 content_info->media_meta.copyright,
268                                 content_info->media_meta.description,
269                                 content_info->file_name_pinyin,
270                                 content_info->media_meta.title_pinyin,
271                                 content_info->media_meta.album_pinyin,
272                                 content_info->media_meta.artist_pinyin,
273                                 content_info->media_meta.album_artist_pinyin,
274                                 content_info->media_meta.genre_pinyin,
275                                 content_info->media_meta.composer_pinyin,
276                                 content_info->media_meta.copyright_pinyin,
277                                 content_info->media_meta.description_pinyin,
278                                 content_info->path
279                                 );
280
281         if (sql != NULL) {
282                 media_svc_debug("query : %s", sql);
283                 _media_svc_sql_query_add(&g_media_svc_update_item_query_list, &sql);
284         } else {
285                 media_svc_error("sqlite3_mprintf failed");
286                 ret = MS_MEDIA_ERR_OUT_OF_MEMORY;
287         }
288
289         return ret;
290 }
291
292 int _media_svc_update_item_with_data(const char *storage_id, media_svc_content_info_s *content_info, uid_t uid)
293 {
294         int ret = MS_MEDIA_ERR_NONE;
295
296         /* This sql is due to sqlite3_mprintf's wrong operation when using floating point in the text format */
297         /* This code will be removed when sqlite3_mprintf works clearly */
298         char *test_sql = sqlite3_mprintf("%f, %f, %f", content_info->media_meta.longitude, content_info->media_meta.latitude, content_info->media_meta.altitude);
299         SQLITE3_SAFE_FREE(test_sql);
300
301         /*Update Pinyin If Support Pinyin*/
302         if (_media_svc_check_pinyin_support()) {
303                 if (STRING_VALID(content_info->file_name))
304                         _media_svc_get_pinyin_str(content_info->file_name, &content_info->file_name_pinyin);
305                 if (STRING_VALID(content_info->media_meta.title))
306                         _media_svc_get_pinyin_str(content_info->media_meta.title, &content_info->media_meta.title_pinyin);
307                 if (STRING_VALID(content_info->media_meta.album))
308                         _media_svc_get_pinyin_str(content_info->media_meta.album, &content_info->media_meta.album_pinyin);
309                 if (STRING_VALID(content_info->media_meta.artist))
310                         _media_svc_get_pinyin_str(content_info->media_meta.artist, &content_info->media_meta.artist_pinyin);
311                 if (STRING_VALID(content_info->media_meta.album_artist))
312                         _media_svc_get_pinyin_str(content_info->media_meta.album_artist, &content_info->media_meta.album_artist_pinyin);
313                 if (STRING_VALID(content_info->media_meta.genre))
314                         _media_svc_get_pinyin_str(content_info->media_meta.genre, &content_info->media_meta.genre_pinyin);
315                 if (STRING_VALID(content_info->media_meta.composer))
316                         _media_svc_get_pinyin_str(content_info->media_meta.composer, &content_info->media_meta.composer_pinyin);
317                 if (STRING_VALID(content_info->media_meta.copyright))
318                         _media_svc_get_pinyin_str(content_info->media_meta.copyright, &content_info->media_meta.copyright_pinyin);
319                 if (STRING_VALID(content_info->media_meta.description))
320                         _media_svc_get_pinyin_str(content_info->media_meta.description, &content_info->media_meta.description_pinyin);
321         }
322
323         char *sql = sqlite3_mprintf("UPDATE '%q' SET size=%lld, modified_time=%d, thumbnail_path=%Q, title=%Q, album_id=%d, album=%Q, artist=%Q, album_artist=%Q, genre=%Q, \
324                 composer=%Q, year=%Q, recorded_date=%Q, copyright=%Q, track_num=%Q, description=%Q, bitrate=%d, bitpersample=%d, samplerate=%d, channel=%d, duration=%d, \
325                 longitude=%f, latitude=%f, altitude=%f, exposure_time=%Q, fnumber=%f, iso=%d, model=%Q, width=%d, height=%d, datetaken=%Q, orientation=%d WHERE path=%Q;",
326                                 storage_id,
327                                 content_info->size,
328                                 content_info->modified_time,
329                                 content_info->thumbnail_path,
330                                 content_info->media_meta.title,
331                                 content_info->album_id,
332                                 content_info->media_meta.album,
333                                 content_info->media_meta.artist,
334                                 content_info->media_meta.album_artist,
335                                 content_info->media_meta.genre,
336                                 content_info->media_meta.composer,
337                                 content_info->media_meta.year,
338                                 content_info->media_meta.recorded_date,
339                                 content_info->media_meta.copyright,
340                                 content_info->media_meta.track_num,
341                                 content_info->media_meta.description,
342                                 content_info->media_meta.bitrate,
343                                 content_info->media_meta.bitpersample,
344                                 content_info->media_meta.samplerate,
345                                 content_info->media_meta.channel,
346                                 content_info->media_meta.duration,
347                                 content_info->media_meta.longitude,
348                                 content_info->media_meta.latitude,
349                                 content_info->media_meta.altitude,
350                                 content_info->media_meta.exposure_time,
351                                 content_info->media_meta.fnumber,
352                                 content_info->media_meta.iso,
353                                 content_info->media_meta.model,
354                                 content_info->media_meta.width,
355                                 content_info->media_meta.height,
356                                 content_info->media_meta.datetaken,
357                                 content_info->media_meta.orientation,
358                                 content_info->path
359                                 );
360
361         ret = _media_svc_sql_query(sql, uid);
362         SQLITE3_SAFE_FREE(sql);
363
364         return ret;
365 }
366 int _media_svc_get_thumbnail_path_by_path(sqlite3 *handle, const char *path, char *thumbnail_path)
367 {
368         int ret = MS_MEDIA_ERR_NONE;
369         sqlite3_stmt *sql_stmt = NULL;
370         char *sql = NULL;
371
372         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
373
374         sql = sqlite3_mprintf("SELECT thumbnail_path FROM '%q' WHERE path='%q'", MEDIA_SVC_DB_VIEW_MEDIA, path);
375
376         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
377
378         if (ret != MS_MEDIA_ERR_NONE) {
379                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
380                         media_svc_debug("there is no thumbnail.");
381                 else
382                         media_svc_error("error when _media_svc_get_thumbnail_path_by_path. err = [%d]", ret);
383
384                 return ret;
385         }
386
387         SAFE_STRLCPY(thumbnail_path, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_PATHNAME_SIZE);
388
389         SQLITE3_FINALIZE(sql_stmt);
390
391         return MS_MEDIA_ERR_NONE;
392 }
393
394 int _media_svc_get_media_type_by_path(sqlite3 *handle, const char *storage_id, const char *path, int *media_type)
395 {
396         int ret = MS_MEDIA_ERR_NONE;
397         sqlite3_stmt *sql_stmt = NULL;
398         char *sql = NULL;
399
400         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
401         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
402
403         sql = sqlite3_mprintf("SELECT media_type FROM '%q' WHERE path='%q'", storage_id, path);
404
405         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
406
407         if (ret != MS_MEDIA_ERR_NONE) {
408                 media_svc_error("error when _media_svc_get_media_type_by_path. err = [%d]", ret);
409                 return ret;
410         }
411
412         *media_type = sqlite3_column_int(sql_stmt, 0);
413
414         SQLITE3_FINALIZE(sql_stmt);
415
416         return MS_MEDIA_ERR_NONE;
417 }
418
419 int _media_svc_delete_item_by_path(const char *storage_id, const char *path, bool stack_query, uid_t uid)
420 {
421         int ret = MS_MEDIA_ERR_NONE;
422         char *sql = NULL;
423
424         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
425         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
426
427         sql = sqlite3_mprintf("DELETE FROM '%q' WHERE path='%q';", storage_id, path);
428
429         if (!stack_query) {
430                 ret = _media_svc_sql_query(sql, uid);
431                 SQLITE3_SAFE_FREE(sql);
432                 if (ret != MS_MEDIA_ERR_NONE) {
433                         media_svc_error("failed to delete item");
434                         return ret;
435                 }
436         } else {
437                 media_svc_debug("query : %s", sql);
438                 _media_svc_sql_query_add(&g_media_svc_insert_item_query_list, &sql);
439         }
440
441         return ret;
442 }
443
444 int _media_svc_update_item_validity(const char *storage_id, const char *path, int validity, bool stack_query, uid_t uid)
445 {
446         int ret = MS_MEDIA_ERR_NONE;
447         char *sql = NULL;
448
449         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
450         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
451
452         media_svc_debug("path=[%s], validity=[%d]", path, validity);
453
454         sql = sqlite3_mprintf("UPDATE '%q' SET validity=%d WHERE path= '%q';", storage_id, validity, path);
455
456         if (!stack_query) {
457                 ret = _media_svc_sql_query(sql, uid);
458                 SQLITE3_SAFE_FREE(sql);
459         } else {
460                 _media_svc_sql_query_add(&g_media_svc_item_validity_query_list, &sql);
461         }
462
463         return ret;
464 }
465
466 int _media_svc_update_thumbnail_path(const char *storage_id, const char *path, const char *thumb_path, uid_t uid)
467 {
468         int ret = MS_MEDIA_ERR_NONE;
469
470         char *sql = sqlite3_mprintf("UPDATE '%q' SET thumbnail_path=%Q WHERE path=%Q;", storage_id, thumb_path, path);
471
472         ret = _media_svc_sql_query(sql, uid);
473         SQLITE3_SAFE_FREE(sql);
474
475         return ret;
476 }
477
478 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)
479 {
480         /* update path, filename, modified_time, folder_uuid, thumbnail_path, */
481
482         /* NOTICE (This operation is abnormal. Modifications should be cautious.)
483         1. Update source to destination info
484         2. Insert destination table
485         3. Remove source info
486         */
487         int ret = MS_MEDIA_ERR_NONE;
488         char *update = sqlite3_mprintf("UPDATE '%q' SET path=%Q, file_name=%Q, modified_time=%d, folder_id=%Q, storage_type=%d, storage_uuid='%q' WHERE path=%Q;",
489                                 org_storage_id, dest_path, file_name, modified_time, folder_uuid, dest_storage, dst_storage_id, src_path);
490         char *insert = sqlite3_mprintf("INSERT OR REPLACE INTO '%q' SELECT * FROM '%q' WHERE path = '%q';", dst_storage_id, org_storage_id, dest_path);
491         char *remove = sqlite3_mprintf("DELETE FROM '%q' WHERE path = '%q';", org_storage_id, dest_path);
492         char *query = NULL;
493
494         if (strcmp(org_storage_id, dst_storage_id) != 0)
495                 query = sqlite3_mprintf("%s %s %s", update, insert, remove);
496         else
497                 query = sqlite3_mprintf("%s %s", update, insert);
498
499         SQLITE3_SAFE_FREE(update);
500         SQLITE3_SAFE_FREE(insert);
501         SQLITE3_SAFE_FREE(remove);
502
503         ret = _media_svc_sql_query(query, uid);
504         SQLITE3_SAFE_FREE(query);
505
506         return ret;
507 }
508
509 int _media_svc_list_query_do(media_svc_query_type_e query_type, uid_t uid)
510 {
511         int ret = MS_MEDIA_ERR_NONE;
512
513         /* For multiples of 200, empty requests are possible */
514         if (query_type == MEDIA_SVC_QUERY_UPDATE_COMMON) {
515                 if (g_media_svc_update_list == NULL || g_list_length(g_media_svc_update_list) == 0)
516                         return MS_MEDIA_ERR_NONE;
517         }
518
519         ret = _media_svc_sql_begin_trans(uid);
520         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
521
522         if (query_type == MEDIA_SVC_QUERY_SET_ITEM_VALIDITY)
523                 ret = _media_svc_sql_query_list(&g_media_svc_item_validity_query_list, uid);
524         else if (query_type == MEDIA_SVC_QUERY_INSERT_ITEM)
525                 ret = _media_svc_sql_query_list(&g_media_svc_insert_item_query_list, uid);
526         else if (query_type == MEDIA_SVC_QUERY_UPDATE_ITEM)
527                 ret = _media_svc_sql_query_list(&g_media_svc_update_item_query_list, uid);
528         else if (query_type == MEDIA_SVC_QUERY_INSERT_FOLDER)
529                 ret = _media_svc_sql_query_list(_media_svc_get_folder_list_ptr(), uid);
530         else if (query_type == MEDIA_SVC_QUERY_UPDATE_COMMON)
531                 ret = _media_svc_sql_query_list(&g_media_svc_update_list, uid);
532         else
533                 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
534
535         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Failed to request queries");
536
537         ret = _media_svc_sql_end_trans(uid);
538         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Failed to batch insert");
539
540         return MS_MEDIA_ERR_NONE;
541 }
542
543 int _media_svc_append_query_list(const char *query, uid_t uid)
544 {
545         int ret = MS_MEDIA_ERR_NONE;
546
547         media_svc_retvm_if(query == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "query is NULL");
548
549         g_media_svc_update_list = g_list_append(g_media_svc_update_list, (gpointer)query);
550
551         if (g_list_length(g_media_svc_update_list) >= MEDIA_SVC_MAX_COMMIT_SIZE)
552                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_COMMON, uid);
553
554         return ret;
555 }
556
557 int _media_svc_get_noti_info(sqlite3 *handle, const char *storage_id, const char *path, int update_item, media_svc_noti_item **item)
558 {
559         int ret = MS_MEDIA_ERR_NONE;
560         sqlite3_stmt *sql_stmt = NULL;
561         char *sql = NULL;
562         int is_root_dir = FALSE;
563
564         media_svc_retvm_if(item == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "item is NULL");
565         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
566         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
567
568         if (update_item == MS_MEDIA_ITEM_FILE)
569                 sql = sqlite3_mprintf("SELECT media_uuid, media_type, mime_type FROM '%q' WHERE path=%Q", storage_id, path);
570         else if (update_item == MS_MEDIA_ITEM_DIRECTORY)
571                 sql = sqlite3_mprintf("SELECT folder_id FROM '%q' WHERE folder_path=%Q AND storage_uuid='%q'", MEDIA_SVC_DB_TABLE_FOLDER, path, storage_id);
572         else {
573                 media_svc_error("_media_svc_get_noti_info failed : update item");
574                 return MS_MEDIA_ERR_INVALID_PARAMETER;
575         }
576
577         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
578
579         if (ret != MS_MEDIA_ERR_NONE) {
580                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD && update_item == MS_MEDIA_ITEM_DIRECTORY) {
581                         media_svc_debug("This is root directory of media");
582                         sql_stmt = NULL;
583                         is_root_dir = TRUE;
584                 } else {
585                         media_svc_error("error when _media_svc_get_noti_info. err = [%d]", ret);
586                         return ret;
587                 }
588         }
589
590         *item = calloc(1, sizeof(media_svc_noti_item));
591         if (*item == NULL) {
592                 media_svc_error("_media_svc_get_noti_info failed : calloc");
593                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
594         }
595
596         if (update_item == MS_MEDIA_ITEM_FILE) {
597                 (*item)->media_uuid = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
598                 (*item)->media_type = sqlite3_column_int(sql_stmt, 1);
599                 (*item)->mime_type = g_strdup((const char *)sqlite3_column_text(sql_stmt, 2));
600         } else if (update_item == MS_MEDIA_ITEM_DIRECTORY) {
601                 if (is_root_dir)
602                         (*item)->media_uuid = NULL;
603                 else
604                         (*item)->media_uuid = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
605         }
606
607         SQLITE3_FINALIZE(sql_stmt);
608
609         return MS_MEDIA_ERR_NONE;
610 }