DatabasePrint.cxx 5.43 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
#include <functional>
36

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

46
static void
47 48
PrintDirectoryURI(Response &r, bool base,
		  const LightDirectory &directory) noexcept
49
{
50 51
	r.Format("directory: %s\n",
		 ApplyBaseFlag(directory.GetPath(), base));
52 53
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

168
	db.Visit(selection, d, s, p);
169 170
}

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

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

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

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

188 189 190
	db.Visit(selection, f);
}

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

198 199
	for (const auto &[key, tag] : map) {
		r.Format("%s: %s\n", name, key.c_str());
200

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

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

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

215 216
	PrintUniqueTags(r, tag_types,
			db.CollectUniqueTags(selection, tag_types));
217
}