PlaylistPrint.cxx 3.86 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2015 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"
30
#include "client/Response.hxx"
Max Kellermann's avatar
Max Kellermann committed
31
#include "input/InputStream.hxx"
32
#include "DetachedSong.hxx"
Max Kellermann's avatar
Max Kellermann committed
33
#include "fs/Traits.hxx"
34
#include "util/Error.hxx"
35
#include "thread/Cond.hxx"
36

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

40
void
41 42
playlist_print_uris(Response &r, Partition &partition,
		    const playlist &playlist)
43
{
44
	const Queue &queue = playlist.queue;
45

46
	queue_print_uris(r, partition, queue, 0, queue.GetLength());
47 48 49
}

bool
50 51
playlist_print_info(Response &r, Partition &partition,
		    const playlist &playlist,
52 53
		    unsigned start, unsigned end)
{
54
	const Queue &queue = playlist.queue;
55

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

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

64
	queue_print_info(r, partition, queue, start, end);
65 66 67 68
	return true;
}

bool
69
playlist_print_id(Response &r, Partition &partition, const playlist &playlist,
70 71 72 73
		  unsigned id)
{
	int position;

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

79 80
	return playlist_print_info(r, partition,
				   playlist, position, position + 1);
81 82 83
}

bool
84 85
playlist_print_current(Response &r, Partition &partition,
		       const playlist &playlist)
86
{
87
	int current_position = playlist.GetCurrentPosition();
88 89 90
	if (current_position < 0)
		return false;

91
	queue_print_info(r, partition, playlist.queue,
92 93 94 95 96
			 current_position, current_position + 1);
	return true;
}

void
97 98
playlist_print_find(Response &r, Partition &partition,
		    const playlist &playlist,
99
		    const SongFilter &filter)
100
{
101
	queue_find(r, partition, playlist.queue, filter);
102 103 104
}

void
105
playlist_print_changes_info(Response &r, Partition &partition,
106
			    const playlist &playlist,
107 108
			    uint32_t version)
{
109
	queue_print_changes_info(r, partition, playlist.queue, version);
110 111 112
}

void
113
playlist_print_changes_position(Response &r,
114
				const playlist &playlist,
115 116
				uint32_t version)
{
117
	queue_print_changes_position(r, playlist.queue, version);
118 119
}

120 121
#ifdef ENABLE_DATABASE

122
static bool
123
PrintSongDetails(Response &r, Partition &partition, const char *uri_utf8)
124
{
125
	const Database *db = partition.instance.database;
126 127 128
	if (db == nullptr)
		return false;

129
	auto *song = db->GetSong(uri_utf8, IgnoreError());
130 131 132
	if (song == nullptr)
		return false;

133
	song_print_info(r, partition, *song);
134 135 136 137
	db->ReturnSong(song);
	return true;
}

138 139
#endif

140
bool
141 142
spl_print(Response &r, Partition &partition,
	  const char *name_utf8, bool detail,
143
	  Error &error)
144
{
145 146 147 148
#ifndef ENABLE_DATABASE
	(void)detail;
#endif

149 150
	PlaylistFileContents contents = LoadPlaylistFile(name_utf8, error);
	if (contents.empty() && error.IsDefined())
151
		return false;
152

153
	for (const auto &uri_utf8 : contents) {
154
#ifdef ENABLE_DATABASE
155 156
		if (!detail || !PrintSongDetails(r, partition,
						 uri_utf8.c_str()))
157
#endif
158
			r.Format(SONG_FILE "%s\n", uri_utf8.c_str());
159 160
	}

161
	return true;
162
}