DatabasePrint.cxx 6.1 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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
#include "tag/Tag.hxx"
28
#include "LightSong.hxx"
29 30
#include "LightDirectory.hxx"
#include "PlaylistInfo.hxx"
31
#include "DatabasePlugin.hxx"
32

33
#include <functional>
34

35
static bool
36
PrintDirectoryBrief(Client &client, const LightDirectory &directory)
37
{
38 39
	if (!directory.IsRoot())
		client_printf(client, "directory: %s\n", directory.GetPath());
40

41
	return true;
42 43
}

44
static bool
45
PrintDirectoryFull(Client &client, const LightDirectory &directory)
46 47 48
{
	if (!directory.IsRoot()) {
		client_printf(client, "directory: %s\n", directory.GetPath());
49 50 51

		if (directory.mtime > 0)
			time_print(client, "Last-Modified", directory.mtime);
52 53 54 55 56
	}

	return true;
}

57 58 59 60 61 62 63 64 65 66 67
static void
print_playlist_in_directory(Client &client,
			    const char *directory,
			    const char *name_utf8)
{
	if (directory == nullptr)
		client_printf(client, "playlist: %s\n", name_utf8);
	else
		client_printf(client, "playlist: %s/%s\n",
			      directory, name_utf8);
}
68

69
static void
70
print_playlist_in_directory(Client &client,
71
			    const LightDirectory *directory,
72 73
			    const char *name_utf8)
{
74
	if (directory == nullptr || directory->IsRoot())
75 76 77
		client_printf(client, "playlist: %s\n", name_utf8);
	else
		client_printf(client, "playlist: %s/%s\n",
78
			      directory->GetPath(), name_utf8);
79 80
}

81
static bool
82
PrintSongBrief(Client &client, const LightSong &song)
83
{
84
	song_print_uri(client, song);
85

86
	if (song.tag->has_playlist)
87
		/* this song file has an embedded CUE sheet */
88
		print_playlist_in_directory(client, song.directory, song.uri);
89

90
	return true;
91 92
}

93
static bool
94
PrintSongFull(Client &client, const LightSong &song)
95
{
96
	song_print_info(client, song);
97

98
	if (song.tag->has_playlist)
99
		/* this song file has an embedded CUE sheet */
100
		print_playlist_in_directory(client, song.directory, song.uri);
101 102

	return true;
103 104
}

105
static bool
106
PrintPlaylistBrief(Client &client,
107
		   const PlaylistInfo &playlist,
108
		   const LightDirectory &directory)
109
{
110
	print_playlist_in_directory(client, &directory, playlist.name.c_str());
111 112 113 114
	return true;
}

static bool
115
PrintPlaylistFull(Client &client,
116
		  const PlaylistInfo &playlist,
117
		  const LightDirectory &directory)
118
{
119
	print_playlist_in_directory(client, &directory, playlist.name.c_str());
120

121 122
	if (playlist.mtime > 0)
		time_print(client, "Last-Modified", playlist.mtime);
123 124 125 126

	return true;
}

127
bool
128
db_selection_print(Client &client, const DatabaseSelection &selection,
129
		   bool full, Error &error)
130
{
131
	const Database *db = client.GetDatabase(error);
132 133 134
	if (db == nullptr)
		return false;

135
	using namespace std::placeholders;
136
	const auto d = selection.filter == nullptr
137
		? std::bind(full ? PrintDirectoryFull : PrintDirectoryBrief,
138
			    std::ref(client), _1)
139
		: VisitDirectory();
140
	const auto s = std::bind(full ? PrintSongFull : PrintSongBrief,
141
				 std::ref(client), _1);
142
	const auto p = selection.filter == nullptr
143
		? std::bind(full ? PrintPlaylistFull : PrintPlaylistBrief,
144
			    std::ref(client), _1, _2)
145
		: VisitPlaylist();
146

147
	return db->Visit(selection, d, s, p, error);
148 149
}

150 151 152 153 154
struct SearchStats {
	int numberOfSongs;
	unsigned long playTime;
};

155
static void printSearchStats(Client &client, SearchStats *stats)
156 157 158 159 160
{
	client_printf(client, "songs: %i\n", stats->numberOfSongs);
	client_printf(client, "playtime: %li\n", stats->playTime);
}

161
static bool
162
stats_visitor_song(SearchStats &stats, const LightSong &song)
163
{
164
	stats.numberOfSongs++;
165
	stats.playTime += song.GetDuration();
166

167
	return true;
168 169
}

170
bool
171
searchStatsForSongsIn(Client &client, const char *name,
172
		      const SongFilter *filter,
173
		      Error &error)
174
{
175
	const Database *db = client.GetDatabase(error);
176 177 178
	if (db == nullptr)
		return false;

179
	const DatabaseSelection selection(name, true, filter);
180

181
	SearchStats stats;
182 183 184
	stats.numberOfSongs = 0;
	stats.playTime = 0;

185
	using namespace std::placeholders;
186
	const auto f = std::bind(stats_visitor_song, std::ref(stats),
187
				 _1);
188
	if (!db->Visit(selection, f, error))
189
		return false;
190

191 192
	printSearchStats(client, &stats);
	return true;
193 194
}

195
bool
196
printAllIn(Client &client, const char *uri_utf8, Error &error)
197
{
198
	const DatabaseSelection selection(uri_utf8, true);
199
	return db_selection_print(client, selection, false, error);
200 201
}

202
bool
203
printInfoForAllIn(Client &client, const char *uri_utf8,
204
		  Error &error)
205
{
206
	const DatabaseSelection selection(uri_utf8, true);
207
	return db_selection_print(client, selection, true, error);
208 209
}

210
static bool
211
PrintSongURIVisitor(Client &client, const LightSong &song)
212
{
213
	song_print_uri(client, song);
214

215
	return true;
216 217
}

218
static bool
219
PrintUniqueTag(Client &client, TagType tag_type,
220
	       const char *value)
221
{
222
	client_printf(client, "%s: %s\n", tag_item_names[tag_type], value);
223
	return true;
224 225
}

226
bool
227
listAllUniqueTags(Client &client, int type,
228
		  const SongFilter *filter,
229
		  Error &error)
230
{
231
	const Database *db = client.GetDatabase(error);
232 233 234
	if (db == nullptr)
		return false;

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

237 238
	if (type == LOCATE_TAG_FILE_TYPE) {
		using namespace std::placeholders;
239 240
		const auto f = std::bind(PrintSongURIVisitor,
					 std::ref(client), _1);
241
		return db->Visit(selection, f, error);
242 243
	} else {
		using namespace std::placeholders;
244
		const auto f = std::bind(PrintUniqueTag, std::ref(client),
245 246
					 (TagType)type, _1);
		return db->VisitUniqueTags(selection, (TagType)type,
247
					   f, error);
248
	}
249
}