SongSticker.cxx 3.19 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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/Error.hxx"
26 27 28 29

#include <glib.h>

#include <assert.h>
30
#include <string.h>
31

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

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

bool
48
sticker_song_delete(const LightSong &song)
49
{
50
	const auto uri = song.GetURI();
51
	return sticker_delete("song", uri.c_str());
52
}
53

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

61
struct sticker *
62
sticker_song_get(const LightSong &song)
63
{
64
	const auto uri = song.GetURI();
65
	return sticker_load("song", uri.c_str());
66
}
67 68

struct sticker_song_find_data {
69
	const Database *db;
70 71 72
	const char *base_uri;
	size_t base_uri_length;

73
	void (*func)(const LightSong &song, const char *value,
74 75
		     void *user_data);
	void *user_data;
76 77 78
};

static void
79
sticker_song_find_cb(const char *uri, const char *value, void *user_data)
80
{
Max Kellermann's avatar
Max Kellermann committed
81 82
	struct sticker_song_find_data *data =
		(struct sticker_song_find_data *)user_data;
83 84 85 86 87

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

88 89 90 91 92 93
	const Database *db = data->db;
	const LightSong *song = db->GetSong(uri, IgnoreError());
	if (song != nullptr) {
		data->func(*song, value, data->user_data);
		db->ReturnSong(song);
	}
94 95 96
}

bool
97
sticker_song_find(const Database &db, const char *base_uri, const char *name,
98
		  void (*func)(const LightSong &song, const char *value,
99 100
			       void *user_data),
		  void *user_data)
101
{
Max Kellermann's avatar
Max Kellermann committed
102
	struct sticker_song_find_data data;
103
	data.db = &db;
Max Kellermann's avatar
Max Kellermann committed
104 105
	data.func = func;
	data.user_data = user_data;
106

Max Kellermann's avatar
Max Kellermann committed
107
	char *allocated;
108
	data.base_uri = base_uri;
109 110 111
	if (*data.base_uri != 0)
		/* append slash to base_uri */
		data.base_uri = allocated =
112
			g_strconcat(data.base_uri, "/", nullptr);
113 114
	else
		/* searching in root directory - no trailing slash */
115
		allocated = nullptr;
116 117 118

	data.base_uri_length = strlen(data.base_uri);

Max Kellermann's avatar
Max Kellermann committed
119 120
	bool success = sticker_find("song", data.base_uri, name,
				    sticker_song_find_cb, &data);
121 122 123 124
	g_free(allocated);

	return success;
}