SongSticker.cxx 3.36 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2013 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.hxx"
24
#include "Directory.hxx"
25 26 27 28

#include <glib.h>

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

31
std::string
32
sticker_song_get_value(const Song *song, const char *name)
33
{
34
	assert(song != nullptr);
35
	assert(song->IsInDatabase());
36

37 38
	const auto uri = song->GetURI();
	return sticker_load_value("song", uri.c_str(), name);
39 40 41
}

bool
42
sticker_song_set_value(const Song *song,
43 44
		       const char *name, const char *value)
{
45
	assert(song != nullptr);
46
	assert(song->IsInDatabase());
47

48 49
	const auto uri = song->GetURI();
	return sticker_store_value("song", uri.c_str(), name, value);
50 51 52
}

bool
53
sticker_song_delete(const Song *song)
54
{
55
	assert(song != nullptr);
56
	assert(song->IsInDatabase());
57

58 59
	const auto uri = song->GetURI();
	return sticker_delete("song", uri.c_str());
60
}
61

62
bool
63
sticker_song_delete_value(const Song *song, const char *name)
64
{
65
	assert(song != nullptr);
66
	assert(song->IsInDatabase());
67

68 69
	const auto uri = song->GetURI();
	return sticker_delete_value("song", uri.c_str(), name);
70 71
}

72
struct sticker *
73
sticker_song_get(const Song *song)
74
{
75
	assert(song != nullptr);
76
	assert(song->IsInDatabase());
77

78 79
	const auto uri = song->GetURI();
	return sticker_load("song", uri.c_str());
80
}
81 82

struct sticker_song_find_data {
83
	Directory *directory;
84 85 86
	const char *base_uri;
	size_t base_uri_length;

87
	void (*func)(Song &song, const char *value,
88 89
		     void *user_data);
	void *user_data;
90 91 92
};

static void
93
sticker_song_find_cb(const char *uri, const char *value, void *user_data)
94
{
Max Kellermann's avatar
Max Kellermann committed
95 96
	struct sticker_song_find_data *data =
		(struct sticker_song_find_data *)user_data;
97 98 99 100 101

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

102
	Song *song = data->directory->LookupSong(uri + data->base_uri_length);
103
	if (song != nullptr)
104
		data->func(*song, value, data->user_data);
105 106 107
}

bool
108 109
sticker_song_find(Directory &directory, const char *name,
		  void (*func)(Song &song, const char *value,
110 111
			       void *user_data),
		  void *user_data)
112
{
Max Kellermann's avatar
Max Kellermann committed
113
	struct sticker_song_find_data data;
114
	data.directory = &directory;
Max Kellermann's avatar
Max Kellermann committed
115 116
	data.func = func;
	data.user_data = user_data;
117

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

	data.base_uri_length = strlen(data.base_uri);

Max Kellermann's avatar
Max Kellermann committed
130 131
	bool success = sticker_find("song", data.base_uri, name,
				    sticker_song_find_cb, &data);
132 133 134 135
	g_free(allocated);

	return success;
}