DatabasePrint.cxx 5.52 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * 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.
 *
 * 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.
 */

20
#include "DatabasePrint.hxx"
Max Kellermann's avatar
Max Kellermann committed
21
#include "Selection.hxx"
Max Kellermann's avatar
Max Kellermann committed
22 23
#include "SongPrint.hxx"
#include "TimePrint.hxx"
24 25
#include "client/Response.hxx"
#include "Partition.hxx"
26
#include "song/LightSong.hxx"
27
#include "tag/Tag.hxx"
28 29
#include "LightDirectory.hxx"
#include "PlaylistInfo.hxx"
30
#include "Interface.hxx"
31
#include "fs/Traits.hxx"
32
#include "time/ChronoUtil.hxx"
33
#include "util/RecursiveMap.hxx"
34

35 36
#include <fmt/format.h>

37
#include <functional>
38

39
gcc_pure
40
static const char *
41
ApplyBaseFlag(const char *uri, bool base) noexcept
42 43 44 45 46 47
{
	if (base)
		uri = PathTraitsUTF8::GetBase(uri);
	return uri;
}

48
static void
49 50
PrintDirectoryURI(Response &r, bool base,
		  const LightDirectory &directory) noexcept
51
{
52 53
	r.Fmt(FMT_STRING("directory: {}\n"),
	      ApplyBaseFlag(directory.GetPath(), base));
54 55
}

56
static void
57 58
PrintDirectoryBrief(Response &r, bool base,
		    const LightDirectory &directory) noexcept
59
{
60
	if (!directory.IsRoot())
61
		PrintDirectoryURI(r, base, directory);
62 63
}

64
static void
65 66
PrintDirectoryFull(Response &r, bool base,
		   const LightDirectory &directory) noexcept
67 68
{
	if (!directory.IsRoot()) {
69
		PrintDirectoryURI(r, base, directory);
70

71
		if (!IsNegative(directory.mtime))
72
			time_print(r, "Last-Modified", directory.mtime);
73 74 75
	}
}

76
static void
77
print_playlist_in_directory(Response &r, bool base,
78
			    const char *directory,
79
			    const char *name_utf8) noexcept
80
{
81
	if (base || directory == nullptr)
82 83
		r.Fmt(FMT_STRING("playlist: {}\n"),
		      ApplyBaseFlag(name_utf8, base));
84
	else
85 86
		r.Fmt(FMT_STRING("playlist: {}/{}\n"),
		      directory, name_utf8);
87
}
88

89
static void
90
print_playlist_in_directory(Response &r, bool base,
91
			    const LightDirectory *directory,
92
			    const char *name_utf8) noexcept
93
{
94
	if (base || directory == nullptr || directory->IsRoot())
95
		r.Fmt(FMT_STRING("playlist: {}\n"), name_utf8);
96
	else
97 98
		r.Fmt(FMT_STRING("playlist: {}/{}\n"),
		      directory->GetPath(), name_utf8);
99 100
}

101
static void
102
PrintSongBrief(Response &r, bool base, const LightSong &song) noexcept
103
{
104
	song_print_uri(r, song, base);
105

106
	if (song.tag.has_playlist)
107
		/* this song file has an embedded CUE sheet */
108
		print_playlist_in_directory(r, base,
109
					    song.directory, song.uri);
110 111
}

112
static void
113
PrintSongFull(Response &r, bool base, const LightSong &song) noexcept
114
{
115
	song_print_info(r, song, base);
116

117
	if (song.tag.has_playlist)
118
		/* this song file has an embedded CUE sheet */
119
		print_playlist_in_directory(r, base,
120
					    song.directory, song.uri);
121 122
}

123
static void
124
PrintPlaylistBrief(Response &r, bool base,
125
		   const PlaylistInfo &playlist,
126
		   const LightDirectory &directory) noexcept
127
{
128
	print_playlist_in_directory(r, base,
129
				    &directory, playlist.name.c_str());
130 131
}

132
static void
133
PrintPlaylistFull(Response &r, bool base,
134
		  const PlaylistInfo &playlist,
135
		  const LightDirectory &directory) noexcept
136
{
137
	print_playlist_in_directory(r, base,
138
				    &directory, playlist.name.c_str());
139

140
	if (!IsNegative(playlist.mtime))
141
		time_print(r, "Last-Modified", playlist.mtime);
142 143
}

144
void
145 146
db_selection_print(Response &r, Partition &partition,
		   const DatabaseSelection &selection,
147
		   bool full, bool base)
148
{
149
	const Database &db = partition.GetDatabaseOrThrow();
150

151
	const auto d = selection.filter == nullptr
152 153 154 155
		? [&,base](const auto &dir)
			{ return full ?
				PrintDirectoryFull(r, base, dir) :
				PrintDirectoryBrief(r, base, dir); }
156
		: VisitDirectory();
157 158 159 160 161 162

	VisitSong s = [&,base](const auto &song)
		{ return full ?
			PrintSongFull(r, base, song) :
			PrintSongBrief(r, base, song); };

163
	const auto p = selection.filter == nullptr
164 165 166 167
		? [&,base](const auto &playlist, const auto &dir)
			{ return full ?
				PrintPlaylistFull(r, base, playlist, dir) :
				PrintPlaylistBrief(r, base, playlist, dir); }
168
		: VisitPlaylist();
169

170
	db.Visit(selection, d, s, p);
171 172
}

173
static void
174
PrintSongURIVisitor(Response &r, const LightSong &song) noexcept
175
{
176
	song_print_uri(r, song);
177 178
}

179 180 181 182 183 184 185 186
void
PrintSongUris(Response &r, Partition &partition,
	      const SongFilter *filter)
{
	const Database &db = partition.GetDatabaseOrThrow();

	const DatabaseSelection selection("", true, filter);

187 188 189
	const auto f = [&](const auto &song)
		{ return PrintSongURIVisitor(r, song); };

190 191 192
	db.Visit(selection, f);
}

193
static void
194 195
PrintUniqueTags(Response &r, ConstBuffer<TagType> tag_types,
		const RecursiveMap<std::string> &map) noexcept
196
{
197 198
	const char *const name = tag_item_names[tag_types.front()];
	tag_types.pop_front();
199

200
	for (const auto &[key, tag] : map) {
201
		r.Fmt(FMT_STRING("{}: {}\n"), name, key);
202

203
		if (!tag_types.empty())
204
			PrintUniqueTags(r, tag_types, tag);
205
	}
206 207
}

208
void
209
PrintUniqueTags(Response &r, Partition &partition,
210
		ConstBuffer<TagType> tag_types,
211
		const SongFilter *filter)
212
{
213
	const Database &db = partition.GetDatabaseOrThrow();
214

215
	const DatabaseSelection selection("", true, filter);
216

217 218
	PrintUniqueTags(r, tag_types,
			db.CollectUniqueTags(selection, tag_types));
219
}