aboutsummaryrefslogtreecommitdiffstats
path: root/src/command/PlaylistCommands.cxx
blob: 593eab8657bf07804859a717459833c10a743ebd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
 * Copyright (C) 2003-2014 The Music Player Daemon Project
 * 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"
#include "db/DatabasePlaylist.hxx"
#include "CommandError.hxx"
#include "PlaylistPrint.hxx"
#include "PlaylistSave.hxx"
#include "PlaylistFile.hxx"
#include "db/PlaylistVector.hxx"
#include "SongLoader.hxx"
#include "BulkEdit.hxx"
#include "playlist/PlaylistQueue.hxx"
#include "playlist/Print.hxx"
#include "queue/Playlist.hxx"
#include "TimePrint.hxx"
#include "client/Client.hxx"
#include "protocol/ArgParser.hxx"
#include "protocol/Result.hxx"
#include "ls.hxx"
#include "Mapper.hxx"
#include "fs/AllocatedPath.hxx"
#include "util/UriUtil.hxx"
#include "util/Error.hxx"

bool
playlist_commands_available()
{
	return !map_spl_path().IsNull();
}

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

		if (i.mtime > 0)
			time_print(client, "Last-Modified", i.mtime);
	}
}

CommandResult
handle_save(Client &client, gcc_unused unsigned argc, char *argv[])
{
	PlaylistResult result = spl_save_playlist(argv[1], client.playlist);
	return print_playlist_result(client, result);
}

CommandResult
handle_load(Client &client, unsigned argc, char *argv[])
{
	unsigned start_index, end_index;

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

	const ScopeBulkEdit bulk_edit(client.partition);

	Error error;
	const SongLoader loader(client);
	if (!playlist_open_into_queue(argv[1],
				      start_index, end_index,
				      client.playlist,
				      client.player_control, loader, error))
		return print_error(client, error);

	return CommandResult::OK;
}

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

	Error error;
	return spl_print(client, argv[1], false, error)
		? CommandResult::OK
		: print_error(client, error);
}

CommandResult
handle_listplaylistinfo(Client &client,
			gcc_unused unsigned argc, char *argv[])
{
	if (playlist_file_print(client, argv[1], true))
		return CommandResult::OK;

	Error error;
	return spl_print(client, argv[1], true, error)
		? CommandResult::OK
		: print_error(client, error);
}

CommandResult
handle_rm(Client &client, gcc_unused unsigned argc, char *argv[])
{
	Error error;
	return spl_delete(argv[1], error)
		? CommandResult::OK
		: print_error(client, error);
}

CommandResult
handle_rename(Client &client, gcc_unused unsigned argc, char *argv[])
{
	Error error;
	return spl_rename(argv[1], argv[2], error)
		? CommandResult::OK
		: print_error(client, error);
}

CommandResult
handle_playlistdelete(Client &client,
		      gcc_unused unsigned argc, char *argv[]) {
	char *playlist = argv[1];
	unsigned from;

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

	Error error;
	return spl_remove_index(playlist, from, error)
		? CommandResult::OK
		: print_error(client, error);
}

CommandResult
handle_playlistmove(Client &client, gcc_unused unsigned argc, char *argv[])
{
	char *playlist = argv[1];
	unsigned from, to;

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

	Error error;
	return spl_move_index(playlist, from, to, error)
		? CommandResult::OK
		: print_error(client, error);
}

CommandResult
handle_playlistclear(Client &client, gcc_unused unsigned argc, char *argv[])
{
	Error error;
	return spl_clear(argv[1], error)
		? CommandResult::OK
		: print_error(client, error);
}

CommandResult
handle_playlistadd(Client &client, gcc_unused unsigned argc, char *argv[])
{
	char *playlist = argv[1];
	char *uri = argv[2];

	bool success;
	Error error;
	if (uri_has_scheme(uri)) {
		const SongLoader loader(client);
		success = spl_append_uri(playlist, loader, uri, error);
	} else {
#ifdef ENABLE_DATABASE
		const Database *db = client.GetDatabase(error);
		if (db == nullptr)
			return print_error(client, error);

		success = search_add_to_playlist(*db, *client.GetStorage(),
						 uri, playlist, nullptr,
						 error);
#else
		success = false;
#endif
	}

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

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

CommandResult
handle_listplaylists(Client &client,
		     gcc_unused unsigned argc, gcc_unused char *argv[])
{
	Error error;
	const auto list = ListPlaylistFiles(error);
	if (list.empty() && error.IsDefined())
		return print_error(client, error);

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