StickerCommands.cxx 4.71 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2013 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 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"
Max Kellermann's avatar
Max Kellermann committed
22
#include "SongPrint.hxx"
Max Kellermann's avatar
Max Kellermann committed
23
#include "DatabaseLock.hxx"
24 25
#include "DatabasePlugin.hxx"
#include "DatabaseGlue.hxx"
26
#include "DatabaseSimple.hxx"
Max Kellermann's avatar
Max Kellermann committed
27 28 29
#include "SongSticker.hxx"
#include "StickerPrint.hxx"
#include "StickerDatabase.hxx"
30
#include "CommandError.hxx"
Max Kellermann's avatar
Max Kellermann committed
31
#include "protocol/Result.hxx"
32 33 34
#include "util/Error.hxx"

#include <glib.h>
35 36 37 38

#include <string.h>

struct sticker_song_find_data {
39
	Client &client;
40 41 42 43
	const char *name;
};

static void
44
sticker_song_find_print_cb(Song &song, const char *value,
45
			   void *user_data)
46 47 48 49 50 51 52 53
{
	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);
}

54
static CommandResult
55
handle_sticker_song(Client &client, int argc, char *argv[])
56
{
57 58
	Error error;
	const Database *db = GetDatabase(error);
59 60 61
	if (db == nullptr)
		return print_error(client, error);

62 63
	/* get song song_id key */
	if (argc == 5 && strcmp(argv[1], "get") == 0) {
64
		Song *song = db->GetSong(argv[3], error);
65 66
		if (song == nullptr)
			return print_error(client, error);
67

68
		const auto value = sticker_song_get_value(song, argv[4]);
69
		db->ReturnSong(song);
70
		if (value.empty()) {
71 72
			command_error(client, ACK_ERROR_NO_EXIST,
				      "no such sticker");
73
			return CommandResult::ERROR;
74 75
		}

76
		sticker_print_value(client, argv[4], value.c_str());
77

78
		return CommandResult::OK;
79 80
	/* list song song_id */
	} else if (argc == 4 && strcmp(argv[1], "list") == 0) {
81
		Song *song = db->GetSong(argv[3], error);
82 83
		if (song == nullptr)
			return print_error(client, error);
84

85 86
		sticker *sticker = sticker_song_get(song);
		db->ReturnSong(song);
87
		if (sticker) {
88
			sticker_print(client, *sticker);
89 90 91
			sticker_free(sticker);
		}

92
		return CommandResult::OK;
93 94
	/* set song song_id id key */
	} else if (argc == 6 && strcmp(argv[1], "set") == 0) {
95
		Song *song = db->GetSong(argv[3], error);
96 97
		if (song == nullptr)
			return print_error(client, error);
98

99 100
		bool ret = sticker_song_set_value(song, argv[4], argv[5]);
		db->ReturnSong(song);
101 102 103
		if (!ret) {
			command_error(client, ACK_ERROR_SYSTEM,
				      "failed to set sticker value");
104
			return CommandResult::ERROR;
105 106
		}

107
		return CommandResult::OK;
108 109 110
	/* delete song song_id [key] */
	} else if ((argc == 4 || argc == 5) &&
		   strcmp(argv[1], "delete") == 0) {
111
		Song *song = db->GetSong(argv[3], error);
112 113
		if (song == nullptr)
			return print_error(client, error);
114

115
		bool ret = argc == 4
116 117
			? sticker_song_delete(song)
			: sticker_song_delete_value(song, argv[4]);
118
		db->ReturnSong(song);
119 120 121
		if (!ret) {
			command_error(client, ACK_ERROR_SYSTEM,
				      "no such sticker");
122
			return CommandResult::ERROR;
123 124
		}

125
		return CommandResult::OK;
126 127 128 129 130 131 132 133 134 135
	/* find song dir key */
	} else if (argc == 5 && strcmp(argv[1], "find") == 0) {
		/* "sticker find song a/directory name" */
		bool success;
		struct sticker_song_find_data data = {
			client,
			argv[4],
		};

		db_lock();
136
		Directory *directory = db_get_directory(argv[3]);
137
		if (directory == nullptr) {
138 139 140
			db_unlock();
			command_error(client, ACK_ERROR_NO_EXIST,
				      "no such directory");
141
			return CommandResult::ERROR;
142 143
		}

144
		success = sticker_song_find(*directory, data.name,
145 146 147 148 149
					    sticker_song_find_print_cb, &data);
		db_unlock();
		if (!success) {
			command_error(client, ACK_ERROR_SYSTEM,
				      "failed to set search sticker database");
150
			return CommandResult::ERROR;
151 152
		}

153
		return CommandResult::OK;
154 155
	} else {
		command_error(client, ACK_ERROR_ARG, "bad request");
156
		return CommandResult::ERROR;
157 158 159
	}
}

160
CommandResult
161
handle_sticker(Client &client, int argc, char *argv[])
162 163 164 165 166 167
{
	assert(argc >= 4);

	if (!sticker_enabled()) {
		command_error(client, ACK_ERROR_UNKNOWN,
			      "sticker database is disabled");
168
		return CommandResult::ERROR;
169 170 171 172 173 174 175
	}

	if (strcmp(argv[2], "song") == 0)
		return handle_sticker_song(client, argc, argv);
	else {
		command_error(client, ACK_ERROR_ARG,
			      "unknown sticker domain");
176
		return CommandResult::ERROR;
177 178
	}
}