DatabasePrint.cxx 5.52 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2013 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"
22
#include "DatabaseSelection.hxx"
23
#include "SongFilter.hxx"
24
#include "PlaylistVector.hxx"
Max Kellermann's avatar
Max Kellermann committed
25 26
#include "SongPrint.hxx"
#include "TimePrint.hxx"
27
#include "Directory.hxx"
Max Kellermann's avatar
Max Kellermann committed
28
#include "Client.hxx"
29
#include "tag.h"
30 31

extern "C" {
32
#include "song.h"
33
}
34

35 36
#include "DatabaseGlue.hxx"
#include "DatabasePlugin.hxx"
37

38
#include <functional>
39

40
static bool
41
PrintDirectory(Client *client, const Directory &directory)
42
{
43 44
	if (!directory.IsRoot())
		client_printf(client, "directory: %s\n", directory.GetPath());
45

46
	return true;
47 48
}

49
static void
50
print_playlist_in_directory(Client *client,
51
			    const Directory &directory,
52 53
			    const char *name_utf8)
{
54
	if (directory.IsRoot())
55 56 57
		client_printf(client, "playlist: %s\n", name_utf8);
	else
		client_printf(client, "playlist: %s/%s\n",
58
			      directory.GetPath(), name_utf8);
59 60
}

61
static bool
62
PrintSongBrief(Client *client, song &song)
63
{
64
	assert(song.parent != NULL);
65

66
	song_print_uri(client, &song);
67

68
	if (song.tag != NULL && song.tag->has_playlist)
69
		/* this song file has an embedded CUE sheet */
70
		print_playlist_in_directory(client, *song.parent, song.uri);
71

72
	return true;
73 74
}

75
static bool
76
PrintSongFull(Client *client, song &song)
77
{
78
	assert(song.parent != NULL);
79

80
	song_print_info(client, &song);
81

82
	if (song.tag != NULL && song.tag->has_playlist)
83
		/* this song file has an embedded CUE sheet */
84
		print_playlist_in_directory(client, *song.parent, song.uri);
85 86

	return true;
87 88
}

89
static bool
90
PrintPlaylistBrief(Client *client,
91
		   const PlaylistInfo &playlist,
92
		   const Directory &directory)
93
{
94
	print_playlist_in_directory(client, directory, playlist.name.c_str());
95 96 97 98
	return true;
}

static bool
99
PrintPlaylistFull(Client *client,
100
		  const PlaylistInfo &playlist,
101
		  const Directory &directory)
102
{
103
	print_playlist_in_directory(client, directory, playlist.name.c_str());
104

105 106
	if (playlist.mtime > 0)
		time_print(client, "Last-Modified", playlist.mtime);
107 108 109 110

	return true;
}

111
bool
112
db_selection_print(Client *client, const DatabaseSelection &selection,
113 114
		   bool full, GError **error_r)
{
115 116 117 118
	const Database *db = GetDatabase(error_r);
	if (db == nullptr)
		return false;

119
	using namespace std::placeholders;
120
	const auto d = selection.filter == nullptr
121 122
		? std::bind(PrintDirectory, client, _1)
		: VisitDirectory();
123 124
	const auto s = std::bind(full ? PrintSongFull : PrintSongBrief,
				 client, _1);
125
	const auto p = selection.filter == nullptr
126 127 128
		? std::bind(full ? PrintPlaylistFull : PrintPlaylistBrief,
			    client, _1, _2)
		: VisitPlaylist();
129

130
	return db->Visit(selection, d, s, p, error_r);
131 132
}

133 134 135 136 137
struct SearchStats {
	int numberOfSongs;
	unsigned long playTime;
};

138
static void printSearchStats(Client *client, SearchStats *stats)
139 140 141 142 143
{
	client_printf(client, "songs: %i\n", stats->numberOfSongs);
	client_printf(client, "playtime: %li\n", stats->playTime);
}

144
static bool
145
stats_visitor_song(SearchStats &stats, song &song)
146
{
147 148
	stats.numberOfSongs++;
	stats.playTime += song_get_duration(&song);
149

150
	return true;
151 152
}

153
bool
154
searchStatsForSongsIn(Client *client, const char *name,
155
		      const SongFilter *filter,
156
		      GError **error_r)
157
{
158 159 160 161
	const Database *db = GetDatabase(error_r);
	if (db == nullptr)
		return false;

162
	const DatabaseSelection selection(name, true, filter);
163

164
	SearchStats stats;
165 166 167
	stats.numberOfSongs = 0;
	stats.playTime = 0;

168
	using namespace std::placeholders;
169
	const auto f = std::bind(stats_visitor_song, std::ref(stats),
170
				 _1);
171
	if (!db->Visit(selection, f, error_r))
172
		return false;
173

174 175
	printSearchStats(client, &stats);
	return true;
176 177
}

178
bool
179
printAllIn(Client *client, const char *uri_utf8, GError **error_r)
180
{
181
	const DatabaseSelection selection(uri_utf8, true);
182
	return db_selection_print(client, selection, false, error_r);
183 184
}

185
bool
186
printInfoForAllIn(Client *client, const char *uri_utf8,
187
		  GError **error_r)
188
{
189
	const DatabaseSelection selection(uri_utf8, true);
190
	return db_selection_print(client, selection, true, error_r);
191 192
}

193
static bool
194
PrintSongURIVisitor(Client *client, song &song)
195
{
196
	song_print_uri(client, &song);
197

198
	return true;
199 200
}

201
static bool
202
PrintUniqueTag(Client *client, enum tag_type tag_type,
203
	       const char *value)
204
{
205
	client_printf(client, "%s: %s\n", tag_item_names[tag_type], value);
206
	return true;
207 208
}

209
bool
210
listAllUniqueTags(Client *client, int type,
211
		  const SongFilter *filter,
212
		  GError **error_r)
213
{
214 215 216 217
	const Database *db = GetDatabase(error_r);
	if (db == nullptr)
		return false;

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

220 221 222
	if (type == LOCATE_TAG_FILE_TYPE) {
		using namespace std::placeholders;
		const auto f = std::bind(PrintSongURIVisitor, client, _1);
223
		return db->Visit(selection, f, error_r);
224 225 226 227
	} else {
		using namespace std::placeholders;
		const auto f = std::bind(PrintUniqueTag, client,
					 (enum tag_type)type, _1);
228 229
		return db->VisitUniqueTags(selection, (enum tag_type)type,
					   f, error_r);
230
	}
231
}