Update scanner process
[platform/core/multimedia/libmedia-service.git] / src / common / media-svc-album.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 <media-util-err.h>
23 #include "media-svc-album.h"
24 #include "media-svc-debug.h"
25 #include "media-svc-env.h"
26 #include "media-svc-util.h"
27 #include "media-svc-db-utils.h"
28
29 int _media_svc_get_album_id(sqlite3 *handle, const char *album, const char *artist, int *album_id)
30 {
31         int ret = MS_MEDIA_ERR_NONE;
32         sqlite3_stmt *sql_stmt = NULL;
33         char *sql = NULL;
34
35         media_svc_retvm_if(album == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "album is NULL");
36
37         if (artist != NULL) {
38                 sql = sqlite3_mprintf("SELECT album_id FROM %s WHERE name = '%q' AND artist = '%q';", MEDIA_SVC_DB_TABLE_ALBUM, album, artist);
39         } else {
40                 sql = sqlite3_mprintf("SELECT album_id FROM %s WHERE name = '%q' AND artist IS NULL;", MEDIA_SVC_DB_TABLE_ALBUM, album, artist);
41         }
42
43         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
44
45         if (ret != MS_MEDIA_ERR_NONE) {
46                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
47                         media_svc_debug("there is no album.");
48                 } else {
49                         media_svc_error("error when _media_svc_get_album_id. err = [%d]", ret);
50                 }
51                 return ret;
52         }
53
54         *album_id = sqlite3_column_int(sql_stmt, 0);
55
56         SQLITE3_FINALIZE(sql_stmt);
57
58         return ret;
59 }
60
61 int _media_svc_get_album_art_by_album_id(sqlite3 *handle, int album_id, char **album_art)
62 {
63         int ret = MS_MEDIA_ERR_NONE;
64         sqlite3_stmt *sql_stmt = NULL;
65         char *value = NULL;
66
67         char *sql = sqlite3_mprintf("SELECT album_art FROM %s WHERE album_id=%d", MEDIA_SVC_DB_TABLE_ALBUM, album_id);
68
69         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
70
71         if (ret != MS_MEDIA_ERR_NONE) {
72                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
73                         media_svc_debug("there is no album_id.");
74                 } else {
75                         media_svc_error("error when get album_art. err = [%d]", ret);
76                 }
77                 return ret;
78         }
79
80         value = (char *)sqlite3_column_text(sql_stmt, 0);
81
82         if (STRING_VALID(value)) {
83                 ret = __media_svc_malloc_and_strncpy(album_art, value);
84                 if (ret != MS_MEDIA_ERR_NONE) {
85                         SQLITE3_FINALIZE(sql_stmt);
86                         return ret;
87                 }
88         } else {
89                 *album_art = NULL;
90         }
91
92         SQLITE3_FINALIZE(sql_stmt);
93
94         return MS_MEDIA_ERR_NONE;
95 }
96
97 int _media_svc_append_album(sqlite3 *handle, const char *album, const char *artist, const char *album_art, int *album_id, uid_t uid)
98 {
99         int ret = MS_MEDIA_ERR_NONE;
100
101         char *sql = sqlite3_mprintf("INSERT INTO %s (name, artist, album_art, album_art) values (%Q, %Q, %Q, %Q); ",
102                                     MEDIA_SVC_DB_TABLE_ALBUM, album, artist, album_art, album_art);
103         ret = _media_svc_sql_query(handle, sql, uid);
104         sqlite3_free(sql);
105         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
106
107         /**album_id = sqlite3_last_insert_rowid(handle); */
108         int inserted_album_id = 0;
109         ret = _media_svc_get_album_id(handle, album, artist, &inserted_album_id);
110         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
111
112         *album_id = inserted_album_id;
113
114         return MS_MEDIA_ERR_NONE;
115 }
116
117 int _media_svc_get_media_count_with_album_id_by_path(sqlite3 *handle, const char *path, int *count)
118 {
119         int ret = MS_MEDIA_ERR_NONE;
120         sqlite3_stmt *sql_stmt = NULL;
121         char *sql = NULL;
122
123         media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
124
125         sql = sqlite3_mprintf("select count(media_uuid) from %s INNER JOIN (select album_id from %s where path=%Q and album_id > 0) as album ON album.album_id=media.album_id;", MEDIA_SVC_DB_TABLE_MEDIA, MEDIA_SVC_DB_TABLE_MEDIA, path);
126         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
127
128         if (ret != MS_MEDIA_ERR_NONE) {
129                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
130                         media_svc_debug("there is no media in relted to this media's album.");
131                 } else {
132                         media_svc_error("error when _media_svc_get_media_count_with_album_id_by_path. err = [%d]", ret);
133                 }
134                 return ret;
135         }
136
137         *count = sqlite3_column_int(sql_stmt, 0);
138         media_svc_debug("Media count : %d", *count);
139
140         SQLITE3_FINALIZE(sql_stmt);
141
142         return ret;
143 }