DatabaseCommands.cxx 7.95 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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 "DatabaseCommands.hxx"
22
#include "Request.hxx"
Max Kellermann's avatar
Max Kellermann committed
23 24 25
#include "db/DatabaseQueue.hxx"
#include "db/DatabasePlaylist.hxx"
#include "db/DatabasePrint.hxx"
26
#include "db/Count.hxx"
Max Kellermann's avatar
Max Kellermann committed
27
#include "db/Selection.hxx"
28
#include "CommandError.hxx"
29
#include "protocol/RangeArg.hxx"
30
#include "client/Client.hxx"
31
#include "client/Response.hxx"
32
#include "tag/ParseName.hxx"
33
#include "tag/Mask.hxx"
34
#include "util/ConstBuffer.hxx"
35
#include "util/Exception.hxx"
36
#include "util/StringAPI.hxx"
37
#include "util/ASCII.hxx"
38
#include "song/Filter.hxx"
39
#include "BulkEdit.hxx"
40

41 42
#include <memory>

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

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

61 62 63 64 65 66 67 68 69 70 71 72 73
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;
}

74
static CommandResult
75
handle_match(Client &client, Request args, Response &r, bool fold_case)
76
{
77
	RangeArg window = RangeArg::All();
78
	if (args.size >= 2 && StringIsEqual(args[args.size - 2], "window")) {
79
		window = args.ParseRange(args.size - 1);
80 81 82

		args.pop_back();
		args.pop_back();
83
	}
84

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

94
		sort = ParseSortTag(s);
95 96 97 98 99

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

100
	SongFilter filter;
101 102 103 104 105
	try {
		filter.Parse(args, fold_case);
	} catch (...) {
		r.Error(ACK_ERROR_ARG,
			GetFullMessage(std::current_exception()).c_str());
106
		return CommandResult::ERROR;
107
	}
108
	filter.Optimize();
109

110
	const DatabaseSelection selection("", true, &filter);
111

112
	db_selection_print(r, client.GetPartition(),
113
			   selection, true, false,
114
			   sort, descending,
115
			   window);
116
	return CommandResult::OK;
117 118
}

119
CommandResult
120
handle_find(Client &client, Request args, Response &r)
121
{
122
	return handle_match(client, args, r, false);
123 124
}

125
CommandResult
126
handle_search(Client &client, Request args, Response &r)
127
{
128
	return handle_match(client, args, r, true);
129 130
}

131
static CommandResult
132
handle_match_add(Client &client, Request args, Response &r, bool fold_case)
133
{
134
	SongFilter filter;
135 136 137 138 139
	try {
		filter.Parse(args, fold_case);
	} catch (...) {
		r.Error(ACK_ERROR_ARG,
			GetFullMessage(std::current_exception()).c_str());
140
		return CommandResult::ERROR;
141
	}
142
	filter.Optimize();
143

144 145
	auto &partition = client.GetPartition();
	const ScopeBulkEdit bulk_edit(partition);
146

147
	const DatabaseSelection selection("", true, &filter);
148
	AddFromDatabase(partition, selection);
149
	return CommandResult::OK;
150 151
}

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

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

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

169
	SongFilter filter;
170 171 172 173 174
	try {
		filter.Parse(args, true);
	} catch (...) {
		r.Error(ACK_ERROR_ARG,
			GetFullMessage(std::current_exception()).c_str());
175
		return CommandResult::ERROR;
176
	}
177
	filter.Optimize();
178

179
	const Database &db = client.GetDatabaseOrThrow();
180

181
	search_add_to_playlist(db, client.GetStorage(),
182 183
			       "", playlist, &filter);
	return CommandResult::OK;
184 185
}

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

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

203
	SongFilter filter;
204 205 206 207 208 209 210 211
	if (!args.empty()) {
		try {
			filter.Parse(args, false);
		} catch (...) {
			r.Error(ACK_ERROR_ARG,
				GetFullMessage(std::current_exception()).c_str());
			return CommandResult::ERROR;
		}
212 213

		filter.Optimize();
214 215
	}

216
	PrintSongCount(r, client.GetPartition(), "", &filter, group);
217
	return CommandResult::OK;
218 219
}

220
CommandResult
221
handle_listall(Client &client, Request args, Response &r)
222
{
223
	/* default is root directory */
224
	const auto uri = args.GetOptional(0, "");
225

226
	db_selection_print(r, client.GetPartition(),
227 228 229
			   DatabaseSelection(uri, true),
			   false, false);
	return CommandResult::OK;
230 231
}

232 233 234 235 236 237 238 239 240 241 242 243 244 245
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;
		}
246
		filter->Optimize();
247 248 249 250 251 252
	}

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

253
CommandResult
254
handle_list(Client &client, Request args, Response &r)
255
{
256
	const char *tag_name = args.shift();
257 258
	if (StringEqualsCaseASCII(tag_name, "file") ||
	    StringEqualsCaseASCII(tag_name, "filename"))
259 260
		return handle_list_file(client, args, r);

261 262
	const auto tagType = tag_name_parse_i(tag_name);
	if (tagType == TAG_NUM_OF_ITEM_TYPES) {
263
		r.FormatError(ACK_ERROR_ARG,
264
			      "Unknown tag type: %s", tag_name);
265
		return CommandResult::ERROR;
266 267
	}

268
	std::unique_ptr<SongFilter> filter;
269
	TagMask group_mask = TagMask::None();
270 271

	if (args.size == 1) {
272
		/* for compatibility with < 0.12.0 */
273
		if (tagType != TAG_ALBUM) {
274
			r.FormatError(ACK_ERROR_ARG,
275 276
				      "should be \"%s\" for 3 arguments",
				      tag_item_names[TAG_ALBUM]);
277
			return CommandResult::ERROR;
278 279
		}

280
		filter.reset(new SongFilter(TAG_ARTIST,
281
					    args.shift()));
282
	}
283

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

294
		group_mask |= gt;
295 296 297 298 299

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

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

312
	if (group_mask.Test(tagType)) {
313
		r.Error(ACK_ERROR_ARG, "Conflicting group");
314 315 316
		return CommandResult::ERROR;
	}

317
	PrintUniqueTags(r, client.GetPartition(),
318
			tagType, group_mask, 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
}