SongSticker.cxx 3.16 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 24 25
#include "db/LightSong.hxx"
#include "db/Song.hxx"
#include "db/Directory.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
	Directory *directory;
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
	Song *song = data->directory->LookupSong(uri + data->base_uri_length);
89
	if (song != nullptr)
90
		data->func(song->Export(), value, data->user_data);
91 92 93
}

bool
94
sticker_song_find(Directory &directory, const char *name,
95
		  void (*func)(const LightSong &song, const char *value,
96 97
			       void *user_data),
		  void *user_data)
98
{
Max Kellermann's avatar
Max Kellermann committed
99
	struct sticker_song_find_data data;
100
	data.directory = &directory;
Max Kellermann's avatar
Max Kellermann committed
101 102
	data.func = func;
	data.user_data = user_data;
103

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

	data.base_uri_length = strlen(data.base_uri);

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

	return success;
}