DatabasePrint.cxx 6.22 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
bool
152 153
db_selection_print(Response &r, Partition &partition,
		   const DatabaseSelection &selection,
154 155 156
		   bool full, bool base,
		   unsigned window_start, unsigned window_end,
		   Error &error)
157
{
158
	const Database *db = partition.GetDatabase(error);
159 160 161
	if (db == nullptr)
		return false;

162 163
	unsigned i = 0;

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

176 177
	if (window_start > 0 ||
	    window_end < (unsigned)std::numeric_limits<int>::max())
178 179 180 181 182 183 184
		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);
		};

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

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

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

204
	return true;
205 206
}

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

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

220
	return true;
221 222
}

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

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

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

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