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

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

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

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

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

57 58
	const char *const cmd = args.front();

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

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

71
		sticker_print_value(r, args[3], value.c_str());
72

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

80
		Sticker *sticker = sticker_song_get(*song);
81
		if (sticker) {
82
			sticker_print(r, *sticker);
83
			sticker_free(sticker);
84
		}
85

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

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

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

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

116
		const char *const base_uri = args[2];
117

118 119 120
		StickerOperator op = StickerOperator::EXISTS;
		const char *value = nullptr;

121 122 123 124 125 126
		if (args.size == 6) {
			/* match the value */

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

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

139
		struct sticker_song_find_data data = {
140
			r,
141
			args[3],
142 143
		};

144 145 146
		sticker_song_find(db, base_uri, data.name,
				  op, value,
				  sticker_song_find_print_cb, &data);
147

148
		return CommandResult::OK;
149
	} else {
150
		r.Error(ACK_ERROR_ARG, "bad request");
151
		return CommandResult::ERROR;
152 153 154
	}
}

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

	if (!sticker_enabled()) {
161
		r.Error(ACK_ERROR_UNKNOWN, "sticker database is disabled");
162
		return CommandResult::ERROR;
163 164
	}

165
	if (StringIsEqual(args[1], "song"))
166
		return handle_sticker_song(r, client.GetPartition(), args);
167
	else {
168
		r.Error(ACK_ERROR_ARG, "unknown sticker domain");
169
		return CommandResult::ERROR;
170 171
	}
}