DatabasePrint.cxx 5.99 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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 bool
53
PrintDirectoryBrief(Response &r, bool base, const LightDirectory &directory)
54
{
55
	if (!directory.IsRoot())
56
		PrintDirectoryURI(r, base, directory);
57

58
	return true;
59 60
}

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

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

	return true;
}

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

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

110
	return true;
111 112
}

113
static bool
114 115
PrintSongFull(Response &r, Partition &partition,
	      bool base, const LightSong &song)
116
{
117
	song_print_info(r, partition, 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

	return true;
125 126
}

127
static bool
128
PrintPlaylistBrief(Response &r, bool base,
129
		   const PlaylistInfo &playlist,
130
		   const LightDirectory &directory)
131
{
132
	print_playlist_in_directory(r, base,
133
				    &directory, playlist.name.c_str());
134 135 136 137
	return true;
}

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

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

	return true;
}

151
void
152 153
db_selection_print(Response &r, Partition &partition,
		   const DatabaseSelection &selection,
154
		   bool full, bool base,
155
		   unsigned window_start, unsigned window_end)
156
{
157
	const Database &db = partition.GetDatabaseOrThrow();
158

159 160
	unsigned i = 0;

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

173 174
	if (window_start > 0 ||
	    window_end < (unsigned)std::numeric_limits<int>::max())
175
		s = [s, window_start, window_end, &i](const LightSong &song){
176 177
			const bool in_window = i >= window_start && i < window_end;
			++i;
178 179
			if (in_window)
				s(song);
180 181
		};

182
	db.Visit(selection, d, s, p);
183 184
}

185
void
186 187
db_selection_print(Response &r, Partition &partition,
		   const DatabaseSelection &selection,
188
		   bool full, bool base)
189
{
190 191
	db_selection_print(r, partition, selection, full, base,
			   0, std::numeric_limits<int>::max());
192 193
}

194
static bool
195
PrintSongURIVisitor(Response &r, Partition &partition, const LightSong &song)
196
{
197
	song_print_uri(r, partition, song);
198

199
	return true;
200 201
}

202
static bool
203
PrintUniqueTag(Response &r, TagType tag_type,
204
	       const Tag &tag)
205
{
206 207
	const char *value = tag.GetValue(tag_type);
	assert(value != nullptr);
208
	r.Format("%s: %s\n", tag_item_names[tag_type], value);
209

210
	for (const auto &item : tag)
211
		if (item.type != tag_type)
212 213
			r.Format("%s: %s\n",
				 tag_item_names[item.type], item.value);
214

215
	return true;
216 217
}

218
void
219
PrintUniqueTags(Response &r, Partition &partition,
220
		unsigned type, tag_mask_t group_mask,
221
		const SongFilter *filter)
222
{
223
	const Database &db = partition.GetDatabaseOrThrow();
224

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

227 228
	if (type == LOCATE_TAG_FILE_TYPE) {
		using namespace std::placeholders;
229
		const auto f = std::bind(PrintSongURIVisitor,
230
					 std::ref(r), std::ref(partition), _1);
231
		db.Visit(selection, f);
232
	} else {
233 234
		assert(type < TAG_NUM_OF_ITEM_TYPES);

235
		using namespace std::placeholders;
236
		const auto f = std::bind(PrintUniqueTag, std::ref(r),
237
					 (TagType)type, _1);
238 239
		db.VisitUniqueTags(selection, (TagType)type,
				   group_mask, f);
240
	}
241
}