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 "PlaylistCommands.hxx"
|
2014-01-24 16:18:50 +01:00
|
|
|
#include "db/DatabasePlaylist.hxx"
|
2012-09-25 12:10:12 +02:00
|
|
|
#include "CommandError.hxx"
|
2012-08-29 20:17:13 +02:00
|
|
|
#include "PlaylistPrint.hxx"
|
2012-09-27 22:55:57 +02:00
|
|
|
#include "PlaylistSave.hxx"
|
2012-09-28 00:40:00 +02:00
|
|
|
#include "PlaylistFile.hxx"
|
2013-01-04 00:02:09 +01:00
|
|
|
#include "PlaylistVector.hxx"
|
2014-01-23 23:30:12 +01:00
|
|
|
#include "playlist/PlaylistQueue.hxx"
|
|
|
|
#include "playlist/Print.hxx"
|
2013-01-02 20:29:24 +01:00
|
|
|
#include "TimePrint.hxx"
|
2014-01-24 00:26:53 +01:00
|
|
|
#include "client/Client.hxx"
|
2013-01-03 10:33:04 +01:00
|
|
|
#include "protocol/ArgParser.hxx"
|
|
|
|
#include "protocol/Result.hxx"
|
2013-01-03 17:34:51 +01:00
|
|
|
#include "ls.hxx"
|
2013-01-04 20:50:00 +01:00
|
|
|
#include "Playlist.hxx"
|
2013-04-08 23:30:21 +02:00
|
|
|
#include "util/UriUtil.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Error.hxx"
|
2012-08-25 12:59:54 +02:00
|
|
|
|
|
|
|
static void
|
2013-10-19 18:48:38 +02:00
|
|
|
print_spl_list(Client &client, const PlaylistVector &list)
|
2012-08-25 12:59:54 +02:00
|
|
|
{
|
2012-09-27 23:48:29 +02:00
|
|
|
for (const auto &i : list) {
|
|
|
|
client_printf(client, "playlist: %s\n", i.name.c_str());
|
2012-08-25 12:59:54 +02:00
|
|
|
|
2012-09-27 23:48:29 +02:00
|
|
|
if (i.mtime > 0)
|
|
|
|
time_print(client, "Last-Modified", i.mtime);
|
2012-08-25 12:59:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
CommandResult
|
2013-10-19 18:48:38 +02:00
|
|
|
handle_save(Client &client, gcc_unused int argc, char *argv[])
|
2012-08-25 12:59:54 +02:00
|
|
|
{
|
2013-10-19 19:50:54 +02:00
|
|
|
PlaylistResult result = spl_save_playlist(argv[1], client.playlist);
|
2012-08-25 12:59:54 +02:00
|
|
|
return print_playlist_result(client, result);
|
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
CommandResult
|
2013-10-19 18:48:38 +02:00
|
|
|
handle_load(Client &client, int argc, char *argv[])
|
2012-08-25 12:59:54 +02:00
|
|
|
{
|
|
|
|
unsigned start_index, end_index;
|
|
|
|
|
|
|
|
if (argc < 3) {
|
|
|
|
start_index = 0;
|
2013-08-10 18:02:44 +02:00
|
|
|
end_index = unsigned(-1);
|
2012-08-25 12:59:54 +02:00
|
|
|
} else if (!check_range(client, &start_index, &end_index, argv[2]))
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::ERROR;
|
2012-08-25 12:59:54 +02:00
|
|
|
|
2013-10-19 19:50:54 +02:00
|
|
|
const PlaylistResult result =
|
|
|
|
playlist_open_into_queue(argv[1],
|
|
|
|
start_index, end_index,
|
|
|
|
client.playlist,
|
|
|
|
client.player_control, true);
|
|
|
|
if (result != PlaylistResult::NO_SUCH_LIST)
|
2012-08-25 12:59:54 +02:00
|
|
|
return print_playlist_result(client, result);
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
Error error;
|
2013-10-19 18:48:38 +02:00
|
|
|
if (playlist_load_spl(client.playlist, client.player_control,
|
2012-08-25 12:59:54 +02:00
|
|
|
argv[1], start_index, end_index,
|
2013-08-10 18:02:44 +02:00
|
|
|
error))
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::OK;
|
2012-08-25 12:59:54 +02:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
if (error.IsDomain(playlist_domain) &&
|
2013-10-19 19:50:54 +02:00
|
|
|
PlaylistResult(error.GetCode()) == PlaylistResult::BAD_NAME) {
|
2012-08-25 12:59:54 +02:00
|
|
|
/* the message for BAD_NAME is confusing when the
|
|
|
|
client wants to load a playlist file from the music
|
2013-08-10 18:02:44 +02:00
|
|
|
directory; patch the Error object to show "no such
|
2012-08-25 12:59:54 +02:00
|
|
|
playlist" instead */
|
2013-10-19 19:50:54 +02:00
|
|
|
Error error2(playlist_domain, int(PlaylistResult::NO_SUCH_LIST),
|
2013-08-10 18:02:44 +02:00
|
|
|
error.GetMessage());
|
|
|
|
error = std::move(error2);
|
|
|
|
}
|
2012-08-25 12:59:54 +02:00
|
|
|
|
|
|
|
return print_error(client, error);
|
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
CommandResult
|
2013-10-19 18:48:38 +02:00
|
|
|
handle_listplaylist(Client &client, gcc_unused int argc, char *argv[])
|
2012-08-25 12:59:54 +02:00
|
|
|
{
|
|
|
|
if (playlist_file_print(client, argv[1], false))
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::OK;
|
2012-08-25 12:59:54 +02:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
Error error;
|
|
|
|
return spl_print(client, argv[1], false, error)
|
2013-10-20 13:10:54 +02:00
|
|
|
? CommandResult::OK
|
2012-08-25 12:59:54 +02:00
|
|
|
: print_error(client, error);
|
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
CommandResult
|
2013-10-19 18:48:38 +02:00
|
|
|
handle_listplaylistinfo(Client &client,
|
2013-08-04 23:48:01 +02:00
|
|
|
gcc_unused int argc, char *argv[])
|
2012-08-25 12:59:54 +02:00
|
|
|
{
|
|
|
|
if (playlist_file_print(client, argv[1], true))
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::OK;
|
2012-08-25 12:59:54 +02:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
Error error;
|
|
|
|
return spl_print(client, argv[1], true, error)
|
2013-10-20 13:10:54 +02:00
|
|
|
? CommandResult::OK
|
2012-08-25 12:59:54 +02:00
|
|
|
: print_error(client, error);
|
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
CommandResult
|
2013-10-19 18:48:38 +02:00
|
|
|
handle_rm(Client &client, gcc_unused int argc, char *argv[])
|
2012-08-25 12:59:54 +02:00
|
|
|
{
|
2013-08-10 18:02:44 +02:00
|
|
|
Error error;
|
|
|
|
return spl_delete(argv[1], error)
|
2013-10-20 13:10:54 +02:00
|
|
|
? CommandResult::OK
|
2012-08-25 12:59:54 +02:00
|
|
|
: print_error(client, error);
|
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
CommandResult
|
2013-10-19 18:48:38 +02:00
|
|
|
handle_rename(Client &client, gcc_unused int argc, char *argv[])
|
2012-08-25 12:59:54 +02:00
|
|
|
{
|
2013-08-10 18:02:44 +02:00
|
|
|
Error error;
|
|
|
|
return spl_rename(argv[1], argv[2], error)
|
2013-10-20 13:10:54 +02:00
|
|
|
? CommandResult::OK
|
2012-08-25 12:59:54 +02:00
|
|
|
: print_error(client, error);
|
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
CommandResult
|
2013-10-19 18:48:38 +02:00
|
|
|
handle_playlistdelete(Client &client,
|
2013-08-04 23:48:01 +02:00
|
|
|
gcc_unused int argc, char *argv[]) {
|
2012-08-25 12:59:54 +02:00
|
|
|
char *playlist = argv[1];
|
|
|
|
unsigned from;
|
|
|
|
|
|
|
|
if (!check_unsigned(client, &from, argv[2]))
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::ERROR;
|
2012-08-25 12:59:54 +02:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
Error error;
|
|
|
|
return spl_remove_index(playlist, from, error)
|
2013-10-20 13:10:54 +02:00
|
|
|
? CommandResult::OK
|
2012-08-25 12:59:54 +02:00
|
|
|
: print_error(client, error);
|
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
CommandResult
|
2013-10-19 18:48:38 +02:00
|
|
|
handle_playlistmove(Client &client, gcc_unused int argc, char *argv[])
|
2012-08-25 12:59:54 +02:00
|
|
|
{
|
|
|
|
char *playlist = argv[1];
|
|
|
|
unsigned from, to;
|
|
|
|
|
|
|
|
if (!check_unsigned(client, &from, argv[2]))
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::ERROR;
|
2012-08-25 12:59:54 +02:00
|
|
|
if (!check_unsigned(client, &to, argv[3]))
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::ERROR;
|
2012-08-25 12:59:54 +02:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
Error error;
|
|
|
|
return spl_move_index(playlist, from, to, error)
|
2013-10-20 13:10:54 +02:00
|
|
|
? CommandResult::OK
|
2012-08-25 12:59:54 +02:00
|
|
|
: print_error(client, error);
|
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
CommandResult
|
2013-10-19 18:48:38 +02:00
|
|
|
handle_playlistclear(Client &client, gcc_unused int argc, char *argv[])
|
2012-08-25 12:59:54 +02:00
|
|
|
{
|
2013-08-10 18:02:44 +02:00
|
|
|
Error error;
|
|
|
|
return spl_clear(argv[1], error)
|
2013-10-20 13:10:54 +02:00
|
|
|
? CommandResult::OK
|
2012-08-25 12:59:54 +02:00
|
|
|
: print_error(client, error);
|
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
CommandResult
|
2013-10-19 18:48:38 +02:00
|
|
|
handle_playlistadd(Client &client, gcc_unused int argc, char *argv[])
|
2012-08-25 12:59:54 +02:00
|
|
|
{
|
|
|
|
char *playlist = argv[1];
|
|
|
|
char *uri = argv[2];
|
|
|
|
|
|
|
|
bool success;
|
2013-08-10 18:02:44 +02:00
|
|
|
Error error;
|
2012-08-25 12:59:54 +02:00
|
|
|
if (uri_has_scheme(uri)) {
|
|
|
|
if (!uri_supported_scheme(uri)) {
|
|
|
|
command_error(client, ACK_ERROR_NO_EXIST,
|
|
|
|
"unsupported URI scheme");
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::ERROR;
|
2012-08-25 12:59:54 +02:00
|
|
|
}
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
success = spl_append_uri(uri, playlist, error);
|
2012-08-25 12:59:54 +02:00
|
|
|
} else
|
2012-08-29 18:55:49 +02:00
|
|
|
success = search_add_to_playlist(uri, playlist, nullptr,
|
2013-08-10 18:02:44 +02:00
|
|
|
error);
|
2012-08-25 12:59:54 +02:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
if (!success && !error.IsDefined()) {
|
2012-08-25 12:59:54 +02:00
|
|
|
command_error(client, ACK_ERROR_NO_EXIST,
|
|
|
|
"directory or file not found");
|
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 success ? CommandResult::OK : print_error(client, error);
|
2012-08-25 12:59:54 +02:00
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
CommandResult
|
2013-10-19 18:48:38 +02:00
|
|
|
handle_listplaylists(Client &client,
|
2013-08-04 23:48:01 +02:00
|
|
|
gcc_unused int argc, gcc_unused char *argv[])
|
2012-08-25 12:59:54 +02:00
|
|
|
{
|
2013-08-10 18:02:44 +02:00
|
|
|
Error error;
|
|
|
|
const auto list = ListPlaylistFiles(error);
|
|
|
|
if (list.empty() && error.IsDefined())
|
2012-08-25 12:59:54 +02:00
|
|
|
return print_error(client, error);
|
|
|
|
|
|
|
|
print_spl_list(client, list);
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::OK;
|
2012-08-25 12:59:54 +02:00
|
|
|
}
|