StickerCommands.cxx 4.92 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 "StickerCommands.hxx"
21
#include "Request.hxx"
Max Kellermann's avatar
Max Kellermann committed
22
#include "SongPrint.hxx"
23
#include "db/Interface.hxx"
24
#include "sticker/Sticker.hxx"
25
#include "sticker/SongSticker.hxx"
26 27
#include "sticker/Print.hxx"
#include "sticker/Database.hxx"
28
#include "client/Client.hxx"
29
#include "client/Response.hxx"
30
#include "Partition.hxx"
31
#include "Instance.hxx"
32
#include "util/StringAPI.hxx"
33
#include "util/ScopeExit.hxx"
34

35
namespace {
36
struct sticker_song_find_data {
37
	Response &r;
38 39
	const char *name;
};
40
}
41 42

static void
43
sticker_song_find_print_cb(const LightSong &song, const char *value,
44
			   void *user_data)
45 46 47 48
{
	struct sticker_song_find_data *data =
		(struct sticker_song_find_data *)user_data;

49
	song_print_uri(data->r, song);
50
	sticker_print_value(data->r, data->name, value);
51 52
}

53
static CommandResult
54 55 56
handle_sticker_song(Response &r, Partition &partition,
		    StickerDatabase &sticker_database,
		    Request args)
57
{
58
	const Database &db = partition.GetDatabaseOrThrow();
59

60 61
	const char *const cmd = args.front();

62
	/* get song song_id key */
63
	if (args.size == 4 && StringIsEqual(cmd, "get")) {
64
		const LightSong *song = db.GetSong(args[2]);
65 66
		assert(song != nullptr);
		AtScopeExit(&db, song) { db.ReturnSong(song); };
67

68 69
		const auto value = sticker_song_get_value(sticker_database,
							  *song, args[3]);
70
		if (value.empty()) {
71
			r.Error(ACK_ERROR_NO_EXIST, "no such sticker");
72
			return CommandResult::ERROR;
73 74
		}

75
		sticker_print_value(r, args[3], value.c_str());
76

77
		return CommandResult::OK;
78
	/* list song song_id */
79
	} else if (args.size == 3 && StringIsEqual(cmd, "list")) {
80
		const LightSong *song = db.GetSong(args[2]);
81
		assert(song != nullptr);
82
		AtScopeExit(&db, song) { db.ReturnSong(song); };
83

84
		const auto sticker = sticker_song_get(sticker_database, *song);
85
		sticker_print(r, sticker);
86

87
		return CommandResult::OK;
88
	/* set song song_id id key */
89
	} else if (args.size == 5 && StringIsEqual(cmd, "set")) {
90
		const LightSong *song = db.GetSong(args[2]);
91
		assert(song != nullptr);
92
		AtScopeExit(&db, song) { db.ReturnSong(song); };
93

94 95
		sticker_song_set_value(sticker_database, *song,
				       args[3], args[4]);
96
		return CommandResult::OK;
97
	/* delete song song_id [key] */
98
	} else if ((args.size == 3 || args.size == 4) &&
99
		   StringIsEqual(cmd, "delete")) {
100
		const LightSong *song = db.GetSong(args[2]);
101
		assert(song != nullptr);
102
		AtScopeExit(&db, song) { db.ReturnSong(song); };
103

104
		bool ret = args.size == 3
105 106 107
			? sticker_song_delete(sticker_database, *song)
			: sticker_song_delete_value(sticker_database, *song,
						    args[3]);
108
		if (!ret) {
109
			r.Error(ACK_ERROR_NO_EXIST, "no such sticker");
110
			return CommandResult::ERROR;
111 112
		}

113
		return CommandResult::OK;
114
	/* find song dir key */
115
	} else if ((args.size == 4 || args.size == 6) &&
116
		   StringIsEqual(cmd, "find")) {
117
		/* "sticker find song a/directory name" */
118

119
		const char *const base_uri = args[2];
120

121 122 123
		StickerOperator op = StickerOperator::EXISTS;
		const char *value = nullptr;

124 125 126 127 128 129
		if (args.size == 6) {
			/* match the value */

			const char *op_s = args[4];
			value = args[5];

130
			if (StringIsEqual(op_s, "="))
131
				op = StickerOperator::EQUALS;
132
			else if (StringIsEqual(op_s, "<"))
133
				op = StickerOperator::LESS_THAN;
134
			else if (StringIsEqual(op_s, ">"))
135
				op = StickerOperator::GREATER_THAN;
136
			else {
137
				r.Error(ACK_ERROR_ARG, "bad operator");
138 139 140 141
				return CommandResult::ERROR;
			}
		}

142
		struct sticker_song_find_data data = {
143
			r,
144
			args[3],
145 146
		};

147
		sticker_song_find(sticker_database, db, base_uri, data.name,
148 149
				  op, value,
				  sticker_song_find_print_cb, &data);
150

151
		return CommandResult::OK;
152
	} else {
153
		r.Error(ACK_ERROR_ARG, "bad request");
154
		return CommandResult::ERROR;
155 156 157
	}
}

158
CommandResult
159
handle_sticker(Client &client, Request args, Response &r)
160
{
161
	assert(args.size >= 3);
162

163 164
	auto &instance = client.GetInstance();
	if (!instance.HasStickerDatabase()) {
165
		r.Error(ACK_ERROR_UNKNOWN, "sticker database is disabled");
166
		return CommandResult::ERROR;
167 168
	}

169 170
	auto &sticker_database = *instance.sticker_database;

171
	if (StringIsEqual(args[1], "song"))
172 173 174
		return handle_sticker_song(r, client.GetPartition(),
					   sticker_database,
					   args);
175
	else {
176
		r.Error(ACK_ERROR_ARG, "unknown sticker domain");
177
		return CommandResult::ERROR;
178 179
	}
}