SongSticker.cxx 3.26 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
#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

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
};

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

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

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

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

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

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

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

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