DatabasePrint.cxx 5.83 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * 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.
 */

20
#include "DatabasePrint.hxx"
Max Kellermann's avatar
Max Kellermann committed
21
#include "Selection.hxx"
Max Kellermann's avatar
Max Kellermann committed
22 23
#include "SongPrint.hxx"
#include "TimePrint.hxx"
24
#include "TagPrint.hxx"
25
#include "client/Response.hxx"
26
#include "protocol/RangeArg.hxx"
27
#include "Partition.hxx"
28 29 30
#include "song/DetachedSong.hxx"
#include "song/Filter.hxx"
#include "song/LightSong.hxx"
31
#include "tag/Tag.hxx"
32
#include "tag/Mask.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
gcc_pure
42
static const char *
43
ApplyBaseFlag(const char *uri, bool base) noexcept
44 45 46 47 48 49
{
	if (base)
		uri = PathTraitsUTF8::GetBase(uri);
	return uri;
}

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

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

66
static void
67 68
PrintDirectoryFull(Response &r, bool base,
		   const LightDirectory &directory) noexcept
69 70
{
	if (!directory.IsRoot()) {
71
		PrintDirectoryURI(r, base, directory);
72

73
		if (!IsNegative(directory.mtime))
74
			time_print(r, "Last-Modified", directory.mtime);
75 76 77
	}
}

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

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

103
static void
104
PrintSongBrief(Response &r, bool base, const LightSong &song) noexcept
105
{
106
	song_print_uri(r, song, base);
107

108
	if (song.tag.has_playlist)
109
		/* this song file has an embedded CUE sheet */
110
		print_playlist_in_directory(r, base,
111
					    song.directory, song.uri);
112 113
}

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

125
static void
126
PrintPlaylistBrief(Response &r, bool base,
127
		   const PlaylistInfo &playlist,
128
		   const LightDirectory &directory) noexcept
129
{
130
	print_playlist_in_directory(r, base,
131
				    &directory, playlist.name.c_str());
132 133
}

134
static void
135
PrintPlaylistFull(Response &r, bool base,
136
		  const PlaylistInfo &playlist,
137
		  const LightDirectory &directory) noexcept
138
{
139
	print_playlist_in_directory(r, base,
140
				    &directory, playlist.name.c_str());
141

142
	if (!IsNegative(playlist.mtime))
143
		time_print(r, "Last-Modified", playlist.mtime);
144 145
}

146
void
147 148
db_selection_print(Response &r, Partition &partition,
		   const DatabaseSelection &selection,
149
		   bool full, bool base)
150
{
151
	const Database &db = partition.GetDatabaseOrThrow();
152

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

165
	db.Visit(selection, d, s, p);
166 167
}

168
static void
169
PrintSongURIVisitor(Response &r, const LightSong &song) noexcept
170
{
171
	song_print_uri(r, song);
172 173
}

174 175 176 177 178 179 180 181 182 183 184 185 186 187
void
PrintSongUris(Response &r, Partition &partition,
	      const SongFilter *filter)
{
	const Database &db = partition.GetDatabaseOrThrow();

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

	using namespace std::placeholders;
	const auto f = std::bind(PrintSongURIVisitor,
				 std::ref(r), _1);
	db.Visit(selection, f);
}

188
static void
189 190
PrintUniqueTags(Response &r, TagType tag_type,
		const std::set<std::string> &values)
191
{
192 193 194 195
	const char *const name = tag_item_names[tag_type];
	for (const auto &i : values)
		r.Format("%s: %s\n", name, i.c_str());
}
196

197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
static void
PrintGroupedUniqueTags(Response &r, TagType tag_type, TagType group,
		       const std::map<std::string, std::set<std::string>> &groups)
{
	if (group == TAG_NUM_OF_ITEM_TYPES) {
		for (const auto &i : groups)
			PrintUniqueTags(r, tag_type, i.second);
		return;
	}

	const char *const group_name = tag_item_names[group];
	for (const auto &i : groups) {
		r.Format("%s: %s\n", group_name, i.first.c_str());
		PrintUniqueTags(r, tag_type, i.second);
	}
212 213
}

214
void
215
PrintUniqueTags(Response &r, Partition &partition,
216
		TagType type, TagType group,
217
		const SongFilter *filter)
218
{
219 220
	assert(type < TAG_NUM_OF_ITEM_TYPES);

221
	const Database &db = partition.GetDatabaseOrThrow();
222

223
	const DatabaseSelection selection("", true, filter);
224

225 226
	PrintGroupedUniqueTags(r, type, group,
			       db.CollectUniqueTags(selection, type, group));
227
}