Commit 684a3d30 authored by Max Kellermann's avatar Max Kellermann

command: move code to *Commands.cxx

parent 25740d27
......@@ -242,7 +242,12 @@ src_mpd_SOURCES = \
src/protocol/result.c src/protocol/result.h \
src/CommandError.cxx src/CommandError.h \
src/command.c \
src/QueueCommands.cxx src/QueueCommands.hxx \
src/PlayerCommands.cxx src/PlayerCommands.hxx \
src/PlaylistCommands.cxx src/PlaylistCommands.hxx \
src/DatabaseCommands.cxx src/DatabaseCommands.hxx \
src/OutputCommands.cxx src/OutputCommands.hxx \
src/MessageCommands.cxx src/MessageCommands.hxx \
src/idle.c \
src/cmdline.c \
src/conf.c \
......@@ -390,6 +395,7 @@ endif
if ENABLE_SQLITE
src_mpd_SOURCES += \
src/StickerCommands.cxx src/StickerCommands.hxx \
src/sticker.c \
src/sticker_print.c \
src/song_sticker.c
......
/*
* Copyright (C) 2003-2012 The Music Player Daemon Project
* 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 "MessageCommands.hxx"
extern "C" {
#include "protocol/argparser.h"
#include "protocol/result.h"
#include "client_internal.h"
#include "client_subscribe.h"
}
#include <assert.h>
enum command_return
handle_subscribe(struct client *client, G_GNUC_UNUSED int argc, char *argv[])
{
assert(argc == 2);
switch (client_subscribe(client, argv[1])) {
case CLIENT_SUBSCRIBE_OK:
return COMMAND_RETURN_OK;
case CLIENT_SUBSCRIBE_INVALID:
command_error(client, ACK_ERROR_ARG,
"invalid channel name");
return COMMAND_RETURN_ERROR;
case CLIENT_SUBSCRIBE_ALREADY:
command_error(client, ACK_ERROR_EXIST,
"already subscribed to this channel");
return COMMAND_RETURN_ERROR;
case CLIENT_SUBSCRIBE_FULL:
command_error(client, ACK_ERROR_EXIST,
"subscription list is full");
return COMMAND_RETURN_ERROR;
}
/* unreachable */
return COMMAND_RETURN_OK;
}
enum command_return
handle_unsubscribe(struct client *client, G_GNUC_UNUSED int argc, char *argv[])
{
assert(argc == 2);
if (client_unsubscribe(client, argv[1]))
return COMMAND_RETURN_OK;
else {
command_error(client, ACK_ERROR_NO_EXIST,
"not subscribed to this channel");
return COMMAND_RETURN_ERROR;
}
}
struct channels_context {
GStringChunk *chunk;
GHashTable *channels;
};
static void
collect_channels(gpointer data, gpointer user_data)
{
struct channels_context *context =
(struct channels_context *)user_data;
const struct client *client = (const struct client *)data;
for (GSList *i = client->subscriptions; i != NULL;
i = g_slist_next(i)) {
const char *channel = (const char *)i->data;
if (g_hash_table_lookup(context->channels, channel) == NULL) {
char *channel2 = g_string_chunk_insert(context->chunk,
channel);
g_hash_table_insert(context->channels, channel2,
context);
}
}
}
static void
print_channel(gpointer key, G_GNUC_UNUSED gpointer value, gpointer user_data)
{
struct client *client = (struct client *)user_data;
const char *channel = (const char *)key;
client_printf(client, "channel: %s\n", channel);
}
enum command_return
handle_channels(struct client *client,
G_GNUC_UNUSED int argc, G_GNUC_UNUSED char *argv[])
{
assert(argc == 1);
struct channels_context context = {
g_string_chunk_new(1024),
g_hash_table_new(g_str_hash, g_str_equal),
};
client_list_foreach(collect_channels, &context);
g_hash_table_foreach(context.channels, print_channel, client);
g_hash_table_destroy(context.channels);
g_string_chunk_free(context.chunk);
return COMMAND_RETURN_OK;
}
enum command_return
handle_read_messages(struct client *client,
G_GNUC_UNUSED int argc, G_GNUC_UNUSED char *argv[])
{
assert(argc == 1);
GSList *messages = client_read_messages(client);
for (GSList *i = messages; i != NULL; i = g_slist_next(i)) {
struct client_message *msg = (struct client_message *)i->data;
client_printf(client, "channel: %s\nmessage: %s\n",
msg->channel, msg->message);
client_message_free(msg);
}
g_slist_free(messages);
return COMMAND_RETURN_OK;
}
struct send_message_context {
struct client_message msg;
bool sent;
};
static void
send_message(gpointer data, gpointer user_data)
{
struct send_message_context *context =
(struct send_message_context *)user_data;
struct client *client = (struct client *)data;
if (client_push_message(client, &context->msg))
context->sent = true;
}
enum command_return
handle_send_message(struct client *client,
G_GNUC_UNUSED int argc, G_GNUC_UNUSED char *argv[])
{
assert(argc == 3);
if (!client_message_valid_channel_name(argv[1])) {
command_error(client, ACK_ERROR_ARG,
"invalid channel name");
return COMMAND_RETURN_ERROR;
}
struct send_message_context context;
context.sent = false;
client_message_init(&context.msg, argv[1], argv[2]);
client_list_foreach(send_message, &context);
client_message_deinit(&context.msg);
if (context.sent)
return COMMAND_RETURN_OK;
else {
command_error(client, ACK_ERROR_NO_EXIST,
"nobody is subscribed to this channel");
return COMMAND_RETURN_ERROR;
}
}
/*
* Copyright (C) 2003-2012 The Music Player Daemon Project
* 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.
*/
#ifndef MPD_MESSAGE_COMMANDS_HXX
#define MPD_MESSAGE_COMMANDS_HXX
#include "command.h"
G_BEGIN_DECLS
enum command_return
handle_subscribe(struct client *client, int argc, char *argv[]);
enum command_return
handle_unsubscribe(struct client *client, int argc, char *argv[]);
enum command_return
handle_channels(struct client *client, int argc, char *argv[]);
enum command_return
handle_read_messages(struct client *client, int argc, char *argv[]);
enum command_return
handle_send_message(struct client *client, int argc, char *argv[]);
G_END_DECLS
#endif
/*
* Copyright (C) 2003-2012 The Music Player Daemon Project
* 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 "OutputCommands.hxx"
extern "C" {
#include "protocol/argparser.h"
#include "protocol/result.h"
#include "output_command.h"
#include "output_print.h"
}
#include <string.h>
enum command_return
handle_enableoutput(struct client *client, G_GNUC_UNUSED int argc, char *argv[])
{
unsigned device;
bool ret;
if (!check_unsigned(client, &device, argv[1]))
return COMMAND_RETURN_ERROR;
ret = audio_output_enable_index(device);
if (!ret) {
command_error(client, ACK_ERROR_NO_EXIST,
"No such audio output");
return COMMAND_RETURN_ERROR;
}
return COMMAND_RETURN_OK;
}
enum command_return
handle_disableoutput(struct client *client, G_GNUC_UNUSED int argc, char *argv[])
{
unsigned device;
bool ret;
if (!check_unsigned(client, &device, argv[1]))
return COMMAND_RETURN_ERROR;
ret = audio_output_disable_index(device);
if (!ret) {
command_error(client, ACK_ERROR_NO_EXIST,
"No such audio output");
return COMMAND_RETURN_ERROR;
}
return COMMAND_RETURN_OK;
}
enum command_return
handle_devices(struct client *client,
G_GNUC_UNUSED int argc, G_GNUC_UNUSED char *argv[])
{
printAudioDevices(client);
return COMMAND_RETURN_OK;
}
/*
* Copyright (C) 2003-2012 The Music Player Daemon Project
* 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.
*/
#ifndef MPD_OUTPUT_COMMANDS_HXX
#define MPD_OUTPUT_COMMANDS_HXX
#include "command.h"
G_BEGIN_DECLS
enum command_return
handle_enableoutput(struct client *client, int argc, char *argv[]);
enum command_return
handle_disableoutput(struct client *client, int argc, char *argv[]);
enum command_return
handle_devices(struct client *client, int argc, char *argv[]);
G_END_DECLS
#endif
/*
* Copyright (C) 2003-2012 The Music Player Daemon Project
* 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.
*/
#ifndef MPD_PLAYER_COMMANDS_HXX
#define MPD_PLAYER_COMMANDS_HXX
#include "command.h"
G_BEGIN_DECLS
enum command_return
handle_play(struct client *client, int argc, char *argv[]);
enum command_return
handle_playid(struct client *client, int argc, char *argv[]);
enum command_return
handle_stop(struct client *client, int argc, char *argv[]);
enum command_return
handle_currentsong(struct client *client, int argc, char *argv[]);
enum command_return
handle_pause(struct client *client, int argc, char *argv[]);
enum command_return
handle_status(struct client *client, int argc, char *argv[]);
enum command_return
handle_next(struct client *client, int argc, char *argv[]);
enum command_return
handle_previous(struct client *client, int argc, char *avg[]);
enum command_return
handle_repeat(struct client *client, int argc, char *argv[]);
enum command_return
handle_single(struct client *client, int argc, char *argv[]);
enum command_return
handle_consume(struct client *client, int argc, char *argv[]);
enum command_return
handle_random(struct client *client, int argc, char *argv[]);
enum command_return
handle_clearerror(struct client *client, int argc, char *argv[]);
enum command_return
handle_seek(struct client *client, int argc, char *argv[]);
enum command_return
handle_seekid(struct client *client, int argc, char *argv[]);
enum command_return
handle_seekcur(struct client *client, int argc, char *argv[]);
enum command_return
handle_crossfade(struct client *client, int argc, char *argv[]);
enum command_return
handle_mixrampdb(struct client *client, int argc, char *argv[]);
enum command_return
handle_mixrampdelay(struct client *client, int argc, char *argv[]);
enum command_return
handle_replay_gain_mode(struct client *client, int argc, char *argv[]);
enum command_return
handle_replay_gain_status(struct client *client, int argc, char *argv[]);
G_END_DECLS
#endif
/*
* Copyright (C) 2003-2012 The Music Player Daemon Project
* 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 "PlaylistCommands.hxx"
#include "CommandError.h"
extern "C" {
#include "protocol/argparser.h"
#include "protocol/result.h"
#include "playlist.h"
#include "playlist_print.h"
#include "playlist_save.h"
#include "playlist_queue.h"
#include "time_print.h"
#include "ls.h"
#include "uri.h"
#include "stored_playlist.h"
#include "dbUtils.h"
#include "client_internal.h"
}
#include <assert.h>
#include <stdlib.h>
static void
print_spl_list(struct client *client, GPtrArray *list)
{
for (unsigned i = 0; i < list->len; ++i) {
struct stored_playlist_info *playlist =
(struct stored_playlist_info *)
g_ptr_array_index(list, i);
client_printf(client, "playlist: %s\n", playlist->name);
if (playlist->mtime > 0)
time_print(client, "Last-Modified", playlist->mtime);
}
}
enum command_return
handle_save(struct client *client, G_GNUC_UNUSED int argc, char *argv[])
{
enum playlist_result result;
result = spl_save_playlist(argv[1], &g_playlist);
return print_playlist_result(client, result);
}
enum command_return
handle_load(struct client *client, int argc, char *argv[])
{
unsigned start_index, end_index;
if (argc < 3) {
start_index = 0;
end_index = G_MAXUINT;
} else if (!check_range(client, &start_index, &end_index, argv[2]))
return COMMAND_RETURN_ERROR;
enum playlist_result result;
result = playlist_open_into_queue(argv[1],
start_index, end_index,
&g_playlist,
client->player_control, true);
if (result != PLAYLIST_RESULT_NO_SUCH_LIST)
return print_playlist_result(client, result);
GError *error = NULL;
if (playlist_load_spl(&g_playlist, client->player_control,
argv[1], start_index, end_index,
&error))
return COMMAND_RETURN_OK;
if (error->domain == playlist_quark() &&
error->code == PLAYLIST_RESULT_BAD_NAME)
/* the message for BAD_NAME is confusing when the
client wants to load a playlist file from the music
directory; patch the GError object to show "no such
playlist" instead */
error->code = PLAYLIST_RESULT_NO_SUCH_LIST;
return print_error(client, error);
}
enum command_return
handle_listplaylist(struct client *client, G_GNUC_UNUSED int argc, char *argv[])
{
if (playlist_file_print(client, argv[1], false))
return COMMAND_RETURN_OK;
GError *error = NULL;
return spl_print(client, argv[1], false, &error)
? COMMAND_RETURN_OK
: print_error(client, error);
}
enum command_return
handle_listplaylistinfo(struct client *client,
G_GNUC_UNUSED int argc, char *argv[])
{
if (playlist_file_print(client, argv[1], true))
return COMMAND_RETURN_OK;
GError *error = NULL;
return spl_print(client, argv[1], true, &error)
? COMMAND_RETURN_OK
: print_error(client, error);
}
enum command_return
handle_rm(struct client *client, G_GNUC_UNUSED int argc, char *argv[])
{
GError *error = NULL;
return spl_delete(argv[1], &error)
? COMMAND_RETURN_OK
: print_error(client, error);
}
enum command_return
handle_rename(struct client *client, G_GNUC_UNUSED int argc, char *argv[])
{
GError *error = NULL;
return spl_rename(argv[1], argv[2], &error)
? COMMAND_RETURN_OK
: print_error(client, error);
}
enum command_return
handle_playlistdelete(struct client *client,
G_GNUC_UNUSED int argc, char *argv[]) {
char *playlist = argv[1];
unsigned from;
if (!check_unsigned(client, &from, argv[2]))
return COMMAND_RETURN_ERROR;
GError *error = NULL;
return spl_remove_index(playlist, from, &error)
? COMMAND_RETURN_OK
: print_error(client, error);
}
enum command_return
handle_playlistmove(struct client *client, G_GNUC_UNUSED int argc, char *argv[])
{
char *playlist = argv[1];
unsigned from, to;
if (!check_unsigned(client, &from, argv[2]))
return COMMAND_RETURN_ERROR;
if (!check_unsigned(client, &to, argv[3]))
return COMMAND_RETURN_ERROR;
GError *error = NULL;
return spl_move_index(playlist, from, to, &error)
? COMMAND_RETURN_OK
: print_error(client, error);
}
enum command_return
handle_playlistclear(struct client *client, G_GNUC_UNUSED int argc, char *argv[])
{
GError *error = NULL;
return spl_clear(argv[1], &error)
? COMMAND_RETURN_OK
: print_error(client, error);
}
enum command_return
handle_playlistadd(struct client *client, G_GNUC_UNUSED int argc, char *argv[])
{
char *playlist = argv[1];
char *uri = argv[2];
bool success;
GError *error = NULL;
if (uri_has_scheme(uri)) {
if (!uri_supported_scheme(uri)) {
command_error(client, ACK_ERROR_NO_EXIST,
"unsupported URI scheme");
return COMMAND_RETURN_ERROR;
}
success = spl_append_uri(argv[1], playlist, &error);
} else
success = addAllInToStoredPlaylist(uri, playlist, &error);
if (!success && error == NULL) {
command_error(client, ACK_ERROR_NO_EXIST,
"directory or file not found");
return COMMAND_RETURN_ERROR;
}
return success ? COMMAND_RETURN_OK : print_error(client, error);
}
enum command_return
handle_listplaylists(struct client *client,
G_GNUC_UNUSED int argc, G_GNUC_UNUSED char *argv[])
{
GError *error = NULL;
GPtrArray *list = spl_list(&error);
if (list == NULL)
return print_error(client, error);
print_spl_list(client, list);
spl_list_free(list);
return COMMAND_RETURN_OK;
}
/*
* Copyright (C) 2003-2012 The Music Player Daemon Project
* 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.
*/
#ifndef MPD_PLAYLIST_COMMANDS_HXX
#define MPD_PLAYLIST_COMMANDS_HXX
#include "command.h"
G_BEGIN_DECLS
enum command_return
handle_save(struct client *client, int argc, char *argv[]);
enum command_return
handle_load(struct client *client, int argc, char *argv[]);
enum command_return
handle_listplaylist(struct client *client, int argc, char *argv[]);
enum command_return
handle_listplaylistinfo(struct client *client, int argc, char *argv[]);
enum command_return
handle_rm(struct client *client, int argc, char *argv[]);
enum command_return
handle_rename(struct client *client, int argc, char *argv[]);
enum command_return
handle_playlistdelete(struct client *client, int argc, char *argv[]);
enum command_return
handle_playlistmove(struct client *client, int argc, char *argv[]);
enum command_return
handle_playlistclear(struct client *client, int argc, char *argv[]);
enum command_return
handle_playlistadd(struct client *client, int argc, char *argv[]);
enum command_return
handle_listplaylists(struct client *client, int argc, char *argv[]);
G_END_DECLS
#endif
/*
* Copyright (C) 2003-2012 The Music Player Daemon Project
* 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.
*/
#ifndef MPD_QUEUE_COMMANDS_HXX
#define MPD_QUEUE_COMMANDS_HXX
#include "command.h"
G_BEGIN_DECLS
enum command_return
handle_add(struct client *client, int argc, char *argv[]);
enum command_return
handle_addid(struct client *client, int argc, char *argv[]);
enum command_return
handle_delete(struct client *client, int argc, char *argv[]);
enum command_return
handle_deleteid(struct client *client, int argc, char *argv[]);
enum command_return
handle_playlist(struct client *client, int argc, char *argv[]);
enum command_return
handle_shuffle(struct client *client, int argc, char *argv[]);
enum command_return
handle_clear(struct client *client, int argc, char *argv[]);
enum command_return
handle_plchanges(struct client *client, int argc, char *argv[]);
enum command_return
handle_plchangesposid(struct client *client, int argc, char *argv[]);
enum command_return
handle_playlistinfo(struct client *client, int argc, char *argv[]);
enum command_return
handle_playlistid(struct client *client, int argc, char *argv[]);
enum command_return
handle_playlistfind(struct client *client, int argc, char *argv[]);
enum command_return
handle_playlistsearch(struct client *client, int argc, char *argv[]);
enum command_return
handle_prio(struct client *client, int argc, char *argv[]);
enum command_return
handle_prioid(struct client *client, int argc, char *argv[]);
enum command_return
handle_move(struct client *client, int argc, char *argv[]);
enum command_return
handle_moveid(struct client *client, int argc, char *argv[]);
enum command_return
handle_swap(struct client *client, int argc, char *argv[]);
enum command_return
handle_swapid(struct client *client, int argc, char *argv[]);
G_END_DECLS
#endif
/*
* Copyright (C) 2003-2012 The Music Player Daemon Project
* 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"
extern "C" {
#include "protocol/result.h"
#include "sticker.h"
#include "sticker_print.h"
#include "song_print.h"
#include "song_sticker.h"
#include "database.h"
#include "db_lock.h"
}
#include <string.h>
struct sticker_song_find_data {
struct client *client;
const char *name;
};
static void
sticker_song_find_print_cb(struct song *song, const char *value,
gpointer user_data)
{
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 enum command_return
handle_sticker_song(struct client *client, int argc, char *argv[])
{
/* get song song_id key */
if (argc == 5 && strcmp(argv[1], "get") == 0) {
struct song *song;
char *value;
song = db_get_song(argv[3]);
if (song == NULL) {
command_error(client, ACK_ERROR_NO_EXIST,
"no such song");
return COMMAND_RETURN_ERROR;
}
value = sticker_song_get_value(song, argv[4]);
db_return_song(song);
if (value == NULL) {
command_error(client, ACK_ERROR_NO_EXIST,
"no such sticker");
return COMMAND_RETURN_ERROR;
}
sticker_print_value(client, argv[4], value);
g_free(value);
return COMMAND_RETURN_OK;
/* list song song_id */
} else if (argc == 4 && strcmp(argv[1], "list") == 0) {
struct song *song;
struct sticker *sticker;
song = db_get_song(argv[3]);
if (song == NULL) {
command_error(client, ACK_ERROR_NO_EXIST,
"no such song");
return COMMAND_RETURN_ERROR;
}
sticker = sticker_song_get(song);
db_return_song(song);
if (sticker) {
sticker_print(client, sticker);
sticker_free(sticker);
}
return COMMAND_RETURN_OK;
/* set song song_id id key */
} else if (argc == 6 && strcmp(argv[1], "set") == 0) {
struct song *song;
bool ret;
song = db_get_song(argv[3]);
if (song == NULL) {
command_error(client, ACK_ERROR_NO_EXIST,
"no such song");
return COMMAND_RETURN_ERROR;
}
ret = sticker_song_set_value(song, argv[4], argv[5]);
db_return_song(song);
if (!ret) {
command_error(client, ACK_ERROR_SYSTEM,
"failed to set sticker value");
return COMMAND_RETURN_ERROR;
}
return COMMAND_RETURN_OK;
/* delete song song_id [key] */
} else if ((argc == 4 || argc == 5) &&
strcmp(argv[1], "delete") == 0) {
struct song *song;
bool ret;
song = db_get_song(argv[3]);
if (song == NULL) {
command_error(client, ACK_ERROR_NO_EXIST,
"no such song");
return COMMAND_RETURN_ERROR;
}
ret = argc == 4
? sticker_song_delete(song)
: sticker_song_delete_value(song, argv[4]);
db_return_song(song);
if (!ret) {
command_error(client, ACK_ERROR_SYSTEM,
"no such sticker");
return COMMAND_RETURN_ERROR;
}
return COMMAND_RETURN_OK;
/* find song dir key */
} else if (argc == 5 && strcmp(argv[1], "find") == 0) {
/* "sticker find song a/directory name" */
struct directory *directory;
bool success;
struct sticker_song_find_data data = {
client,
argv[4],
};
db_lock();
directory = db_get_directory(argv[3]);
if (directory == NULL) {
db_unlock();
command_error(client, ACK_ERROR_NO_EXIST,
"no such directory");
return COMMAND_RETURN_ERROR;
}
success = sticker_song_find(directory, data.name,
sticker_song_find_print_cb, &data);
db_unlock();
if (!success) {
command_error(client, ACK_ERROR_SYSTEM,
"failed to set search sticker database");
return COMMAND_RETURN_ERROR;
}
return COMMAND_RETURN_OK;
} else {
command_error(client, ACK_ERROR_ARG, "bad request");
return COMMAND_RETURN_ERROR;
}
}
enum command_return
handle_sticker(struct client *client, int argc, char *argv[])
{
assert(argc >= 4);
if (!sticker_enabled()) {
command_error(client, ACK_ERROR_UNKNOWN,
"sticker database is disabled");
return COMMAND_RETURN_ERROR;
}
if (strcmp(argv[2], "song") == 0)
return handle_sticker_song(client, argc, argv);
else {
command_error(client, ACK_ERROR_ARG,
"unknown sticker domain");
return COMMAND_RETURN_ERROR;
}
}
/*
* Copyright (C) 2003-2012 The Music Player Daemon Project
* 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.
*/
#ifndef MPD_STICKER_COMMANDS_HXX
#define MPD_STICKER_COMMANDS_HXX
#include "command.h"
G_BEGIN_DECLS
enum command_return
handle_sticker(struct client *client, int argc, char *argv[]);
G_END_DECLS
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment