configure.ac: Move FAAD to Decoder Plugins, add header.
[mpd-mk.git] / src / song_sticker.c
blobc3c64c8d1d1b7f9e23466d6682765d5ca29d0976
1 /*
2 * Copyright (C) 2003-2010 The Music Player Daemon Project
3 * http://www.musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "config.h"
21 #include "song_sticker.h"
22 #include "song.h"
23 #include "directory.h"
24 #include "sticker.h"
26 #include <glib.h>
28 #include <assert.h>
29 #include <string.h>
31 char *
32 sticker_song_get_value(const struct song *song, const char *name)
34 char *uri, *value;
36 assert(song != NULL);
37 assert(song_in_database(song));
39 uri = song_get_uri(song);
40 value = sticker_load_value("song", uri, name);
41 g_free(uri);
43 return value;
46 bool
47 sticker_song_set_value(const struct song *song,
48 const char *name, const char *value)
50 char *uri;
51 bool ret;
53 assert(song != NULL);
54 assert(song_in_database(song));
56 uri = song_get_uri(song);
57 ret = sticker_store_value("song", uri, name, value);
58 g_free(uri);
60 return ret;
63 bool
64 sticker_song_delete(const struct song *song)
66 char *uri;
67 bool ret;
69 assert(song != NULL);
70 assert(song_in_database(song));
72 uri = song_get_uri(song);
73 ret = sticker_delete("song", uri);
74 g_free(uri);
76 return ret;
79 bool
80 sticker_song_delete_value(const struct song *song, const char *name)
82 char *uri;
83 bool success;
85 assert(song != NULL);
86 assert(song_in_database(song));
88 uri = song_get_uri(song);
89 success = sticker_delete_value("song", uri, name);
90 g_free(uri);
92 return success;
95 struct sticker *
96 sticker_song_get(const struct song *song)
98 char *uri;
99 struct sticker *sticker;
101 assert(song != NULL);
102 assert(song_in_database(song));
104 uri = song_get_uri(song);
105 sticker = sticker_load("song", uri);
106 g_free(uri);
108 return sticker;
111 struct sticker_song_find_data {
112 struct directory *directory;
113 const char *base_uri;
114 size_t base_uri_length;
116 void (*func)(struct song *song, const char *value,
117 gpointer user_data);
118 gpointer user_data;
121 static void
122 sticker_song_find_cb(const char *uri, const char *value, gpointer user_data)
124 struct sticker_song_find_data *data = user_data;
125 struct song *song;
127 if (memcmp(uri, data->base_uri, data->base_uri_length) != 0)
128 /* should not happen, ignore silently */
129 return;
131 song = directory_lookup_song(data->directory,
132 uri + data->base_uri_length);
133 if (song != NULL)
134 data->func(song, value, data->user_data);
137 bool
138 sticker_song_find(struct directory *directory, const char *name,
139 void (*func)(struct song *song, const char *value,
140 gpointer user_data),
141 gpointer user_data)
143 struct sticker_song_find_data data = {
144 .directory = directory,
145 .func = func,
146 .user_data = user_data,
148 char *allocated;
149 bool success;
151 data.base_uri = directory_get_path(directory);
152 if (*data.base_uri != 0)
153 /* append slash to base_uri */
154 data.base_uri = allocated =
155 g_strconcat(data.base_uri, "/", NULL);
156 else
157 /* searching in root directory - no trailing slash */
158 allocated = NULL;
160 data.base_uri_length = strlen(data.base_uri);
162 success = sticker_find("song", data.base_uri, name,
163 sticker_song_find_cb, &data);
164 g_free(allocated);
166 return success;