DatabaseCommands.cxx 8.06 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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 "DatabaseCommands.hxx"
21
#include "Request.hxx"
Max Kellermann's avatar
Max Kellermann committed
22 23 24
#include "db/DatabaseQueue.hxx"
#include "db/DatabasePlaylist.hxx"
#include "db/DatabasePrint.hxx"
25
#include "db/Count.hxx"
Max Kellermann's avatar
Max Kellermann committed
26
#include "db/Selection.hxx"
27
#include "CommandError.hxx"
28
#include "protocol/RangeArg.hxx"
29
#include "client/Client.hxx"
30
#include "client/Response.hxx"
31
#include "tag/ParseName.hxx"
32
#include "tag/Mask.hxx"
33
#include "util/ConstBuffer.hxx"
34
#include "util/Exception.hxx"
35
#include "util/StringAPI.hxx"
36
#include "util/ASCII.hxx"
37
#include "song/Filter.hxx"
38
#include "BulkEdit.hxx"
39

40 41
#include <memory>

42
CommandResult
43
handle_listfiles_db(Client &client, Response &r, const char *uri)
44 45
{
	const DatabaseSelection selection(uri, false);
46
	db_selection_print(r, client.GetPartition(),
47
			   selection, false, true);
48 49 50
	return CommandResult::OK;
}

51
CommandResult
52
handle_lsinfo2(Client &client, const char *uri, Response &r)
53
{
54
	const DatabaseSelection selection(uri, false);
55
	db_selection_print(r, client.GetPartition(),
56
			   selection, true, false);
57
	return CommandResult::OK;
58 59
}

60 61 62 63 64 65 66 67 68 69 70 71 72
static TagType
ParseSortTag(const char *s)
{
	if (StringIsEqualIgnoreCase(s, "Last-Modified"))
		return TagType(SORT_TAG_LAST_MODIFIED);

	TagType tag = tag_name_parse_i(s);
	if (tag == TAG_NUM_OF_ITEM_TYPES)
		throw ProtocolError(ACK_ERROR_ARG, "Unknown sort tag");

	return tag;
}

73 74 75 76 77 78 79
/**
 * Convert all remaining arguments to a #DatabaseSelection.
 *
 * @param filter a buffer to be used for DatabaseSelection::filter
 */
static DatabaseSelection
ParseDatabaseSelection(Request args, bool fold_case, SongFilter &filter)
80
{
81
	RangeArg window = RangeArg::All();
82
	if (args.size >= 2 && StringIsEqual(args[args.size - 2], "window")) {
83
		window = args.ParseRange(args.size - 1);
84 85 86

		args.pop_back();
		args.pop_back();
87
	}
88

89
	TagType sort = TAG_NUM_OF_ITEM_TYPES;
90
	bool descending = false;
91
	if (args.size >= 2 && StringIsEqual(args[args.size - 2], "sort")) {
92 93 94 95 96 97
		const char *s = args.back();
		if (*s == '-') {
			descending = true;
			++s;
		}

98
		sort = ParseSortTag(s);
99 100 101 102 103

		args.pop_back();
		args.pop_back();
	}

104 105 106
	try {
		filter.Parse(args, fold_case);
	} catch (...) {
107 108
		throw ProtocolError(ACK_ERROR_ARG,
				    GetFullMessage(std::current_exception()).c_str());
109
	}
110
	filter.Optimize();
111

112 113 114 115
	DatabaseSelection selection("", true, &filter);
	selection.window = window;
	selection.sort = sort;
	selection.descending = descending;
116 117 118 119 120 121 122 123
	return selection;
}

static CommandResult
handle_match(Client &client, Request args, Response &r, bool fold_case)
{
	SongFilter filter;
	const auto selection = ParseDatabaseSelection(args, fold_case, filter);
124

125
	db_selection_print(r, client.GetPartition(),
126
			   selection, true, false);
127
	return CommandResult::OK;
128 129
}

130
CommandResult
131
handle_find(Client &client, Request args, Response &r)
132
{
133
	return handle_match(client, args, r, false);
134 135
}

136
CommandResult
137
handle_search(Client &client, Request args, Response &r)
138
{
139
	return handle_match(client, args, r, true);
140 141
}

142
static CommandResult
143
handle_match_add(Client &client, Request args, bool fold_case)
144
{
145
	SongFilter filter;
146
	const auto selection = ParseDatabaseSelection(args, fold_case, filter);
147

148 149
	auto &partition = client.GetPartition();
	AddFromDatabase(partition, selection);
150
	return CommandResult::OK;
151 152
}

153
CommandResult
154
handle_findadd(Client &client, Request args, Response &)
155
{
156
	return handle_match_add(client, args, false);
157 158
}

159
CommandResult
160
handle_searchadd(Client &client, Request args, Response &)
161
{
162
	return handle_match_add(client, args, true);
163 164
}

165
CommandResult
166
handle_searchaddpl(Client &client, Request args, Response &)
167
{
168
	const char *playlist = args.shift();
169

170
	SongFilter filter;
171
	const auto selection = ParseDatabaseSelection(args, true, filter);
172

173
	const Database &db = client.GetDatabaseOrThrow();
174

175
	search_add_to_playlist(db, client.GetStorage(),
176
			       playlist, selection);
177
	return CommandResult::OK;
178 179
}

