DatabasePrint.cxx 6.25 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2015 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
#include "client/Client.hxx"
27 28
#include "client/Response.hxx"
#include "Partition.hxx"
29
#include "tag/Tag.hxx"
30
#include "LightSong.hxx"
31 32
#include "LightDirectory.hxx"
#include "PlaylistInfo.hxx"
33
#include "Interface.hxx"
34
#include "fs/Traits.hxx"
35

36
#include <functional>
37

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

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

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

59
	return true;
60 61
}

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

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

	return true;
}

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

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

100
static bool
101 102
PrintSongBrief(Response &r, Partition &partition,
	       bool base, const LightSong &song)
103
{
104
	song_print_uri(r, partition, 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
	return true;
112 113
}

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

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

	return true;
126 127
}

128
static bool
129
PrintPlaylistBrief(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 137 138
	return true;
}

static bool
139
PrintPlaylistFull(Response &r, bool base,
140
		  const PlaylistInfo &playlist,
141
		  const LightDirectory &directory)
142
{
143
	print_playlist_in_directory(r, base,
144
				    &directory, playlist.name.c_str());
145

146
	if (playlist.mtime > 0)
147
		time_print(r, "Last-Modified", playlist.mtime);
148 149 150 151

	return true;
}

152
bool
153 154
db_selection_print(Response &r, Partition &partition,
		   const DatabaseSelection &selection,
155 156 157
		   bool full, bool base,
		   unsigned window_start, unsigned window_end,
		   Error &error)
158
{
159
	const Database *db = partition.GetDatabase(error);
160 161 162
	if (db == nullptr)
		return false;

163 164
	unsigned i = 0;

165
	using namespace std::placeholders;
166
	const auto d = selection.filter == nullptr
167
		? std::bind(full ? PrintDirectoryFull : PrintDirectoryBrief,
168
			    std::ref(r), base, _1)
169
		: VisitDirectory();
170
	VisitSong s = std::bind(full ? PrintSongFull : PrintSongBrief,
171
				std::ref(r), std::ref(partition), base, _1);
172
	const auto p = selection.filter == nullptr
173
		? std::bind(full ? PrintPlaylistFull : PrintPlaylistBrief,
174
			    std::ref(r), base, _1, _2)
175
		: VisitPlaylist();
176

177 178
	if (window_start > 0 ||
	    window_end < (unsigned)std::numeric_limits<int>::max())
179 180 181 182 183 184 185
		s = [s, window_start, window_end, &i](const LightSong &song,
						      Error &error2){
			const bool in_window = i >= window_start && i < window_end;
			++i;
			return !in_window || s(song, error2);
		};

186
	return db->Visit(selection, d, s, p, error);
187 188
}

189
bool
190 191
db_selection_print(Response &r, Partition &partition,
		   const DatabaseSelection &selection,
192 193 194
		   bool full, bool base,
		   Error &error)
{
195
	return db_selection_print(r, partition, selection, full, base,
196 197 198 199
				  0, std::numeric_limits<int>::max(),
				  error);
}

200
static bool
201
PrintSongURIVisitor(Response &r, Partition &partition, const LightSong &song)
202
{
203
	song_print_uri(r, partition, song);
204

205
	return true;
206 207
}

208
static bool
209
PrintUniqueTag(Response &r, TagType tag_type,
210
	       const Tag &tag)
211
{
212 213
	const char *value = tag.GetValue(tag_type);
	assert(value != nullptr);
214
	r.Format("%s: %s\n", tag_item_names[tag_type], value);
215

216
	for (const auto &item : tag)
217
		if (item.type != tag_type)
218 219
			r.Format("%s: %s\n",
				 tag_item_names[item.type], item.value);
220

221
	return true;
222 223
}

224
bool
225
PrintUniqueTags(Response &r, Partition &partition,
226
		unsigned type, tag_mask_t group_mask,
227 228
		const SongFilter *filter,
		Error &error)
229
{
230
	const Database *db = partition.GetDatabase(error);
231 232 233
	if (db == nullptr)
		return false;

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

236 237
	if (type == LOCATE_TAG_FILE_TYPE) {
		using namespace std::placeholders;
238
		const auto f = std::bind(PrintSongURIVisitor,
239
					 std::ref(r), std::ref(partition), _1);
240
		return db->Visit(selection, f, error);
241
	} else {
242 243
		assert(type < TAG_NUM_OF_ITEM_TYPES);

244
		using namespace std::placeholders;
245
		const auto f = std::bind(PrintUniqueTag, std::ref(r),
246 247
					 (TagType)type, _1);
		return db->VisitUniqueTags(selection, (TagType)type,
248
					   group_mask,
249
					   f, error);
250
	}
251
}