DatabasePrint.cxx 5.87 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 25
#include "SongPrint.hxx"
#include "TimePrint.hxx"
26 27
#include "client/Response.hxx"
#include "Partition.hxx"
28
#include "tag/Tag.hxx"
29
#include "LightSong.hxx"
30 31
#include "LightDirectory.hxx"
#include "PlaylistInfo.hxx"
32
#include "Interface.hxx"
33
#include "fs/Traits.hxx"
34

35
#include <functional>
36

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

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

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

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

		if (directory.mtime > 0)
66
			time_print(r, "Last-Modified", directory.mtime);
67 68 69
	}
}

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

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

95
static void
96 97
PrintSongBrief(Response &r, Partition &partition,
	       bool base, const LightSong &song)
98
{
99
	song_print_uri(r, partition, song, base);
100

101
	if (song.tag->has_playlist)
102
		/* this song file has an embedded CUE sheet */
103
		print_playlist_in_directory(r, base,
104
					    song.directory, song.uri);
105 106
}

107
static void
108 109
PrintSongFull(Response &r, Partition &partition,
	      bool base, const LightSong &song)
110
{
111
	song_print_info(r, partition, song, base);
112

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

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

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

136
	if (playlist.mtime > 0)
137
		time_print(r, "Last-Modified", playlist.mtime);
138 139
}

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

148 149
	unsigned i = 0;

150
	using namespace std::placeholders;
151
	const auto d = selection.filter == nullptr
152
		? std::bind(full ? PrintDirectoryFull : PrintDirectoryBrief,
153
			    std::ref(r), base, _1)
154
		: VisitDirectory();
155
	VisitSong s = std::bind(full ? PrintSongFull : PrintSongBrief,
156
				std::ref(r), std::ref(partition), base, _1);
157
	const auto p = selection.filter == nullptr
158
		? std::bind(full ? PrintPlaylistFull : PrintPlaylistBrief,
159
			    std::ref(r), base, _1, _2)
160
		: VisitPlaylist();
161

162 163
	if (window_start > 0 ||
	    window_end < (unsigned)std::numeric_limits<int>::max())
164
		s = [s, window_start, window_end, &i](const LightSong &song){
165 166
			const bool in_window = i >= window_start && i < window_end;
			++i;
167 168
			if (in_window)
				s(song);
169 170
		};

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

174
void
175 176
db_selection_print(Response &r, Partition &partition,
		   const DatabaseSelection &selection,
177
		   bool full, bool base)
178
{
179 180
	db_selection_print(r, partition, selection, full, base,
			   0, std::numeric_limits<int>::max());
181 182
}

183
static void
184
PrintSongURIVisitor(Response &r, Partition &partition, const LightSong &song)
185
{
186
	song_print_uri(r, partition, song);
187 188
}

189
static void
190
PrintUniqueTag(Response &r, TagType tag_type,
191
	       const Tag &tag)
192
{
193 194
	const char *value = tag.GetValue(tag_type);
	assert(value != nullptr);
195
	r.Format("%s: %s\n", tag_item_names[tag_type], value);
196

197
	for (const auto &item : tag)
198
		if (item.type != tag_type)
199 200
			r.Format("%s: %s\n",
				 tag_item_names[item.type], item.value);
201 202
}

203
void
204
PrintUniqueTags(Response &r, Partition &partition,
205
		unsigned type, tag_mask_t group_mask,
206
		const SongFilter *filter)
207
{
208
	const Database &db = partition.GetDatabaseOrThrow();
209

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

212 213
	if (type == LOCATE_TAG_FILE_TYPE) {
		using namespace std::placeholders;
214
		const auto f = std::bind(PrintSongURIVisitor,
215
					 std::ref(r), std::ref(partition), _1);
216
		db.Visit(selection, f);
217
	} else {
218 219
		assert(type < TAG_NUM_OF_ITEM_TYPES);

220
		using namespace std::placeholders;
221
		const auto f = std::bind(PrintUniqueTag, std::ref(r),
222
					 (TagType)type, _1);
223 224
		db.VisitUniqueTags(selection, (TagType)type,
				   group_mask, f);
225
	}
226
}