mpd/src/command/StickerCommands.cxx

172 lines
4.6 KiB
C++
Raw Normal View History

2012-08-25 12:59:54 +02:00
/*
2018-10-31 17:54:59 +01:00
* Copyright 2003-2018 The Music Player Daemon Project
2012-08-25 12:59:54 +02:00
* 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"
#include "Request.hxx"
2013-01-02 20:29:24 +01:00
#include "SongPrint.hxx"
2014-02-19 22:54:52 +01:00
#include "db/Interface.hxx"
2014-01-24 16:15:41 +01:00
#include "sticker/SongSticker.hxx"
#include "sticker/StickerPrint.hxx"
#include "sticker/StickerDatabase.hxx"
#include "client/Client.hxx"
#include "client/Response.hxx"
#include "Partition.hxx"
2015-08-14 19:48:30 +02:00
#include "util/StringAPI.hxx"
#include "util/ScopeExit.hxx"
2012-08-25 12:59:54 +02:00
namespace {
2012-08-25 12:59:54 +02:00
struct sticker_song_find_data {
Response &r;
2012-08-25 12:59:54 +02:00
const char *name;
};
}
2012-08-25 12:59:54 +02:00
static void
sticker_song_find_print_cb(const LightSong &song, const char *value,
void *user_data)
2012-08-25 12:59:54 +02:00
{
struct sticker_song_find_data *data =
(struct sticker_song_find_data *)user_data;
song_print_uri(data->r, song);
sticker_print_value(data->r, data->name, value);
2012-08-25 12:59:54 +02:00
}
static CommandResult
handle_sticker_song(Response &r, Partition &partition, Request args)
2012-08-25 12:59:54 +02:00
{
2016-10-26 18:47:19 +02:00
const Database &db = partition.GetDatabaseOrThrow();
const char *const cmd = args.front();
2012-08-25 12:59:54 +02:00
/* get song song_id key */
2015-08-14 19:48:30 +02:00
if (args.size == 4 && StringIsEqual(cmd, "get")) {
2016-10-26 18:47:19 +02:00
const LightSong *song = db.GetSong(args[2]);
assert(song != nullptr);
AtScopeExit(&db, song) { db.ReturnSong(song); };
2012-08-25 12:59:54 +02:00
const auto value = sticker_song_get_value(*song, args[3]);
2013-10-17 19:37:51 +02:00
if (value.empty()) {
r.Error(ACK_ERROR_NO_EXIST, "no such sticker");
return CommandResult::ERROR;
2012-08-25 12:59:54 +02:00
}
sticker_print_value(r, args[3], value.c_str());
2012-08-25 12:59:54 +02:00
return CommandResult::OK;
2012-08-25 12:59:54 +02:00
/* list song song_id */
2015-08-14 19:48:30 +02:00
} else if (args.size == 3 && StringIsEqual(cmd, "list")) {
2016-10-26 18:47:19 +02:00
const LightSong *song = db.GetSong(args[2]);
assert(song != nullptr);
AtScopeExit(&db, song) { db.ReturnSong(song); };
2012-08-25 12:59:54 +02:00
Sticker *sticker = sticker_song_get(*song);
2012-08-25 12:59:54 +02:00
if (sticker) {
sticker_print(r, *sticker);
2012-08-25 12:59:54 +02:00
sticker_free(sticker);
}
2012-08-25 12:59:54 +02:00
return CommandResult::OK;
2012-08-25 12:59:54 +02:00
/* set song song_id id key */
2015-08-14 19:48:30 +02:00
} else if (args.size == 5 && StringIsEqual(cmd, "set")) {
2016-10-26 18:47:19 +02:00
const LightSong *song = db.GetSong(args[2]);
assert(song != nullptr);
AtScopeExit(&db, song) { db.ReturnSong(song); };
2012-08-25 12:59:54 +02:00
sticker_song_set_value(*song, args[3], args[4]);
return CommandResult::OK;
2012-08-25 12:59:54 +02:00
/* delete song song_id [key] */
} else if ((args.size == 3 || args.size == 4) &&
2015-08-14 19:48:30 +02:00
StringIsEqual(cmd, "delete")) {
2016-10-26 18:47:19 +02:00
const LightSong *song = db.GetSong(args[2]);
assert(song != nullptr);
AtScopeExit(&db, song) { db.ReturnSong(song); };
2012-08-25 12:59:54 +02:00
bool ret = args.size == 3
? sticker_song_delete(*song)
: sticker_song_delete_value(*song, args[3]);
2012-08-25 12:59:54 +02:00
if (!ret) {
r.Error(ACK_ERROR_NO_EXIST, "no such sticker");
return CommandResult::ERROR;
2012-08-25 12:59:54 +02:00
}
return CommandResult::OK;
2012-08-25 12:59:54 +02:00
/* find song dir key */
} else if ((args.size == 4 || args.size == 6) &&
2015-08-14 19:48:30 +02:00
StringIsEqual(cmd, "find")) {
2012-08-25 12:59:54 +02:00
/* "sticker find song a/directory name" */
const char *const base_uri = args[2];
StickerOperator op = StickerOperator::EXISTS;
const char *value = nullptr;
if (args.size == 6) {
/* match the value */
const char *op_s = args[4];
value = args[5];
2015-08-14 19:48:30 +02:00
if (StringIsEqual(op_s, "="))
op = StickerOperator::EQUALS;
2015-08-14 19:48:30 +02:00
else if (StringIsEqual(op_s, "<"))
op = StickerOperator::LESS_THAN;
2015-08-14 19:48:30 +02:00
else if (StringIsEqual(op_s, ">"))
op = StickerOperator::GREATER_THAN;
else {
r.Error(ACK_ERROR_ARG, "bad operator");
return CommandResult::ERROR;
}
}
2012-08-25 12:59:54 +02:00
struct sticker_song_find_data data = {
r,
args[3],
2012-08-25 12:59:54 +02:00
};
sticker_song_find(db, base_uri, data.name,
op, value,
sticker_song_find_print_cb, &data);
2012-08-25 12:59:54 +02:00
return CommandResult::OK;
2012-08-25 12:59:54 +02:00
} else {
r.Error(ACK_ERROR_ARG, "bad request");
return CommandResult::ERROR;
2012-08-25 12:59:54 +02:00
}
}
CommandResult
handle_sticker(Client &client, Request args, Response &r)
2012-08-25 12:59:54 +02:00
{
assert(args.size >= 3);
2012-08-25 12:59:54 +02:00
if (!sticker_enabled()) {
r.Error(ACK_ERROR_UNKNOWN, "sticker database is disabled");
return CommandResult::ERROR;
2012-08-25 12:59:54 +02:00
}
2015-08-14 19:48:30 +02:00
if (StringIsEqual(args[1], "song"))
return handle_sticker_song(r, client.GetPartition(), args);
2012-08-25 12:59:54 +02:00
else {
r.Error(ACK_ERROR_ARG, "unknown sticker domain");
return CommandResult::ERROR;
2012-08-25 12:59:54 +02:00
}
}