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

#include "config.h"
21
#include "DatabasePrint.hxx"
Max Kellermann's avatar
Max Kellermann committed
22
#include "Selection.hxx"
23
#include "SongFilter.hxx"
Max Kellermann's avatar
Max Kellermann committed
24
#include "SongPrint.hxx"
25
#include "DetachedSong.hxx"
Max Kellermann's avatar
Max Kellermann committed
26
#include "TimePrint.hxx"
27
#include "TagPrint.hxx"
28 29
#include "client/Response.hxx"
#include "Partition.hxx"
30
#include "tag/Tag.hxx"
31
#include "tag/Mask.hxx"
32
#include "LightSong.hxx"
33 34
#include "LightDirectory.hxx"
#include "PlaylistInfo.hxx"
35
#include "Interface.hxx"
36
#include "fs/Traits.hxx"
37
#include "util/ChronoUtil.hxx"
38

39
#include <functional>
40

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

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

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

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

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

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

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

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

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

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

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

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

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

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

146
gcc_pure
147
static bool
148
CompareNumeric(const char *a, const char *b) noexcept
149 150 151 152 153 154 155
{
	long a_value = strtol(a, nullptr, 10);
	long b_value = strtol(b, nullptr, 10);

	return a_value < b_value;
}

156
gcc_pure
157
static bool
158
CompareTags(TagType type, bool descending, const Tag &a, const Tag &b) noexcept
159 160 161 162
{
	const char *a_value = a.GetSortValue(type);
	const char *b_value = b.GetSortValue(type);

163 164 165 166 167
	if (descending) {
		using std::swap;
		swap(a_value, b_value);
	}

168 169 170 171 172 173 174 175 176 177
	switch (type) {
	case TAG_DISC:
	case TAG_TRACK:
		return CompareNumeric(a_value, b_value);

	default:
		return strcmp(a_value, b_value) < 0;
	}
}

178
void
179 180
db_selection_print(Response &r, Partition &partition,
		   const DatabaseSelection &selection,
181
		   bool full, bool base,
182
		   TagType sort, bool descending,
183
		   unsigned window_start, unsigned window_end)
184
{
185
	const Database &db = partition.GetDatabaseOrThrow();
186

187 188
	unsigned i = 0;

189
	using namespace std::placeholders;
190
	const auto d = selection.filter == nullptr
191
		? std::bind(full ? PrintDirectoryFull : PrintDirectoryBrief,
192
			    std::ref(r), base, _1)
193
		: VisitDirectory();
194
	VisitSong s = std::bind(full ? PrintSongFull : PrintSongBrief,
195
				std::ref(r), base, _1);
196
	const auto p = selection.filter == nullptr
197
		? std::bind(full ? PrintPlaylistFull : PrintPlaylistBrief,
198
			    std::ref(r), base, _1, _2)
199
		: VisitPlaylist();
200

201 202 203 204 205 206 207 208 209
	if (sort == TAG_NUM_OF_ITEM_TYPES) {
		if (window_start > 0 ||
		    window_end < (unsigned)std::numeric_limits<int>::max())
			s = [s, window_start, window_end, &i](const LightSong &song){
				const bool in_window = i >= window_start && i < window_end;
				++i;
				if (in_window)
					s(song);
			};
210

211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
		db.Visit(selection, d, s, p);
	} else {
		// TODO: allow the database plugin to sort internally

		/* the client has asked us to sort the result; this is
		   pretty expensive, because instead of streaming the
		   result to the client, we need to copy it all into
		   this std::vector, and then sort it */
		std::vector<DetachedSong> songs;

		{
			auto collect_songs = [&songs](const LightSong &song){
				songs.emplace_back(song);
			};

			db.Visit(selection, d, collect_songs, p);
		}

229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
		if (sort == TagType(SORT_TAG_LAST_MODIFIED))
			std::stable_sort(songs.begin(), songs.end(),
					 [descending](const DetachedSong &a, const DetachedSong &b){
						 return descending
							 ? a.GetLastModified() > b.GetLastModified()
							 : a.GetLastModified() < b.GetLastModified();
					 });
		else
			std::stable_sort(songs.begin(), songs.end(),
					 [sort, descending](const DetachedSong &a,
							    const DetachedSong &b){
						 return CompareTags(sort, descending,
								    a.GetTag(),
								    b.GetTag());
					 });
244 245 246 247 248 249 250 251 252 253 254 255 256 257

		if (window_end < songs.size())
			songs.erase(std::next(songs.begin(), window_end),
				    songs.end());

		if (window_start >= songs.size())
			return;

		songs.erase(songs.begin(),
			    std::next(songs.begin(), window_start));

		for (const auto &song : songs)
			s((LightSong)song);
	}
258 259
}

260
void
261 262
db_selection_print(Response &r, Partition &partition,
		   const DatabaseSelection &selection,
263
		   bool full, bool base)
264
{
265
	db_selection_print(r, partition, selection, full, base,
266
			   TAG_NUM_OF_ITEM_TYPES, false,
267
			   0, std::numeric_limits<int>::max());
268 269
}

270
static void
271
PrintSongURIVisitor(Response &r, const LightSong &song) noexcept
272
{
273
	song_print_uri(r, song);
274 275
}

276 277 278 279 280 281 282 283 284 285 286 287 288 289
void
PrintSongUris(Response &r, Partition &partition,
	      const SongFilter *filter)
{
	const Database &db = partition.GetDatabaseOrThrow();

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

	using namespace std::placeholders;
	const auto f = std::bind(PrintSongURIVisitor,
				 std::ref(r), _1);
	db.Visit(selection, f);
}

290
static void
291
PrintUniqueTag(Response &r, TagType tag_type,
292
	       const Tag &tag) noexcept
293
{
294 295
	const char *value = tag.GetValue(tag_type);
	assert(value != nullptr);
296
	tag_print(r, tag_type, value);
297

298
	const auto tag_mask = r.GetTagMask();
299
	for (const auto &item : tag)
300
		if (item.type != tag_type && tag_mask.Test(item.type))
301
			tag_print(r, item.type, item.value);
302 303
}

304
void
305
PrintUniqueTags(Response &r, Partition &partition,
306
		TagType type, TagMask group_mask,
307
		const SongFilter *filter)
308
{
309 310
	assert(type < TAG_NUM_OF_ITEM_TYPES);

311
	const Database &db = partition.GetDatabaseOrThrow();
312

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

315
	using namespace std::placeholders;
316 317
	const auto f = std::bind(PrintUniqueTag, std::ref(r), type, _1);
	db.VisitUniqueTags(selection, type, group_mask, f);
318
}