SongSticker.cxx 3.26 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
14 15 16 17
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 19
 */

Max Kellermann's avatar
Max Kellermann committed
20 21
#include "SongSticker.hxx"
#include "StickerDatabase.hxx"
22
#include "song/LightSong.hxx"
23
#include "db/Interface.hxx"
24
#include "util/Alloc.hxx"
25
#include "util/ScopeExit.hxx"
26

27
#include <string.h>
28
#include <stdlib.h>
29

30
std::string
31
sticker_song_get_value(const LightSong &song, const char *name)
32
{
33
	const auto uri = song.GetURI();
34
	return sticker_load_value("song", uri.c_str(), name);
35 36
}

37
void
38
sticker_song_set_value(const LightSong &song,
39
		       const char *name, const char *value)
40
{
41
	const auto uri = song.GetURI();
42
	sticker_store_value("song", uri.c_str(), name, value);
43 44
}

45
bool
46
sticker_song_delete(const char *uri)
47
{
48
	return sticker_delete("song", uri);
49 50
}

51
bool
52
sticker_song_delete(const LightSong &song)
53
{
54
	return sticker_song_delete(song.GetURI().c_str());
55
}
56

57
bool
58
sticker_song_delete_value(const LightSong &song, const char *name)
59
{
60
	const auto uri = song.GetURI();
61
	return sticker_delete_value("song", uri.c_str(), name);
62 63
}

64
Sticker *
65
sticker_song_get(const LightSong &song)
66
{
67
	const auto uri = song.GetURI();
68
	return sticker_load("song", uri.c_str());
69
}
70

71
namespace {
72
struct sticker_song_find_data {
73
	const Database *db;
74 75 76
	const char *base_uri;
	size_t base_uri_length;

77
	void (*func)(const LightSong &song, const char *value,
78 79
		     void *user_data);
	void *user_data;
80
};
81
}
82 83

static void
84
sticker_song_find_cb(const char *uri, const char *value, void *user_data)
85
{
Max Kellermann's avatar
Max Kellermann committed
86 87
	struct sticker_song_find_data *data =
		(struct sticker_song_find_data *)user_data;
88 89 90 91 92

	if (memcmp(uri, data->base_uri, data->base_uri_length) != 0)
		/* should not happen, ignore silently */
		return;

93
	const Database *db = data->db;
94
	try {
95 96 97
		const LightSong *song = db->GetSong(uri);
		data->func(*song, value, data->user_data);
		db->ReturnSong(song);
98
	} catch (...) {
99
	}
100 101
}

102
void
103
sticker_song_find(const Database &db, const char *base_uri, const char *name,
104
		  StickerOperator op, const char *value,
105
		  void (*func)(const LightSong &song, const char *value,
106
			       void *user_data),
107
		  void *user_data)
108
{
Max Kellermann's avatar
Max Kellermann committed
109
	struct sticker_song_find_data data;
110
	data.db = &db;
Max Kellermann's avatar
Max Kellermann committed
111 112
	data.func = func;
	data.user_data = user_data;
113

Max Kellermann's avatar
Max Kellermann committed
114
	char *allocated;
115
	data.base_uri = base_uri;
116 117 118
	if (*data.base_uri != 0)
		/* append slash to base_uri */
		data.base_uri = allocated =
119
			xstrcatdup(data.base_uri, "/");
120 121
	else
		/* searching in root directory - no trailing slash */
122
		allocated = nullptr;
123

124
	AtScopeExit(allocated) { free(allocated); };
125

126
	data.base_uri_length = strlen(data.base_uri);
127

128 129
	sticker_find("song", data.base_uri, name, op, value,
		     sticker_song_find_cb, &data);
130
}