mpd/src/command/StickerCommands.cxx

170 lines
4.6 KiB
C++
Raw Normal View History

2012-08-25 12:59:54 +02:00
/*
2014-01-13 22:30:36 +01:00
* Copyright (C) 2003-2014 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-01-24 16:18:50 +01:00
#include "db/DatabaseLock.hxx"
#include "db/DatabasePlugin.hxx"
#include "db/DatabaseGlue.hxx"
#include "db/DatabaseSimple.hxx"
2014-01-24 16:15:41 +01:00
#include "sticker/SongSticker.hxx"
#include "sticker/StickerPrint.hxx"
#include "sticker/StickerDatabase.hxx"
#include "CommandError.hxx"
2013-01-03 10:33:04 +01:00
#include "protocol/Result.hxx"
#include "util/Error.hxx"
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
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->client, song);
sticker_print_value(data->client, data->name, value);
}
static CommandResult
2013-10-19 18:48:38 +02:00
handle_sticker_song(Client &client, int argc, char *argv[])
2012-08-25 12:59:54 +02:00
{
Error error;
const Database *db = GetDatabase(error);
if (db == nullptr)
return print_error(client, error);
2012-08-25 12:59:54 +02:00
/* get song song_id key */
if (argc == 5 && strcmp(argv[1], "get") == 0) {
const LightSong *song = db->GetSong(argv[3], error);
if (song == nullptr)
return print_error(client, error);
2012-08-25 12:59:54 +02:00
2014-01-08 00:41:08 +01:00
const auto value = sticker_song_get_value(*song, argv[4]);
db->ReturnSong(song);
2013-10-17 19:37:51 +02:00
if (value.empty()) {
2012-08-25 12:59:54 +02:00
command_error(client, ACK_ERROR_NO_EXIST,
"no such sticker");
return CommandResult::ERROR;
2012-08-25 12:59:54 +02:00
}
2013-10-17 19:37:51 +02:00
sticker_print_value(client, argv[4], 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 */
} else if (argc == 4 && strcmp(argv[1], "list") == 0) {
const LightSong *song = db->GetSong(argv[3], error);
if (song == nullptr)
return print_error(client, error);
2012-08-25 12:59:54 +02:00
2014-01-08 00:41:08 +01:00
sticker *sticker = sticker_song_get(*song);
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);
}
return CommandResult::OK;
2012-08-25 12:59:54 +02:00
/* set song song_id id key */
} else if (argc == 6 && strcmp(argv[1], "set") == 0) {
const LightSong *song = db->GetSong(argv[3], error);
if (song == nullptr)
return print_error(client, error);
2012-08-25 12:59:54 +02:00
2014-01-08 00:41:08 +01:00
bool ret = sticker_song_set_value(*song, argv[4], argv[5]);
db->ReturnSong(song);
2012-08-25 12:59:54 +02:00
if (!ret) {
command_error(client, ACK_ERROR_SYSTEM,
"failed to set sticker value");
return CommandResult::ERROR;
2012-08-25 12:59:54 +02:00
}
return CommandResult::OK;
2012-08-25 12:59:54 +02:00
/* delete song song_id [key] */
} else if ((argc == 4 || argc == 5) &&
strcmp(argv[1], "delete") == 0) {
const LightSong *song = db->GetSong(argv[3], error);
if (song == nullptr)
return print_error(client, error);
2012-08-25 12:59:54 +02:00
bool ret = argc == 4
2014-01-08 00:41:08 +01:00
? sticker_song_delete(*song)
: sticker_song_delete_value(*song, argv[4]);
db->ReturnSong(song);
2012-08-25 12:59:54 +02:00
if (!ret) {
command_error(client, ACK_ERROR_SYSTEM,
"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 (argc == 5 && strcmp(argv[1], "find") == 0) {
/* "sticker find song a/directory name" */
const char *const base_uri = argv[3];
2012-08-25 12:59:54 +02:00
bool success;
struct sticker_song_find_data data = {
client,
argv[4],
};
success = sticker_song_find(base_uri, data.name,
2012-08-25 12:59:54 +02:00
sticker_song_find_print_cb, &data);
if (!success) {
command_error(client, ACK_ERROR_SYSTEM,
"failed to set search sticker database");
return CommandResult::ERROR;
2012-08-25 12:59:54 +02:00
}
return CommandResult::OK;
2012-08-25 12:59:54 +02:00
} else {
command_error(client, ACK_ERROR_ARG, "bad request");
return CommandResult::ERROR;
2012-08-25 12:59:54 +02:00
}
}
CommandResult
2013-10-19 18:48:38 +02:00
handle_sticker(Client &client, int argc, char *argv[])
2012-08-25 12:59:54 +02:00
{
assert(argc >= 4);
if (!sticker_enabled()) {
command_error(client, ACK_ERROR_UNKNOWN,
"sticker database is disabled");
return CommandResult::ERROR;
2012-08-25 12:59:54 +02:00
}
if (strcmp(argv[2], "song") == 0)
return handle_sticker_song(client, argc, argv);
else {
command_error(client, ACK_ERROR_ARG,
"unknown sticker domain");
return CommandResult::ERROR;
2012-08-25 12:59:54 +02:00
}
}