DatabasePrint.cxx 7.79 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 42 43 44 45 46 47 48
static const char *
ApplyBaseFlag(const char *uri, bool base)
{
	if (base)
		uri = PathTraitsUTF8::GetBase(uri);
	return uri;
}

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

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

63
static void
64
PrintDirectoryFull(Response &r, bool base, const LightDirectory &directory)
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 77 78
			    const char *directory,
			    const char *name_utf8)
{
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 91
			    const char *name_utf8)
{
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)
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)
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)
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)
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 143 144 145 146 147 148 149 150 151
static bool
CompareNumeric(const char *a, const char *b)
{
	long a_value = strtol(a, nullptr, 10);
	long b_value = strtol(b, nullptr, 10);

	return a_value < b_value;
}

static bool
152
CompareTags(TagType type, bool descending, const Tag &a, const Tag &b)
153 154 155 156
{
	const char *a_value = a.GetSortValue(type);
	const char *b_value = b.GetSortValue(type);

157 158 159 160 161
	if (descending) {
		using std::swap;
		swap(a_value, b_value);
	}

162 163 164 165 166 167 168 169 170 171
	switch (type) {
	case TAG_DISC:
	case TAG_TRACK:
		return CompareNumeric(a_value, b_value);

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

172
void
173 174
db_selection_print(Response &r, Partition &partition,
		   const DatabaseSelection &selection,
175
		   bool full, bool base,
176
		   TagType sort, bool descending,
177
		   unsigned window_start, unsigned window_end)
178
{
179
	const Database &db = partition.GetDatabaseOrThrow();
180

181 182
	unsigned i = 0;

183
	using namespace std::placeholders;
184
	const auto d = selection.filter == nullptr
185
		? std::bind(full ? PrintDirectoryFull : PrintDirectoryBrief,
186
			    std::ref(r), base, _1)
187
		: VisitDirectory();
188
	VisitSong s = std::bind(full ? PrintSongFull : PrintSongBrief,
189
				std::ref(r), base, _1);
190
	const auto p = selection.filter == nullptr
191
		? std::bind(full ? PrintPlaylistFull : PrintPlaylistBrief,
192
			    std::ref(r), base, _1, _2)
193
		: VisitPlaylist();
194

195 196 197 198 199 200 201 202 203
	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);
			};
204

205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
		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);
		}

223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
		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());
					 });
238 239 240 241 242 243 244 245 246 247 248 249 250 251

		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);
	}
252 253
}

254
void
255 256
db_selection_print(Response &r, Partition &partition,
		   const DatabaseSelection &selection,
257
		   bool full, bool base)
258
{
259
	db_selection_print(r, partition, selection, full, base,
260
			   TAG_NUM_OF_ITEM_TYPES, false,
261
			   0, std::numeric_limits<int>::max());
262 263
}

264
static void
265
PrintSongURIVisitor(Response &r, const LightSong &song)
266
{
267
	song_print_uri(r, song);
268 269
}

270
static void
271
PrintUniqueTag(Response &r, TagType tag_type,
272
	       const Tag &tag)
273
{
274 275
	const char *value = tag.GetValue(tag_type);
	assert(value != nullptr);
276
	tag_print(r, tag_type, value);
277

278
	const auto tag_mask = r.GetTagMask();
279
	for (const auto &item : tag)
280
		if (item.type != tag_type && tag_mask.Test(item.type))
281
			tag_print(r, item.type, item.value);
282 283
}

284
void
285
PrintUniqueTags(Response &r, Partition &partition,
286
		unsigned type, TagMask group_mask,
287
		const SongFilter *filter)
288
{
289
	const Database &db = partition.GetDatabaseOrThrow();
290

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

293 294
	if (type == LOCATE_TAG_FILE_TYPE) {
		using namespace std::placeholders;
295
		const auto f = std::bind(PrintSongURIVisitor,
296
					 std::ref(r), _1);
297
		db.Visit(selection, f);
298
	} else {
299 300
		assert(type < TAG_NUM_OF_ITEM_TYPES);

301
		using namespace std::placeholders;
302
		const auto f = std::bind(PrintUniqueTag, std::ref(r),
303
					 (TagType)type, _1);
304 305
		db.VisitUniqueTags(selection, (TagType)type,
				   group_mask, f);
306
	}
307
}