DatabaseCommands.cxx 5.53 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Copyright (C) 2003-2012 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 "DatabaseCommands.hxx"
22 23
#include "DatabaseQueue.hxx"
#include "DatabasePlaylist.hxx"
24
#include "DatabasePrint.hxx"
25
#include "DatabaseSelection.hxx"
26
#include "CommandError.hxx"
27 28 29
#include "client_internal.h"
#include "tag.h"
#include "uri.h"
30
#include "SongFilter.hxx"
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

extern "C" {
#include "protocol/result.h"
}

#include <assert.h>
#include <string.h>

enum command_return
handle_lsinfo2(struct client *client, int argc, char *argv[])
{
	const char *uri;

	if (argc == 2)
		uri = argv[1];
	else
		/* default is root directory */
		uri = "";

50
	const DatabaseSelection selection(uri, false);
51 52

	GError *error = NULL;
53
	if (!db_selection_print(client, selection, true, &error))
54 55 56 57 58
		return print_error(client, error);

	return COMMAND_RETURN_OK;
}

59 60
static enum command_return
handle_match(struct client *client, int argc, char *argv[], bool fold_case)
61
{
62 63
	SongFilter filter;
	if (!filter.Parse(argc - 1, argv + 1, fold_case)) {
64 65 66 67
		command_error(client, ACK_ERROR_ARG, "incorrect arguments");
		return COMMAND_RETURN_ERROR;
	}

68
	const DatabaseSelection selection("", true, &filter);
69

70
	GError *error = NULL;
71
	return db_selection_print(client, selection, true, &error)
72 73 74 75 76
		? COMMAND_RETURN_OK
		: print_error(client, error);
}

enum command_return
77
handle_find(struct client *client, int argc, char *argv[])
78
{
79
	return handle_match(client, argc, argv, false);
80 81 82 83 84
}

enum command_return
handle_search(struct client *client, int argc, char *argv[])
{
85
	return handle_match(client, argc, argv, true);
86 87
}

88 89
static enum command_return
handle_match_add(struct client *client, int argc, char *argv[], bool fold_case)
90
{
91 92
	SongFilter filter;
	if (!filter.Parse(argc - 1, argv + 1, fold_case)) {
93 94 95 96 97
		command_error(client, ACK_ERROR_ARG, "incorrect arguments");
		return COMMAND_RETURN_ERROR;
	}

	GError *error = NULL;
98
	return findAddIn(client->player_control, "", &filter, &error)
99 100 101 102
		? COMMAND_RETURN_OK
		: print_error(client, error);
}

103 104 105 106 107 108 109 110 111 112 113 114
enum command_return
handle_findadd(struct client *client, int argc, char *argv[])
{
	return handle_match_add(client, argc, argv, false);
}

enum command_return
handle_searchadd(struct client *client, int argc, char *argv[])
{
	return handle_match_add(client, argc, argv, true);
}

115 116 117 118 119
enum command_return
handle_searchaddpl(struct client *client, int argc, char *argv[])
{
	const char *playlist = argv[1];

120 121
	SongFilter filter;
	if (!filter.Parse(argc - 2, argv + 2, true)) {
122 123 124 125 126
		command_error(client, ACK_ERROR_ARG, "incorrect arguments");
		return COMMAND_RETURN_ERROR;
	}

	GError *error = NULL;
127
	return search_add_to_playlist("", playlist, &filter, &error)
128 129 130 131 132 133 134
		? COMMAND_RETURN_OK
		: print_error(client, error);
}

enum command_return
handle_count(struct client *client, int argc, char *argv[])
{
135 136
	SongFilter filter;
	if (!filter.Parse(argc - 1, argv + 1, false)) {
137 138 139 140 141
		command_error(client, ACK_ERROR_ARG, "incorrect arguments");
		return COMMAND_RETURN_ERROR;
	}

	GError *error = NULL;
142
	return  searchStatsForSongsIn(client, "", &filter, &error)
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
		? COMMAND_RETURN_OK
		: print_error(client, error);
}

enum command_return
handle_listall(struct client *client, G_GNUC_UNUSED int argc, char *argv[])
{
	const char *directory = "";

	if (argc == 2)
		directory = argv[1];

	GError *error = NULL;
	return printAllIn(client, directory, &error)
		? COMMAND_RETURN_OK
		: print_error(client, error);
}

enum command_return
handle_list(struct client *client, int argc, char *argv[])
{
164
	unsigned tagType = locate_parse_type(argv[1]);
165

166
	if (tagType == TAG_NUM_OF_ITEM_TYPES) {
167 168 169 170 171 172 173 174 175 176 177
		command_error(client, ACK_ERROR_ARG, "\"%s\" is not known", argv[1]);
		return COMMAND_RETURN_ERROR;
	}

	if (tagType == LOCATE_TAG_ANY_TYPE) {
		command_error(client, ACK_ERROR_ARG,
			      "\"any\" is not a valid return tag type");
		return COMMAND_RETURN_ERROR;
	}

	/* for compatibility with < 0.12.0 */
178
	SongFilter *filter;
179 180 181 182 183 184 185 186
	if (argc == 3) {
		if (tagType != TAG_ALBUM) {
			command_error(client, ACK_ERROR_ARG,
				      "should be \"%s\" for 3 arguments",
				      tag_item_names[TAG_ALBUM]);
			return COMMAND_RETURN_ERROR;
		}

187
		filter = new SongFilter((unsigned)TAG_ARTIST, argv[2]);
188
	} else if (argc > 2) {
189 190 191
		filter = new SongFilter();
		if (!filter->Parse(argc - 2, argv + 2, false)) {
			delete filter;
192 193 194 195
			command_error(client, ACK_ERROR_ARG,
				      "not able to parse args");
			return COMMAND_RETURN_ERROR;
		}
196
	} else
197
		filter = nullptr;
198 199 200

	GError *error = NULL;
	enum command_return ret =
201
		listAllUniqueTags(client, tagType, filter, &error)
202 203 204
		? COMMAND_RETURN_OK
		: print_error(client, error);

205
	delete filter;
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222

	return ret;
}

enum command_return
handle_listallinfo(struct client *client, G_GNUC_UNUSED int argc, char *argv[])
{
	const char *directory = "";

	if (argc == 2)
		directory = argv[1];

	GError *error = NULL;
	return printInfoForAllIn(client, directory, &error)
		? COMMAND_RETURN_OK
		: print_error(client, error);
}