aboutsummaryrefslogtreecommitdiffstats
path: root/src/PlaylistCommands.cxx
blob: d8a9b5fc91bdf6c9f5fba6edf4369ed72d33dce3 (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
221
222
223
224
/*
 * Copyright (C) 2003-2013 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 "DatabasePlaylist.hxx"
#include "CommandError.hxx"
#include "PlaylistPrint.hxx"
#include "PlaylistSave.hxx"
#include "PlaylistFile.hxx"
#include "PlaylistVector.hxx"
#include "PlaylistQueue.hxx"
#include "TimePrint.hxx"
#include "ClientInternal.hxx"
#include "protocol/ArgParser.hxx"
#include "protocol/Result.hxx"
#include "ls.hxx"
#include "Playlist.hxx"

extern "C" {
#include "uri.h"
}

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

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);
	}
}

enum command_return
handle_save(Client *client, G_GNUC_UNUSED int argc, char *argv[])
{
	enum playlist_result result;

	result = spl_save_playlist(argv[1], &g_playlist);
	return print_playlist_result(client, result);
}

enum command_return
handle_load(Client *client, int argc, char *argv[])
{
	unsigned start_index, end_index;

	if (argc < 3) {
		start_index = 0;
		end_index = G_MAXUINT;
	} 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,
					  &g_playlist,
					  client->player_control, true);
	if (result != PLAYLIST_RESULT_NO_SUCH_LIST)
		return print_playlist_result(client, result);

	GError *error = NULL;
	if (playlist_load_spl(&g_playlist, client->player_control,
			      argv[1], start_index, end_index,
			      &error))
		return COMMAND_RETURN_OK;

	if (error->domain == playlist_quark() &&
	    error->code == PLAYLIST_RESULT_BAD_NAME)
		/* the message for BAD_NAME is confusing when the
		   client wants to load a playlist file from the music
		   directory; patch the GError object to show "no such
		   playlist" instead */
		error->code = PLAYLIST_RESULT_NO_SUCH_LIST;

	return print_error(client, error);
}

enum command_return
handle_listplaylist(Client *client, G_GNUC_UNUSED int argc, char *argv[])
{
	if (playlist_file_print(client, argv[1], false))
		return COMMAND_RETURN_OK;

	GError *error = NULL;
	return spl_print(client, argv[1], false, &error)
		? COMMAND_RETURN_OK
		: print_error(client, error);
}

enum command_return
handle_listplaylistinfo(Client *client,
			G_GNUC_UNUSED int argc, char *argv[])
{
	if (playlist_file_print(client, argv[1], true))
		return COMMAND_RETURN_OK;

	GError *error = NULL;
	return spl_print(client, argv[1], true, &error)
		? COMMAND_RETURN_OK
		: print_error(client, error);
}

enum command_return
handle_rm(Client *client, G_GNUC_UNUSED int argc, char *argv[])
{
	GError *error = NULL;
	return spl_delete(argv[1], &error)
		? COMMAND_RETURN_OK
		: print_error(client, error);
}

enum command_return
handle_rename(Client *client, G_GNUC_UNUSED int argc, char *argv[])
{
	GError *error = NULL;
	return spl_rename(argv[1], argv[2], &error)
		? COMMAND_RETURN_OK
		: print_error(client, error);
}

enum command_return
handle_playlistdelete(Client *client,
		      G_GNUC_UNUSED int argc, char *argv[]) {
	char *playlist = argv[1];
	unsigned from;

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

	GError *error = NULL;
	return spl_remove_index(playlist, from, &error)
		? COMMAND_RETURN_OK
		: print_error(client, error);
}

enum command_return
handle_playlistmove(Client *client, G_GNUC_UNUSED int argc, char *argv[])
{
	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;

	GError *error = NULL;
	return spl_move_index(playlist, from, to, &error)
		? COMMAND_RETURN_OK
		: print_error(client, error);
}

enum command_return
handle_playlistclear(Client *client, G_GNUC_UNUSED int argc, char *argv[])
{
	GError *error = NULL;
	return spl_clear(argv[1], &error)
		? COMMAND_RETURN_OK
		: print_error(client, error);
}

enum command_return
handle_playlistadd(Client *client, G_GNUC_UNUSED int argc, char *argv[])
{
	char *playlist = argv[1];
	char *uri = argv[2];

	bool success;
	GError *error = NULL;
	if (uri_has_scheme(uri)) {
		if (!uri_supported_scheme(uri)) {
			command_error(client, ACK_ERROR_NO_EXIST,
				      "unsupported URI scheme");
			return COMMAND_RETURN_ERROR;
		}

		success = spl_append_uri(argv[1], playlist, &error);
	} else
		success = search_add_to_playlist(uri, playlist, nullptr,
						 &error);

	if (!success && error == NULL) {
		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
handle_listplaylists(Client *client,
		     G_GNUC_UNUSED int argc, G_GNUC_UNUSED char *argv[])
{
	GError *error = NULL;
	const auto list = ListPlaylistFiles(&error);
	if (list.empty() && error != NULL)
		return print_error(client, error);

	print_spl_list(client, list);
	return COMMAND_RETURN_OK;
}