180
CommandResult
181
handle_count(Client &client, Request args, Response &r)
182
{
183
	TagType group = TAG_NUM_OF_ITEM_TYPES;
184
	if (args.size >= 2 && StringIsEqual(args[args.size - 2], "group")) {
185 186 187
		const char *s = args[args.size - 1];
		group = tag_name_parse_i(s);
		if (group == TAG_NUM_OF_ITEM_TYPES) {
188
			r.FormatError(ACK_ERROR_ARG,
189 190 191 192 193 194 195 196
				      "Unknown tag type: %s", s);
			return CommandResult::ERROR;
		}

		args.pop_back();
		args.pop_back();
	}

197
	SongFilter filter;
198 199 200 201 202 203 204 205
	if (!args.empty()) {
		try {
			filter.Parse(args, false);
		} catch (...) {
			r.Error(ACK_ERROR_ARG,
				GetFullMessage(std::current_exception()).c_str());
			return CommandResult::ERROR;
		}
206 207

		filter.Optimize();
208 209
	}

210
	PrintSongCount(r, client.GetPartition(), "", &filter, group);
211
	return CommandResult::OK;
212 213
}

214
CommandResult
215
handle_listall(Client &client, Request args, Response &r)
216
{
217
	/* default is root directory */
218
	const auto uri = args.GetOptional(0, "");
219

220
	db_selection_print(r, client.GetPartition(),
221 222 223
			   DatabaseSelection(uri, true),
			   false, false);
	return CommandResult::OK;
224 225
}

226 227 228 229 230 231 232 233 234 235 236 237 238 239
static CommandResult
handle_list_file(Client &client, Request args, Response &r)
{
	std::unique_ptr<SongFilter> filter;

	if (!args.empty()) {
		filter.reset(new SongFilter());
		try {
			filter->Parse(args, false);
		} catch (...) {
			r.Error(ACK_ERROR_ARG,
				GetFullMessage(std::current_exception()).c_str());
			return CommandResult::ERROR;
		}
240
		filter->Optimize();
241 242 243 244 245 246
	}

	PrintSongUris(r, client.GetPartition(), filter.get());
	return CommandResult::OK;
}

247
CommandResult
248
handle_list(Client &client, Request args, Response &r)
249
{
250
	const char *tag_name = args.shift();
251 252
	if (StringEqualsCaseASCII(tag_name, "file") ||
	    StringEqualsCaseASCII(tag_name, "filename"))
253 254
		return handle_list_file(client, args, r);

255 256
	const auto tagType = tag_name_parse_i(tag_name);
	if (tagType == TAG_NUM_OF_ITEM_TYPES) {
257
		r.FormatError(ACK_ERROR_ARG,
258
			      "Unknown tag type: %s", tag_name);
259
		return CommandResult::ERROR;
260 261
	}

262
	std::unique_ptr<SongFilter> filter;
263
	TagType group = TAG_NUM_OF_ITEM_TYPES;
264

265 266 267 268
	if (args.size == 1 &&
	    /* parantheses are the syntax for filter expressions: no
	       compatibility mode */
	    args.front()[0] != '(') {
269
		/* for compatibility with < 0.12.0 */
270
		if (tagType != TAG_ALBUM) {
271
			r.FormatError(ACK_ERROR_ARG,
272 273
				      "should be \"%s\" for 3 arguments",
				      tag_item_names[TAG_ALBUM]);
274
			return CommandResult::ERROR;
275 276
		}

277
		filter.reset(new SongFilter(TAG_ARTIST,
278
					    args.shift()));
279
	}
280

281 282
	if  (args.size >= 2 &&
	     StringIsEqual(args[args.size - 2], "group")) {
283
		const char *s = args[args.size - 1];
284 285
		group = tag_name_parse_i(s);
		if (group == TAG_NUM_OF_ITEM_TYPES) {
286
			r.FormatError(ACK_ERROR_ARG,
287 288 289 290 291 292 293 294
				      "Unknown tag type: %s", s);
			return CommandResult::ERROR;
		}

		args.pop_back();
		args.pop_back();
	}

295
	if (!args.empty()) {
296
		filter.reset(new SongFilter());
297 298 299 300 301
		try {
			filter->Parse(args, false);
		} catch (...) {
			r.Error(ACK_ERROR_ARG,
				GetFullMessage(std::current_exception()).c_str());
302
			return CommandResult::ERROR;
303
		}
304
		filter->Optimize();
305
	}
306

307
	if (tagType < TAG_NUM_OF_ITEM_TYPES && tagType == group) {
308
		r.Error(ACK_ERROR_ARG, "Conflicting group");
309 310 311
		return CommandResult::ERROR;
	}

312
	PrintUniqueTags(r, client.GetPartition(),
313
			tagType, group, filter.get());
314
	return CommandResult::OK;
315 316
}

317
CommandResult
318
handle_listallinfo(Client &client, Request args, Response &r)
319
{
320
	/* default is root directory */
321
	const auto uri = args.GetOptional(0, "");
322

323
	db_selection_print(r, client.GetPartition(),
324 325 326
			   DatabaseSelection(uri, true),
			   true, false);
	return CommandResult::OK;
327
}