PlaylistPrint.cxx 3.63 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
 * 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.
14 15 16 17
 *
 * 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.
18 19
 */

20
#include "config.h"
21
#include "PlaylistPrint.hxx"
Max Kellermann's avatar
Max Kellermann committed
22
#include "PlaylistFile.hxx"
23
#include "queue/Playlist.hxx"
Max Kellermann's avatar
Max Kellermann committed
24
#include "queue/QueuePrint.hxx"
Max Kellermann's avatar
Max Kellermann committed
25
#include "SongPrint.hxx"
26 27
#include "Partition.hxx"
#include "Instance.hxx"
28
#include "db/Interface.hxx"
29
#include "client/Client.hxx"
Max Kellermann's avatar
Max Kellermann committed
30
#include "input/InputStream.hxx"
31
#include "DetachedSong.hxx"
Max Kellermann's avatar
Max Kellermann committed
32
#include "fs/Traits.hxx"
33
#include "util/Error.hxx"
34
#include "thread/Cond.hxx"
35

36 37 38
#define SONG_FILE "file: "
#define SONG_TIME "Time: "

39
void
40
playlist_print_uris(Client &client, const playlist &playlist)
41
{
42
	const Queue &queue = playlist.queue;
43

44
	queue_print_uris(client, queue, 0, queue.GetLength());
45 46 47
}

bool
48
playlist_print_info(Client &client, const playlist &playlist,
49 50
		    unsigned start, unsigned end)
{
51
	const Queue &queue = playlist.queue;
52

53
	if (end > queue.GetLength())
54
		/* correct the "end" offset */
55
		end = queue.GetLength();
56 57 58 59 60 61 62 63 64 65

	if (start > end)
		/* an invalid "start" offset is fatal */
		return false;

	queue_print_info(client, queue, start, end);
	return true;
}

bool
66
playlist_print_id(Client &client, const playlist &playlist,
67 68 69 70
		  unsigned id)
{
	int position;

71
	position = playlist.queue.IdToPosition(id);
72 73 74 75 76 77 78 79
	if (position < 0)
		/* no such song */
		return false;

	return playlist_print_info(client, playlist, position, position + 1);
}

bool
80
playlist_print_current(Client &client, const playlist &playlist)
81
{
82
	int current_position = playlist.GetCurrentPosition();
83 84 85
	if (current_position < 0)
		return false;

86
	queue_print_info(client, playlist.queue,
87 88 89 90 91
			 current_position, current_position + 1);
	return true;
}

void
92
playlist_print_find(Client &client, const playlist &playlist,
93
		    const SongFilter &filter)
94
{
95
	queue_find(client, playlist.queue, filter);
96 97 98
}

void
99 100
playlist_print_changes_info(Client &client,
			    const playlist &playlist,
101 102
			    uint32_t version)
{
103
	queue_print_changes_info(client, playlist.queue, version);
104 105 106
}

void
107 108
playlist_print_changes_position(Client &client,
				const playlist &playlist,
109 110
				uint32_t version)
{
111
	queue_print_changes_position(client, playlist.queue, version);
112 113
}

114 115
#ifdef ENABLE_DATABASE

116
static bool
117
PrintSongDetails(Client &client, const char *uri_utf8)
118
{
119
	const Database *db = client.partition.instance.database;
120 121 122
	if (db == nullptr)
		return false;

123
	auto *song = db->GetSong(uri_utf8, IgnoreError());
124 125 126
	if (song == nullptr)
		return false;

127
	song_print_info(client, *song);
128 129 130 131
	db->ReturnSong(song);
	return true;
}

132 133
#endif

134
bool
135
spl_print(Client &client, const char *name_utf8, bool detail,
136
	  Error &error)
137
{
138 139 140 141
#ifndef ENABLE_DATABASE
	(void)detail;
#endif

142 143
	PlaylistFileContents contents = LoadPlaylistFile(name_utf8, error);
	if (contents.empty() && error.IsDefined())
144
		return false;
145

146
	for (const auto &uri_utf8 : contents) {
147
#ifdef ENABLE_DATABASE
148
		if (!detail || !PrintSongDetails(client, uri_utf8.c_str()))
149
#endif
150 151
			client_printf(client, SONG_FILE "%s\n",
				      uri_utf8.c_str());
152 153
	}

154
	return true;
155
}