2012-08-25 12:59:54 +02:00
|
|
|
/*
|
2015-01-01 19:48:13 +01:00
|
|
|
* Copyright (C) 2003-2015 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"
|
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:18:50 +01:00
|
|
|
#include "db/DatabaseGlue.hxx"
|
2014-01-24 16:15:41 +01:00
|
|
|
#include "sticker/SongSticker.hxx"
|
|
|
|
#include "sticker/StickerPrint.hxx"
|
|
|
|
#include "sticker/StickerDatabase.hxx"
|
2013-01-03 00:30:15 +01:00
|
|
|
#include "CommandError.hxx"
|
2013-01-03 10:33:04 +01:00
|
|
|
#include "protocol/Result.hxx"
|
2014-02-01 00:26:34 +01:00
|
|
|
#include "client/Client.hxx"
|
|
|
|
#include "Partition.hxx"
|
|
|
|
#include "Instance.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Error.hxx"
|
2014-12-06 00:08:08 +01:00
|
|
|
#include "util/ConstBuffer.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
|
2012-08-25 12:59:54 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
struct sticker_song_find_data {
|
2013-10-19 18:48:38 +02:00
|
|
|
Client &client;
|
2012-08-25 12:59:54 +02:00
|
|
|
const char *name;
|
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
song_print_uri(data->client, song);
|
|
|
|
sticker_print_value(data->client, data->name, value);
|
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
static CommandResult
|
2014-12-06 00:08:08 +01:00
|
|
|
handle_sticker_song(Client &client, ConstBuffer<const char *> args)
|
2012-08-25 12:59:54 +02:00
|
|
|
{
|
2013-08-10 18:02:44 +02:00
|
|
|
Error error;
|
2014-02-01 00:26:34 +01:00
|
|
|
const Database *db = client.GetDatabase(error);
|
2013-01-03 00:30:15 +01:00
|
|
|
if (db == nullptr)
|
|
|
|
return print_error(client, error);
|
|
|
|
|
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 */
|
2014-12-06 00:08:08 +01:00
|
|
|
if (args.size == 4 && strcmp(cmd, "get") == 0) {
|
|
|
|
const LightSong *song = db->GetSong(args[2], error);
|
2013-01-03 00:30:15 +01:00
|
|
|
if (song == nullptr)
|
|
|
|
return print_error(client, error);
|
2012-08-25 12:59:54 +02:00
|
|
|
|
2014-12-12 14:13:14 +01:00
|
|
|
const auto value = sticker_song_get_value(*song, args[3],
|
|
|
|
error);
|
2013-01-03 00:30:15 +01:00
|
|
|
db->ReturnSong(song);
|
2013-10-17 19:37:51 +02:00
|
|
|
if (value.empty()) {
|
2014-12-12 14:13:14 +01:00
|
|
|
if (error.IsDefined())
|
|
|
|
return print_error(client, error);
|
|
|
|
|
2012-08-25 12:59:54 +02:00
|
|
|
command_error(client, 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
|
|
|
}
|
|
|
|
|
2014-12-06 00:08:08 +01:00
|
|
|
sticker_print_value(client, 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 */
|
2014-12-06 00:08:08 +01:00
|
|
|
} else if (args.size == 3 && strcmp(cmd, "list") == 0) {
|
|
|
|
const LightSong *song = db->GetSong(args[2], error);
|
2013-01-03 00:30:15 +01:00
|
|
|
if (song == nullptr)
|
|
|
|
return print_error(client, error);
|
2012-08-25 12:59:54 +02:00
|
|
|
|
2015-01-28 19:33:56 +01:00
|
|
|
Sticker *sticker = sticker_song_get(*song, error);
|
2013-01-03 00:30:15 +01:00
|
|
|
db->ReturnSong(song);
|
2012-08-25 12:59:54 +02:00
|
|
|
if (sticker) {
|
2013-10-19 18:48:38 +02:00
|
|
|
sticker_print(client, *sticker);
|
2012-08-25 12:59:54 +02:00
|
|
|
sticker_free(sticker);
|
2014-12-12 14:13:14 +01:00
|
|
|
} else if (error.IsDefined())
|
|
|
|
return print_error(client, 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
|
|
|
/* set song song_id id key */
|
2014-12-06 00:08:08 +01:00
|
|
|
} else if (args.size == 5 && strcmp(cmd, "set") == 0) {
|
|
|
|
const LightSong *song = db->GetSong(args[2], error);
|
2013-01-03 00:30:15 +01:00
|
|
|
if (song == nullptr)
|
|
|
|
return print_error(client, error);
|
2012-08-25 12:59:54 +02:00
|
|
|
|
2014-12-12 14:13:14 +01:00
|
|
|
bool ret = sticker_song_set_value(*song, args[3], args[4],
|
|
|
|
error);
|
2013-01-03 00:30:15 +01:00
|
|
|
db->ReturnSong(song);
|
2012-08-25 12:59:54 +02:00
|
|
|
if (!ret) {
|
2014-12-12 14:13:14 +01:00
|
|
|
if (error.IsDefined())
|
|
|
|
return print_error(client, error);
|
|
|
|
|
2012-08-25 12:59:54 +02:00
|
|
|
command_error(client, ACK_ERROR_SYSTEM,
|
|
|
|
"failed to set sticker value");
|
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
|
|
|
/* delete song song_id [key] */
|
2014-12-06 00:08:08 +01:00
|
|
|
} else if ((args.size == 3 || args.size == 4) &&
|
|
|
|
strcmp(cmd, "delete") == 0) {
|
|
|
|
const LightSong *song = db->GetSong(args[2], error);
|
2013-01-03 00:30:15 +01:00
|
|
|
if (song == nullptr)
|
|
|
|
return print_error(client, error);
|
2012-08-25 12:59:54 +02:00
|
|
|
|
2014-12-06 00:08:08 +01:00
|
|
|
bool ret = args.size == 3
|
2014-12-12 14:13:14 +01:00
|
|
|
? sticker_song_delete(*song, error)
|
|
|
|
: sticker_song_delete_value(*song, args[3], error);
|
2013-01-03 00:30:15 +01:00
|
|
|
db->ReturnSong(song);
|
2012-08-25 12:59:54 +02:00
|
|
|
if (!ret) {
|
2014-12-12 14:13:14 +01:00
|
|
|
if (error.IsDefined())
|
|
|
|
return print_error(client, error);
|
|
|
|
|
2012-08-25 12:59:54 +02:00
|
|
|
command_error(client, ACK_ERROR_SYSTEM,
|
|
|
|
"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) &&
|
|
|
|
strcmp(cmd, "find") == 0) {
|
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];
|
|
|
|
|
|
|
|
if (strcmp(op_s, "=") == 0)
|
|
|
|
op = StickerOperator::EQUALS;
|
2014-12-12 22:26:04 +01:00
|
|
|
else if (strcmp(op_s, "<") == 0)
|
|
|
|
op = StickerOperator::LESS_THAN;
|
|
|
|
else if (strcmp(op_s, ">") == 0)
|
|
|
|
op = StickerOperator::GREATER_THAN;
|
2014-12-12 22:12:19 +01:00
|
|
|
else {
|
|
|
|
command_error(client, ACK_ERROR_ARG,
|
|
|
|
"bad operator");
|
|
|
|
return CommandResult::ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-25 12:59:54 +02:00
|
|
|
struct sticker_song_find_data data = {
|
|
|
|
client,
|
2014-12-06 00:08:08 +01:00
|
|
|
args[3],
|
2012-08-25 12:59:54 +02:00
|
|
|
};
|
|
|
|
|
2015-08-11 21:54:29 +02:00
|
|
|
if (!sticker_song_find(*db, base_uri, data.name,
|
|
|
|
op, value,
|
|
|
|
sticker_song_find_print_cb, &data,
|
|
|
|
error)) {
|
2014-12-12 14:13:14 +01:00
|
|
|
if (error.IsDefined())
|
|
|
|
return print_error(client, error);
|
|
|
|
|
2012-08-25 12:59:54 +02:00
|
|
|
command_error(client, ACK_ERROR_SYSTEM,
|
|
|
|
"failed to set search sticker database");
|
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
|
|
|
} else {
|
|
|
|
command_error(client, 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
|
2014-12-06 00:08:08 +01:00
|
|
|
handle_sticker(Client &client, ConstBuffer<const char *> args)
|
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()) {
|
|
|
|
command_error(client, 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
|
|
|
}
|
|
|
|
|
2014-12-06 00:08:08 +01:00
|
|
|
if (strcmp(args[1], "song") == 0)
|
|
|
|
return handle_sticker_song(client, args);
|
2012-08-25 12:59:54 +02:00
|
|
|
else {
|
|
|
|
command_error(client, 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
|
|
|
}
|
|
|
|
}
|