PlaylistCommands.cxx 5.33 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 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"
Max Kellermann's avatar
Max Kellermann committed
22
#include "db/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 "db/PlaylistVector.hxx"
28
#include "SongLoader.hxx"
29
#include "BulkEdit.hxx"
30 31
#include "playlist/PlaylistQueue.hxx"
#include "playlist/Print.hxx"
32
#include "queue/Playlist.hxx"
Max Kellermann's avatar
Max Kellermann committed
33
#include "TimePrint.hxx"
34
#include "client/Client.hxx"
Max Kellermann's avatar
Max Kellermann committed
35 36
#include "protocol/ArgParser.hxx"
#include "protocol/Result.hxx"
Max Kellermann's avatar
Max Kellermann committed
37
#include "ls.hxx"
Max Kellermann's avatar
Max Kellermann committed
38
#include "util/UriUtil.hxx"
39
#include "util/Error.hxx"
40 41

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
CommandResult
53
handle_save(Client &client, gcc_unused unsigned argc, char *argv[])
54
{
55
	PlaylistResult result = spl_save_playlist(argv[1], client.playlist);
56 57 58
	return print_playlist_result(client, result);
}

59
CommandResult
60
handle_load(Client &client, unsigned argc, char *argv[])
61 62 63 64 65
{
	unsigned start_index, end_index;

	if (argc < 3) {
		start_index = 0;
66
		end_index = unsigned(-1);
67
	} else if (!check_range(client, &start_index, &end_index, argv[2]))
68
		return CommandResult::ERROR;
69

70 71
	const ScopeBulkEdit bulk_edit(client.partition);

72
	Error error;
73
	const SongLoader loader(client);
74 75 76 77 78
	if (!playlist_open_into_queue(argv[1],
				      start_index, end_index,
				      client.playlist,
				      client.player_control, loader, error))
		return print_error(client, error);
79

80
	return CommandResult::OK;
81 82
}

83
CommandResult
84
handle_listplaylist(Client &client, gcc_unused unsigned argc, char *argv[])
85 86
{
	if (playlist_file_print(client, argv[1], false))
87
		return CommandResult::OK;
88

89 90
	Error error;
	return spl_print(client, argv[1], false, error)
91
		? CommandResult::OK
92 93 94
		: print_error(client, error);
}

95
CommandResult
96
handle_listplaylistinfo(Client &client,
97
			gcc_unused unsigned argc, char *argv[])
98 99
{
	if (playlist_file_print(client, argv[1], true))
100
		return CommandResult::OK;
101

102 103
	Error error;
	return spl_print(client, argv[1], true, error)
104
		? CommandResult::OK
105 106 107
		: print_error(client, error);
}

108
CommandResult
109
handle_rm(Client &client, gcc_unused unsigned argc, char *argv[])
110
{
111 112
	Error error;
	return spl_delete(argv[1], error)
113
		? CommandResult::OK
114 115 116
		: print_error(client, error);
}

117
CommandResult
118
handle_rename(Client &client, gcc_unused unsigned argc, char *argv[])
119
{
120 121
	Error error;
	return spl_rename(argv[1], argv[2], error)
122
		? CommandResult::OK
123 124 125
		: print_error(client, error);
}

126
CommandResult
127
handle_playlistdelete(Client &client,
128
		      gcc_unused unsigned argc, char *argv[]) {
129 130 131 132
	char *playlist = argv[1];
	unsigned from;

	if (!check_unsigned(client, &from, argv[2]))
133
		return CommandResult::ERROR;
134

135 136
	Error error;
	return spl_remove_index(playlist, from, error)
137
		? CommandResult::OK
138 139 140
		: print_error(client, error);
}

141
CommandResult
142
handle_playlistmove(Client &client, gcc_unused unsigned argc, char *argv[])
143 144 145 146 147
{
	char *playlist = argv[1];
	unsigned from, to;

	if (!check_unsigned(client, &from, argv[2]))
148
		return CommandResult::ERROR;
149
	if (!check_unsigned(client, &to, argv[3]))
150
		return CommandResult::ERROR;
151

152 153
	Error error;
	return spl_move_index(playlist, from, to, error)
154
		? CommandResult::OK
155 156 157
		: print_error(client, error);
}

158
CommandResult
159
handle_playlistclear(Client &client, gcc_unused unsigned argc, char *argv[])
160
{
161 162
	Error error;
	return spl_clear(argv[1], error)
163
		? CommandResult::OK
164 165 166
		: print_error(client, error);
}

167
CommandResult
168
handle_playlistadd(Client &client, gcc_unused unsigned argc, char *argv[])
169 170 171 172 173
{
	char *playlist = argv[1];
	char *uri = argv[2];

	bool success;
174
	Error error;
175
	if (uri_has_scheme(uri)) {
176 177
		const SongLoader loader(client);
		success = spl_append_uri(playlist, loader, uri, error);
178 179
	} else {
#ifdef ENABLE_DATABASE
180
		const Database *db = client.GetDatabase(error);
181 182 183
		if (db == nullptr)
			return print_error(client, error);

184 185
		success = search_add_to_playlist(*db, *client.GetStorage(),
						 uri, playlist, nullptr,
186
						 error);
187 188 189 190
#else
		success = false;
#endif
	}
191

192
	if (!success && !error.IsDefined()) {
193 194
		command_error(client, ACK_ERROR_NO_EXIST,
			      "directory or file not found");
195
		return CommandResult::ERROR;
196 197
	}

198
	return success ? CommandResult::OK : print_error(client, error);
199 200
}

201
CommandResult
202
handle_listplaylists(Client &client,
203
		     gcc_unused unsigned argc, gcc_unused char *argv[])
204
{
205 206 207
	Error error;
	const auto list = ListPlaylistFiles(error);
	if (list.empty() && error.IsDefined())
208 209 210
		return print_error(client, error);

	print_spl_list(client, list);
211
	return CommandResult::OK;
212
}