Apply tizen coding rule
[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         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
43
44         if (ret != MS_MEDIA_ERR_NONE) {
45                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
46                         media_svc_debug("there is no album.");
47                 else
48                         media_svc_error("error when _media_svc_get_album_id. err = [%d]", ret);
49
50                 return ret;
51         }
52
53         *album_id = sqlite3_column_int(sql_stmt, 0);
54
55         SQLITE3_FINALIZE(sql_stmt);
56
57         return ret;
58 }
59
60 int _media_svc_get_album_art_by_album_id(sqlite3 *handle, int album_id, char **album_art)
61 {
62         int ret = MS_MEDIA_ERR_NONE;
63         sqlite3_stmt *sql_stmt = NULL;
64         char *value = NULL;
65
66         char *sql = sqlite3_mprintf("SELECT album_art FROM %s WHERE album_id=%d", MEDIA_SVC_DB_TABLE_ALBUM, album_id);
67
68         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
69
70         if (ret != MS_MEDIA_ERR_NONE) {
71                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
72                         media_svc_debug("there is no album_id.");
73                 else
74                         media_svc_error("error when get album_art. err = [%d]", ret);
75
76                 return ret;
77         }
78
79         value = (char *)sqlite3_column_text(sql_stmt, 0);
80         if (STRING_VALID(value))
81                 *album_art = g_strdup(value);
82         else
83                 *album_art = NULL;
84
85         SQLITE3_FINALIZE(sql_stmt);
86
87         return MS_MEDIA_ERR_NONE;
88 }
89
90 int _media_svc_append_album(sqlite3 *handle, const char *album, const char *artist, const char *album_art, int *album_id, uid_t uid)
91 {
92         int ret = MS_MEDIA_ERR_NONE;
93
94         char *sql = sqlite3_mprintf("INSERT INTO %s (name, artist, album_art) values (%Q, %Q, %Q);", MEDIA_SVC_DB_TABLE_ALBUM, album, artist, album_art);
95         ret = _media_svc_sql_query(sql, uid);
96         SQLITE3_SAFE_FREE(sql);
97         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
98
99         /**album_id = sqlite3_last_insert_rowid(handle); */
100         int inserted_album_id = 0;
101         ret = _media_svc_get_album_id(handle, album, artist, &inserted_album_id);
102         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
103
104         *album_id = inserted_album_id;
105
106         return MS_MEDIA_ERR_NONE;
107 }