SongSticker.cxx 3.43 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 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
#include "SongSticker.hxx"
21
#include "Sticker.hxx"
22
#include "Database.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 33
sticker_song_get_value(StickerDatabase &db,
		       const LightSong &song, const char *name)
34
{
35
	const auto uri = song.GetURI();
36
	return db.LoadValue("song", uri.c_str(), name);
37 38
}

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

48
bool
49
sticker_song_delete(StickerDatabase &db, const char *uri)
50
{
51
	return db.Delete("song", uri);
52 53
}

54
bool
55
sticker_song_delete(StickerDatabase &db, const LightSong &song)
56
{
57
	return sticker_song_delete(db, song.GetURI().c_str());
58
}
59

60
bool
61 62
sticker_song_delete_value(StickerDatabase &db,
			  const LightSong &song, const char *name)
63
{
64
	const auto uri = song.GetURI();
65
	return db.DeleteValue("song", uri.c_str(), name);
66 67
}

68
Sticker
69
sticker_song_get(StickerDatabase &db, const LightSong &song)
70
{
71
	const auto uri = song.GetURI();
72
	return db.Load("song", uri.c_str());
73
}
74

75
namespace {
76
struct sticker_song_find_data {
77
	const Database *db;
78 79 80
	const char *base_uri;
	size_t base_uri_length;

81
	void (*func)(const LightSong &song, const char *value,
82 83
		     void *user_data);
	void *user_data;
84
};
85
} // namespace
86 87

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

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

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

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

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

129
	AtScopeExit(allocated) { free(allocated); };
130

131
	data.base_uri_length = strlen(data.base_uri);
132

133 134
	sticker_database.Find("song", data.base_uri, name, op, value,
			      sticker_song_find_cb, &data);
135
}