SongSticker.cxx 3.31 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"
Max Kellermann's avatar
Max Kellermann committed
23
#include "db/LightSong.hxx"
24
#include "db/Interface.hxx"
25
#include "util/Alloc.hxx"
26
#include "util/ScopeExit.hxx"
27

28 29
#include <stdexcept>

30
#include <string.h>
31
#include <stdlib.h>
32

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

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

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

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

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

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

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

79
	void (*func)(const LightSong &song, const char *value,
80 81
		     void *user_data);
	void *user_data;
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 (const std::runtime_error &e) {
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
}