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"
|
2015-08-11 22:11:28 +02:00
|
|
|
#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"
|
2014-02-01 00:26:34 +01:00
|
|
|
#include "client/Client.hxx"
|
2015-08-06 22:10:25 +02:00
|
|
|
#include "client/Response.hxx"
|
2014-02-01 00:26:34 +01:00
|
|
|
#include "Partition.hxx"
|
2015-08-14 19:48:30 +02:00
|
|
|
#include "util/StringAPI.hxx"
|
2016-10-27 07:50:08 +02:00
|
|
|
#include "util/ScopeExit.hxx"
|
2012-08-25 12:59:54 +02:00
|
|
|
|
2018-08-09 12:57:47 +02:00
|
|
|
namespace {
|
2012-08-25 12:59:54 +02:00
|
|
|
struct sticker_song_find_data {
|
2015-08-06 22:10:25 +02:00
|
|
|
Response &r;
|
2012-08-25 12:59:54 +02:00
|
|
|
const char *name;
|
|
|
|
};
|
2018-08-09 12:57:47 +02:00
|
|
|
}
|
2012-08-25 12:59:54 +02:00
|
|
|
|
|
|
|
static void
|
2014-01-19 10:51:34 +01:00
|
|
|
sticker_song_find_print_cb(const LightSong &song, const char *value,
|
2013-08-10 18:02:44 +02:00
|
|
|
void *user_data)
|
2012-08-25 12:59:54 +02:00
|
|
|
{
|
|
|
|
struct sticker_song_find_data *data =
|
|
|
|
(struct sticker_song_find_data *)user_data;
|
|
|
|
|
2017-02-24 13:56:13 +01:00
|
|
|
song_print_uri(data->r, song);
|
2015-08-06 22:10:25 +02:00
|
|
|
sticker_print_value(data->r, data->name, value);
|
2012-08-25 12:59:54 +02:00
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
static CommandResult
|
2015-08-06 22:10:25 +02:00
|
|
|
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();
|
2013-01-03 00:30:15 +01:00
|
|
|
|
2014-12-06 00:08:08 +01:00
|
|
|
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]);
|
2016-10-27 07:50:08 +02:00
|
|
|
assert(song != nullptr);
|
|
|
|
AtScopeExit(&db, song) { db.ReturnSong(song); };
|
2012-08-25 12:59:54 +02:00
|
|
|
|
2016-10-27 07:50:08 +02:00
|
|
|
const auto value = sticker_song_get_value(*song, args[3]);
|
2013-10-17 19:37:51 +02:00
|
|
|
if (value.empty()) {
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Error(ACK_ERROR_NO_EXIST, "no such sticker");
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::ERROR;
|
2012-08-25 12:59:54 +02:00
|
|
|
}
|
|
|
|
|
2015-08-06 22:10:25 +02:00
|
|
|
sticker_print_value(r, args[3], value.c_str());
|
2012-08-25 12:59:54 +02:00
|
|
|
|
2013-10-20 13:10: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]);
|
2016-03-19 00:13:57 +01:00
|
|
|
assert(song != nullptr);
|
2016-10-27 07:50:08 +02:00
|
|
|
AtScopeExit(&db, song) { db.ReturnSong(song); };
|
2012-08-25 12:59:54 +02:00
|
|
|
|
2016-10-27 07:50:08 +02:00
|
|
|
Sticker *sticker = sticker_song_get(*song);
|
2012-08-25 12:59:54 +02:00
|
|
|
if (sticker) {
|
2015-08-06 22:10:25 +02:00
|
|
|
sticker_print(r, *sticker);
|
2012-08-25 12:59:54 +02:00
|
|
|
sticker_free(sticker);
|
2016-10-27 07:50:08 +02:00
|
|
|
}
|
2012-08-25 12:59:54 +02:00
|
|
|
|
2013-10-20 13:10: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]);
|
2016-03-19 00:13:57 +01:00
|
|
|
assert(song != nullptr);
|
2016-10-27 07:50:08 +02:00
|
|
|
AtScopeExit(&db, song) { db.ReturnSong(song); };
|
2012-08-25 12:59:54 +02:00
|
|
|
|
2016-10-27 07:50:08 +02:00
|
|
|
sticker_song_set_value(*song, args[3], args[4]);
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::OK;
|
2012-08-25 12:59:54 +02:00
|
|
|
/* delete song song_id [key] */
|
2014-12-06 00:08:08 +01:00
|
|
|
} 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]);
|
2016-03-19 00:13:57 +01:00
|
|
|
assert(song != nullptr);
|
2016-10-27 07:50:08 +02:00
|
|
|
AtScopeExit(&db, song) { db.ReturnSong(song); };
|
2012-08-25 12:59:54 +02:00
|
|
|
|
2014-12-06 00:08:08 +01:00
|
|
|
bool ret = args.size == 3
|
2016-10-27 07:50:08 +02:00
|
|
|
? sticker_song_delete(*song)
|
|
|
|
: sticker_song_delete_value(*song, args[3]);
|
2012-08-25 12:59:54 +02:00
|
|
|
if (!ret) {
|
2018-10-31 19:19:07 +01:00
|
|
|
r.Error(ACK_ERROR_NO_EXIST, "no such sticker");
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::ERROR;
|
2012-08-25 12:59:54 +02:00
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::OK;
|
2012-08-25 12:59:54 +02:00
|
|
|
/* find song dir key */
|
2014-12-12 22:12:19 +01:00
|
|
|
} 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" */
|
2014-01-29 17:45:07 +01:00
|
|
|
|
2014-12-06 00:08:08 +01:00
|
|
|
const char *const base_uri = args[2];
|
2014-01-29 17:45:07 +01:00
|
|
|
|
2014-12-12 13:47:57 +01:00
|
|
|
StickerOperator op = StickerOperator::EXISTS;
|
|
|
|
const char *value = nullptr;
|
|
|
|
|
2014-12-12 22:12:19 +01:00
|
|
|
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, "="))
|
2014-12-12 22:12:19 +01:00
|
|
|
op = StickerOperator::EQUALS;
|
2015-08-14 19:48:30 +02:00
|
|
|
else if (StringIsEqual(op_s, "<"))
|
2014-12-12 22:26:04 +01:00
|
|
|
op = StickerOperator::LESS_THAN;
|
2015-08-14 19:48:30 +02:00
|
|
|
else if (StringIsEqual(op_s, ">"))
|
2014-12-12 22:26:04 +01:00
|
|
|
op = StickerOperator::GREATER_THAN;
|
2014-12-12 22:12:19 +01:00
|
|
|
else {
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Error(ACK_ERROR_ARG, "bad operator");
|
2014-12-12 22:12:19 +01:00
|
|
|
return CommandResult::ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-25 12:59:54 +02:00
|
|
|
struct sticker_song_find_data data = {
|
2015-08-06 22:10:25 +02:00
|
|
|
r,
|
2014-12-06 00:08:08 +01:00
|
|
|
args[3],
|
2012-08-25 12:59:54 +02:00
|
|
|
};
|
|
|
|
|
2016-10-27 07:50:08 +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
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::OK;
|
2012-08-25 12:59:54 +02:00
|
|
|
} else {
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Error(ACK_ERROR_ARG, "bad request");
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::ERROR;
|
2012-08-25 12:59:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
CommandResult
|
2015-08-13 12:48:31 +02:00
|
|
|
handle_sticker(Client &client, Request args, Response &r)
|
2012-08-25 12:59:54 +02:00
|
|
|
{
|
2014-12-06 00:08:08 +01:00
|
|
|
assert(args.size >= 3);
|
2012-08-25 12:59:54 +02:00
|
|
|
|
|
|
|
if (!sticker_enabled()) {
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Error(ACK_ERROR_UNKNOWN, "sticker database is disabled");
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::ERROR;
|
2012-08-25 12:59:54 +02:00
|
|
|
}
|
|
|
|
|
2015-08-14 19:48:30 +02:00
|
|
|
if (StringIsEqual(args[1], "song"))
|
2017-02-25 09:58:58 +01:00
|
|
|
return handle_sticker_song(r, client.GetPartition(), args);
|
2012-08-25 12:59:54 +02:00
|
|
|
else {
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Error(ACK_ERROR_ARG, "unknown sticker domain");
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::ERROR;
|
2012-08-25 12:59:54 +02:00
|
|
|
}
|
|
|
|
}
|