PlaylistCommands.cxx 5.76 KB
Newer Older
1
/*
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 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 "DatabasePlaylist.hxx"
23
#include "CommandError.hxx"
24
#include "PlaylistPrint.hxx"
25
#include "PlaylistSave.hxx"
Max Kellermann's avatar
Max Kellermann committed
26
#include "PlaylistFile.hxx"
27
#include "PlaylistVector.hxx"
28
#include "PlaylistQueue.hxx"
Max Kellermann's avatar
Max Kellermann committed
29
#include "TimePrint.hxx"
Max Kellermann's avatar
Max Kellermann committed
30 31 32
#include "ClientInternal.hxx"
#include "protocol/ArgParser.hxx"
#include "protocol/Result.hxx"
Max Kellermann's avatar
Max Kellermann committed
33
#include "ls.hxx"
34
#include "Playlist.hxx"
Max Kellermann's avatar
Max Kellermann committed
35
#include "util/UriUtil.hxx"
36
#include "util/Error.hxx"
37 38 39 40 41

#include <assert.h>
#include <stdlib.h>

static void
42
print_spl_list(Client *client, const PlaylistVector &list)
43
{
44 45
	for (const auto &i : list) {
		client_printf(client, "playlist: %s\n", i.name.c_str());
46

47 48
		if (i.mtime > 0)
			time_print(client, "Last-Modified", i.mtime);
49 50 51 52
	}
}

enum command_return
53
handle_save(Client *client, gcc_unused int argc, char *argv[])
54 55 56
{
	enum playlist_result result;

57
	result = spl_save_playlist(argv[1], &client->playlist);
58 59 60 61
	return print_playlist_result(client, result);
}

enum command_return
62
handle_load(Client *client, int argc, char *argv[])
63 64 65 66 67
{
	unsigned start_index, end_index;

	if (argc < 3) {
		start_index = 0;
68
		end_index = unsigned(-1);
69 70 71 72 73 74 75
	} else if (!check_range(client, &start_index, &end_index, argv[2]))
		return COMMAND_RETURN_ERROR;

	enum playlist_result result;

	result = playlist_open_into_queue(argv[1],
					  start_index, end_index,
76
					  &client->playlist,
77 78 79 80
					  client->player_control, true);
	if (result != PLAYLIST_RESULT_NO_SUCH_LIST)
		return print_playlist_result(client, result);

81
	Error error;
82
	if (playlist_load_spl(&client->playlist, client->player_control,
83
			      argv[1], start_index, end_index,
84
			      error))
85 86
		return COMMAND_RETURN_OK;

87 88
	if (error.IsDomain(playlist_domain) &&
	    error.GetCode() == PLAYLIST_RESULT_BAD_NAME) {
89 90
		/* the message for BAD_NAME is confusing when the
		   client wants to load a playlist file from the music
91
		   directory; patch the Error object to show "no such
92
		   playlist" instead */
93 94 95 96
		Error error2(playlist_domain, PLAYLIST_RESULT_NO_SUCH_LIST,
			     error.GetMessage());
		error = std::move(error2);
	}
97 98 99 100 101

	return print_error(client, error);
}

enum command_return
102
handle_listplaylist(Client *client, gcc_unused int argc, char *argv[])
103 104 105 106
{
	if (playlist_file_print(client, argv[1], false))
		return COMMAND_RETURN_OK;

107 108
	Error error;
	return spl_print(client, argv[1], false, error)
109 110 111 112 113
		? COMMAND_RETURN_OK
		: print_error(client, error);
}

enum command_return
114
handle_listplaylistinfo(Client *client,
115
			gcc_unused int argc, char *argv[])
116 117 118 119
{
	if (playlist_file_print(client, argv[1], true))
		return COMMAND_RETURN_OK;

120 121
	Error error;
	return spl_print(client, argv[1], true, error)
122 123 124 125 126
		? COMMAND_RETURN_OK
		: print_error(client, error);
}

enum command_return
127
handle_rm(Client *client, gcc_unused int argc, char *argv[])
128
{
129 130
	Error error;
	return spl_delete(argv[1], error)
131 132 133 134 135
		? COMMAND_RETURN_OK
		: print_error(client, error);
}

enum command_return
136
handle_rename(Client *client, gcc_unused int argc, char *argv[])
137
{
138 139
	Error error;
	return spl_rename(argv[1], argv[2], error)
140 141 142 143 144
		? COMMAND_RETURN_OK
		: print_error(client, error);
}

enum command_return
145
handle_playlistdelete(Client *client,
146
		      gcc_unused int argc, char *argv[]) {
147 148 149 150 151 152
	char *playlist = argv[1];
	unsigned from;

	if (!check_unsigned(client, &from, argv[2]))
		return COMMAND_RETURN_ERROR;

153 154
	Error error;
	return spl_remove_index(playlist, from, error)
155 156 157 158 159
		? COMMAND_RETURN_OK
		: print_error(client, error);
}

enum command_return
160
handle_playlistmove(Client *client, gcc_unused int argc, char *argv[])
161 162 163 164 165 166 167 168 169
{
	char *playlist = argv[1];
	unsigned from, to;

	if (!check_unsigned(client, &from, argv[2]))
		return COMMAND_RETURN_ERROR;
	if (!check_unsigned(client, &to, argv[3]))
		return COMMAND_RETURN_ERROR;

170 171
	Error error;
	return spl_move_index(playlist, from, to, error)
172 173 174 175 176
		? COMMAND_RETURN_OK
		: print_error(client, error);
}

enum command_return
177
handle_playlistclear(Client *client, gcc_unused int argc, char *argv[])
178
{
179 180
	Error error;
	return spl_clear(argv[1], error)
181 182 183 184 185
		? COMMAND_RETURN_OK
		: print_error(client, error);
}

enum command_return
186
handle_playlistadd(Client *client, gcc_unused int argc, char *argv[])
187 188 189 190 191
{
	char *playlist = argv[1];
	char *uri = argv[2];

	bool success;
192
	Error error;
193 194 195 196 197 198 199
	if (uri_has_scheme(uri)) {
		if (!uri_supported_scheme(uri)) {
			command_error(client, ACK_ERROR_NO_EXIST,
				      "unsupported URI scheme");
			return COMMAND_RETURN_ERROR;
		}

200
		success = spl_append_uri(uri, playlist, error);
201
	} else
202
		success = search_add_to_playlist(uri, playlist, nullptr,
203
						 error);
204

205
	if (!success && !error.IsDefined()) {
206 207 208 209 210 211 212 213 214
		command_error(client, ACK_ERROR_NO_EXIST,
			      "directory or file not found");
		return COMMAND_RETURN_ERROR;
	}

	return success ? COMMAND_RETURN_OK : print_error(client, error);
}

enum command_return
215
handle_listplaylists(Client *client,
216
		     gcc_unused int argc, gcc_unused char *argv[])
217
{
218 219 220
	Error error;
	const auto list = ListPlaylistFiles(error);
	if (list.empty() && error.IsDefined())
221 222 223 224 225
		return print_error(client, error);

	print_spl_list(client, list);
	return COMMAND_RETURN_OK;
}