DatabaseCommands.cxx 6.4 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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 "client/Client.hxx"
30
#include "client/Response.hxx"
31
#include "tag/Tag.hxx"
32
#include "util/ConstBuffer.hxx"
33
#include "util/StringAPI.hxx"
34
#include "SongFilter.hxx"
35
#include "BulkEdit.hxx"
36

37 38
#include <memory>

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

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

57
static CommandResult
58
handle_match(Client &client, Request args, Response &r, bool fold_case)
59
{
60
	RangeArg window;
61
	if (args.size >= 2 && StringIsEqual(args[args.size - 2], "window")) {
62
		window = args.ParseRange(args.size - 1);
63 64 65

		args.pop_back();
		args.pop_back();
66 67
	} else
		window.SetAll();
68

69
	SongFilter filter;
70
	if (!filter.Parse(args, fold_case)) {
71
		r.Error(ACK_ERROR_ARG, "incorrect arguments");
72
		return CommandResult::ERROR;
73 74
	}

75
	const DatabaseSelection selection("", true, &filter);
76

77 78 79 80
	db_selection_print(r, client.partition,
			   selection, true, false,
			   window.start, window.end);
	return CommandResult::OK;
81 82
}

83
CommandResult
84
handle_find(Client &client, Request args, Response &r)
85
{
86
	return handle_match(client, args, r, false);
87 88
}

89
CommandResult
90
handle_search(Client &client, Request args, Response &r)
91
{
92
	return handle_match(client, args, r, true);
93 94
}

95
static CommandResult
96
handle_match_add(Client &client, Request args, Response &r, bool fold_case)
97
{
98
	SongFilter filter;
99
	if (!filter.Parse(args, fold_case)) {
100
		r.Error(ACK_ERROR_ARG, "incorrect arguments");
101
		return CommandResult::ERROR;
102 103
	}

104 105
	const ScopeBulkEdit bulk_edit(client.partition);

106
	const DatabaseSelection selection("", true, &filter);
107 108
	AddFromDatabase(client.partition, selection);
	return CommandResult::OK;
109 110
}

111
CommandResult
112
handle_findadd(Client &client, Request args, Response &r)
113
{
114
	return handle_match_add(client, args, r, false);
115 116
}

117
CommandResult
118
handle_searchadd(Client &client, Request args, Response &r)
119
{
120
	return handle_match_add(client, args, r, true);
121 122
}

123
CommandResult
124
handle_searchaddpl(Client &client, Request args, Response &r)
125
{
126
	const char *playlist = args.shift();
127

128
	SongFilter filter;
129
	if (!filter.Parse(args, true)) {
130
		r.Error(ACK_ERROR_ARG, "incorrect arguments");
131
		return CommandResult::ERROR;
132 133
	}

134
	const Database &db = client.GetDatabaseOrThrow();
135

136 137 138
	search_add_to_playlist(db, *client.GetStorage(),
			       "", playlist, &filter);
	return CommandResult::OK;
139 140
}

141
CommandResult
142
handle_count(Client &client, Request args, Response &r)
143
{
144
	TagType group = TAG_NUM_OF_ITEM_TYPES;
145
	if (args.size >= 2 && StringIsEqual(args[args.size - 2], "group")) {
146 147 148
		const char *s = args[args.size - 1];
		group = tag_name_parse_i(s);
		if (group == TAG_NUM_OF_ITEM_TYPES) {
149
			r.FormatError(ACK_ERROR_ARG,
150 151 152 153 154 155 156 157
				      "Unknown tag type: %s", s);
			return CommandResult::ERROR;
		}

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

158
	SongFilter filter;
159
	if (!args.IsEmpty() && !filter.Parse(args, false)) {
160
		r.Error(ACK_ERROR_ARG, "incorrect arguments");
161
		return CommandResult::ERROR;
162 163
	}

164 165
	PrintSongCount(r, client.partition, "", &filter, group);
	return CommandResult::OK;
166 167
}

168
CommandResult
169
handle_listall(Client &client, Request args, Response &r)
170
{
171
	/* default is root directory */
172
	const auto uri = args.GetOptional(0, "");
173

174 175 176 177
	db_selection_print(r, client.partition,
			   DatabaseSelection(uri, true),
			   false, false);
	return CommandResult::OK;
178 179
}

180
CommandResult
181
handle_list(Client &client, Request args, Response &r)
182
{
183 184
	const char *tag_name = args.shift();
	unsigned tagType = locate_parse_type(tag_name);
185

186 187
	if (tagType >= TAG_NUM_OF_ITEM_TYPES &&
	    tagType != LOCATE_TAG_FILE_TYPE) {
188
		r.FormatError(ACK_ERROR_ARG,
189
			      "Unknown tag type: %s", tag_name);
190
		return CommandResult::ERROR;
191 192
	}

193
	std::unique_ptr<SongFilter> filter;
194
	tag_mask_t group_mask = 0;
195 196

	if (args.size == 1) {
197
		/* for compatibility with < 0.12.0 */
198
		if (tagType != TAG_ALBUM) {
199
			r.FormatError(ACK_ERROR_ARG,
200 201
				      "should be \"%s\" for 3 arguments",
				      tag_item_names[TAG_ALBUM]);
202
			return CommandResult::ERROR;
203 204
		}

205 206
		filter.reset(new SongFilter((unsigned)TAG_ARTIST,
					    args.shift()));
207
	}
208

209
	while (args.size >= 2 &&
210
	       StringIsEqual(args[args.size - 2], "group")) {
211 212 213
		const char *s = args[args.size - 1];
		TagType gt = tag_name_parse_i(s);
		if (gt == TAG_NUM_OF_ITEM_TYPES) {
214
			r.FormatError(ACK_ERROR_ARG,
215 216 217 218
				      "Unknown tag type: %s", s);
			return CommandResult::ERROR;
		}

219
		group_mask |= tag_mask_t(1) << unsigned(gt);
220 221 222 223 224

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

225
	if (!args.IsEmpty()) {
226
		filter.reset(new SongFilter());
227
		if (!filter->Parse(args, false)) {
228
			r.Error(ACK_ERROR_ARG, "not able to parse args");
229
			return CommandResult::ERROR;
230
		}
231
	}
232

233
	if (tagType < TAG_NUM_OF_ITEM_TYPES &&
234
	    group_mask & (tag_mask_t(1) << tagType)) {
235
		r.Error(ACK_ERROR_ARG, "Conflicting group");
236 237 238
		return CommandResult::ERROR;
	}

239 240 241
	PrintUniqueTags(r, client.partition,
			tagType, group_mask, filter.get());
	return CommandResult::OK;
242 243
}

244
CommandResult
245
handle_listallinfo(Client &client, Request args, Response &r)
246
{
247
	/* default is root directory */
248
	const auto uri = args.GetOptional(0, "");
249

250 251 252 253
	db_selection_print(r, client.partition,
			   DatabaseSelection(uri, true),
			   true, false);
	return CommandResult::OK;
254
}