StickerCommands.cxx 4.66 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

struct sticker_song_find_data {
35 36
	Response &r;
	Partition &partition;
37 38 39 40
	const char *name;
};

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

138
		struct sticker_song_find_data data = {
139 140
			r,
			partition,
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.partition, args);
167
	else {
168
		r.Error(ACK_ERROR_ARG, "unknown sticker domain");
169
		return CommandResult::ERROR;
170 171
	}
}