SongSticker.cxx 3.28 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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
 */

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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