DatabaseCommands.cxx 8.16 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2019 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 "protocol/RangeArg.hxx"
28
#include "client/Client.hxx"
29
#include "client/Response.hxx"
30
#include "tag/ParseName.hxx"
31
#include "util/ConstBuffer.hxx"
32
#include "util/Exception.hxx"
33
#include "util/StringAPI.hxx"
34
#include "util/ASCII.hxx"
35
#include "song/Filter.hxx"
36

37
#include <memory>
38
#include <vector>
39

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

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

58 59 60 61 62 63 64 65 66 67 68 69 70
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;
}

71 72 73 74 75 76 77
/**
 * 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)
78
{
79
	RangeArg window = RangeArg::All();
80
	if (args.size >= 2 && StringIsEqual(args[args.size - 2], "window")) {
81
		window = args.ParseRange(args.size - 1);
82 83 84

		args.pop_back();
		args.pop_back();
85
	}
86

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

96
		sort = ParseSortTag(s);
97 98 99 100 101

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

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

110 111 112 113
	DatabaseSelection selection("", true, &filter);
	selection.window = window;
	selection.sort = sort;
	selection.descending = descending;
114 115 116 117 118 119 120 121
	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);
122

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

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

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

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

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

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

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

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

168
	SongFilter filter;
169
	const auto selection = ParseDatabaseSelection(args, true, filter);
170

171
	const Database &db = client.GetDatabaseOrThrow();
172

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

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

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

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

		filter.Optimize();
206 207
	}

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

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

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

224 225 226 227 228 229 230 231 232 233 234 235 236 237
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;
		}
238
		filter->Optimize();
239 240 241 242 243 244
	}

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

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

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

260
	std::unique_ptr<SongFilter> filter;
261
	std::vector<TagType> tag_types;
262

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

275
		filter.reset(new SongFilter(TAG_ARTIST,
276
					    args.shift()));
277
	}
278

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

289 290 291 292 293 294 295 296 297
		if (group == tagType ||
		    std::find(tag_types.begin(), tag_types.end(),
			      group) != tag_types.end()) {
			r.Error(ACK_ERROR_ARG, "Conflicting group");
			return CommandResult::ERROR;
		}

		tag_types.emplace_back(group);

298 299 300 301
		args.pop_back();
		args.pop_back();
	}

302 303
	tag_types.emplace_back(tagType);

304
	if (!args.empty()) {
305
		filter.reset(new SongFilter());
306 307 308 309 310
		try {
			filter->Parse(args, false);
		} catch (...) {
			r.Error(ACK_ERROR_ARG,
				GetFullMessage(std::current_exception()).c_str());
311
			return CommandResult::ERROR;
312
		}
313
		filter->Optimize();
314
	}
315

316
	PrintUniqueTags(r, client.GetPartition(),
317 318
			{&tag_types.front(), tag_types.size()},
			filter.get());
319
	return CommandResult::OK;
320 321
}

322
CommandResult
323
handle_listallinfo(Client &client, Request args, Response &r)
324
{
325
	/* default is root directory */
326
	const auto uri = args.GetOptional(0, "");
327

328
	db_selection_print(r, client.GetPartition(),
329 330 331
			   DatabaseSelection(uri, true),
			   true, false);
	return CommandResult::OK;
332
}