PlaylistCommands.cxx 4.58 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 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"
#include "PlaylistCommands.hxx"
22
#include "Request.hxx"
Max Kellermann's avatar
Max Kellermann committed
23
#include "db/DatabasePlaylist.hxx"
24
#include "CommandError.hxx"
25
#include "PlaylistSave.hxx"
Max Kellermann's avatar
Max Kellermann committed
26
#include "PlaylistFile.hxx"
27
#include "PlaylistError.hxx"
28
#include "db/PlaylistVector.hxx"
29
#include "SongLoader.hxx"
30
#include "BulkEdit.hxx"
31 32
#include "playlist/PlaylistQueue.hxx"
#include "playlist/Print.hxx"
Max Kellermann's avatar
Max Kellermann committed
33
#include "TimePrint.hxx"
34
#include "client/Client.hxx"
35
#include "client/Response.hxx"
36 37
#include "Mapper.hxx"
#include "fs/AllocatedPath.hxx"
Max Kellermann's avatar
Max Kellermann committed
38
#include "util/UriUtil.hxx"
39
#include "util/ConstBuffer.hxx"
40

41
bool
42
playlist_commands_available() noexcept
43 44 45 46
{
	return !map_spl_path().IsNull();
}

47
static void
48
print_spl_list(Response &r, const PlaylistVector &list)
49
{
50
	for (const auto &i : list) {
51
		r.Format("playlist: %s\n", i.name.c_str());
52

53
		if (i.mtime > 0)
54
			time_print(r, "Last-Modified", i.mtime);
55 56 57
	}
}

58
CommandResult
59
handle_save(Client &client, Request args, gcc_unused Response &r)
60
{
61 62
	spl_save_playlist(args.front(), client.playlist);
	return CommandResult::OK;
63 64
}

65
CommandResult
66
handle_load(Client &client, Request args, gcc_unused Response &r)
67
{
68
	RangeArg range = args.ParseOptional(1, RangeArg::All());
69

70 71
	const ScopeBulkEdit bulk_edit(client.partition);

72
	const SongLoader loader(client);
73 74 75 76
	playlist_open_into_queue(args.front(),
				 range.start, range.end,
				 client.playlist,
				 client.player_control, loader);
77
	return CommandResult::OK;
78 79
}

80
CommandResult
81
handle_listplaylist(Client &client, Request args, Response &r)
82
{
83 84
	const char *const name = args.front();

85
	if (playlist_file_print(r, client.partition, SongLoader(client),
86
				 name, false))
87
		return CommandResult::OK;
88

89
	throw PlaylistError::NoSuchList();
90 91
}

92
CommandResult
93
handle_listplaylistinfo(Client &client, Request args, Response &r)
94
{
95 96
	const char *const name = args.front();

97 98
	if (playlist_file_print(r, client.partition, SongLoader(client),
				name, true))
99
		return CommandResult::OK;
100

101
	throw PlaylistError::NoSuchList();
102 103
}

104
CommandResult
105
handle_rm(gcc_unused Client &client, Request args, gcc_unused Response &r)
106
{
107 108
	const char *const name = args.front();

109 110
	spl_delete(name);
	return CommandResult::OK;
111 112
}

113
CommandResult
114
handle_rename(gcc_unused Client &client, Request args, gcc_unused Response &r)
115
{
116 117 118
	const char *const old_name = args[0];
	const char *const new_name = args[1];

119 120
	spl_rename(old_name, new_name);
	return CommandResult::OK;
121 122
}

123
CommandResult
124 125
handle_playlistdelete(gcc_unused Client &client,
		      Request args, gcc_unused Response &r)
126 127
{
	const char *const name = args[0];
128
	unsigned from = args.ParseUnsigned(1);
129

130 131
	spl_remove_index(name, from);
	return CommandResult::OK;
132 133
}

134
CommandResult
135 136
handle_playlistmove(gcc_unused Client &client,
		    Request args, gcc_unused Response &r)
137
{
138
	const char *const name = args.front();
139 140
	unsigned from = args.ParseUnsigned(1);
	unsigned to = args.ParseUnsigned(2);
141

142 143
	spl_move_index(name, from, to);
	return CommandResult::OK;
144 145
}

146
CommandResult
147 148
handle_playlistclear(gcc_unused Client &client,
		     Request args, gcc_unused Response &r)
149
{
150 151
	const char *const name = args.front();

152 153
	spl_clear(name);
	return CommandResult::OK;
154 155
}

156
CommandResult
157
handle_playlistadd(Client &client, Request args, gcc_unused Response &r)
158
{
159 160
	const char *const playlist = args[0];
	const char *const uri = args[1];
161 162

	if (uri_has_scheme(uri)) {
163
		const SongLoader loader(client);
164
		spl_append_uri(playlist, loader, uri);
165 166
	} else {
#ifdef ENABLE_DATABASE
167
		const Database &db = client.GetDatabaseOrThrow();
168

169 170
		search_add_to_playlist(db, *client.GetStorage(),
				       uri, playlist, nullptr);
171
#else
172
		r.Error(ACK_ERROR_NO_EXIST, "directory or file not found");
173
		return CommandResult::ERROR;
174
#endif
175 176
	}

177
	return CommandResult::OK;
178 179
}

180
CommandResult
181 182
handle_listplaylists(gcc_unused Client &client, gcc_unused Request args,
		     Response &r)
183
{
184
	print_spl_list(r, ListPlaylistFiles());
185
	return CommandResult::OK;
186
}