mpd/src/command/PlaylistCommands.cxx

204 lines
5.0 KiB
C++
Raw Normal View History

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 "PlaylistCommands.hxx"
#include "Request.hxx"
2014-01-24 16:18:50 +01:00
#include "db/DatabasePlaylist.hxx"
#include "CommandError.hxx"
#include "PlaylistPrint.hxx"
#include "PlaylistSave.hxx"
2012-09-28 00:40:00 +02:00
#include "PlaylistFile.hxx"
2014-01-27 11:05:21 +01:00
#include "db/PlaylistVector.hxx"
#include "SongLoader.hxx"
#include "BulkEdit.hxx"
2014-01-23 23:30:12 +01:00
#include "playlist/PlaylistQueue.hxx"
#include "playlist/Print.hxx"
2014-02-27 17:12:42 +01:00
#include "queue/Playlist.hxx"
2013-01-02 20:29:24 +01:00
#include "TimePrint.hxx"
2014-01-24 00:26:53 +01:00
#include "client/Client.hxx"
#include "client/Response.hxx"
2013-01-03 17:34:51 +01:00
#include "ls.hxx"
#include "Mapper.hxx"
#include "fs/AllocatedPath.hxx"
2013-04-08 23:30:21 +02:00
#include "util/UriUtil.hxx"
#include "util/Error.hxx"
#include "util/ConstBuffer.hxx"
2012-08-25 12:59:54 +02:00
bool
playlist_commands_available()
{
return !map_spl_path().IsNull();
}
2012-08-25 12:59:54 +02:00
static void
print_spl_list(Response &r, const PlaylistVector &list)
2012-08-25 12:59:54 +02:00
{
for (const auto &i : list) {
r.Format("playlist: %s\n", i.name.c_str());
2012-08-25 12:59:54 +02:00
if (i.mtime > 0)
time_print(r, "Last-Modified", i.mtime);
2012-08-25 12:59:54 +02:00
}
}
CommandResult
handle_save(Client &client, Request args, gcc_unused Response &r)
2012-08-25 12:59:54 +02:00
{
spl_save_playlist(args.front(), client.playlist);
return CommandResult::OK;
2012-08-25 12:59:54 +02:00
}
CommandResult
handle_load(Client &client, Request args, Response &r)
2012-08-25 12:59:54 +02:00
{
RangeArg range = args.ParseOptional(1, RangeArg::All());
2012-08-25 12:59:54 +02:00
const ScopeBulkEdit bulk_edit(client.partition);
Error error;
const SongLoader loader(client);
if (!playlist_open_into_queue(args.front(),
range.start, range.end,
client.playlist,
client.player_control, loader, error))
return print_error(r, error);
2012-08-25 12:59:54 +02:00
return CommandResult::OK;
2012-08-25 12:59:54 +02:00
}
CommandResult
handle_listplaylist(Client &client, Request args, Response &r)
2012-08-25 12:59:54 +02:00
{
const char *const name = args.front();
if (playlist_file_print(r, client.partition, SongLoader(client),
name, false))
return CommandResult::OK;
2012-08-25 12:59:54 +02:00
spl_print(r, client.partition, name, false);
return CommandResult::OK;
2012-08-25 12:59:54 +02:00
}
CommandResult
handle_listplaylistinfo(Client &client, Request args, Response &r)
2012-08-25 12:59:54 +02:00
{
const char *const name = args.front();
if (playlist_file_print(r, client.partition, SongLoader(client),
name, true))
return CommandResult::OK;
2012-08-25 12:59:54 +02:00
spl_print(r, client.partition, name, true);
return CommandResult::OK;
2012-08-25 12:59:54 +02:00
}
CommandResult
handle_rm(gcc_unused Client &client, Request args, gcc_unused Response &r)
2012-08-25 12:59:54 +02:00
{
const char *const name = args.front();
spl_delete(name);
return CommandResult::OK;
2012-08-25 12:59:54 +02:00
}
CommandResult
handle_rename(gcc_unused Client &client, Request args, gcc_unused Response &r)
2012-08-25 12:59:54 +02:00
{
const char *const old_name = args[0];
const char *const new_name = args[1];
spl_rename(old_name, new_name);
return CommandResult::OK;
2012-08-25 12:59:54 +02:00
}
CommandResult
handle_playlistdelete(gcc_unused Client &client,
Request args, gcc_unused Response &r)
{
const char *const name = args[0];
unsigned from = args.ParseUnsigned(1);
2012-08-25 12:59:54 +02:00
spl_remove_index(name, from);
return CommandResult::OK;
2012-08-25 12:59:54 +02:00
}
CommandResult
handle_playlistmove(gcc_unused Client &client,
Request args, gcc_unused Response &r)
2012-08-25 12:59:54 +02:00
{
const char *const name = args.front();
unsigned from = args.ParseUnsigned(1);
unsigned to = args.ParseUnsigned(2);
2012-08-25 12:59:54 +02:00
spl_move_index(name, from, to);
return CommandResult::OK;
2012-08-25 12:59:54 +02:00
}
CommandResult
handle_playlistclear(gcc_unused Client &client,
Request args, gcc_unused Response &r)
2012-08-25 12:59:54 +02:00
{
const char *const name = args.front();
spl_clear(name);
return CommandResult::OK;
2012-08-25 12:59:54 +02:00
}
CommandResult
handle_playlistadd(Client &client, Request args, Response &r)
2012-08-25 12:59:54 +02:00
{
const char *const playlist = args[0];
const char *const uri = args[1];
2012-08-25 12:59:54 +02:00
bool success;
Error error;
2012-08-25 12:59:54 +02:00
if (uri_has_scheme(uri)) {
2014-02-03 23:30:34 +01:00
const SongLoader loader(client);
success = spl_append_uri(playlist, loader, uri, error);
} else {
#ifdef ENABLE_DATABASE
const Database *db = client.GetDatabase(error);
if (db == nullptr)
return print_error(r, error);
success = search_add_to_playlist(*db, *client.GetStorage(),
uri, playlist, nullptr,
error);
#else
success = false;
#endif
}
2012-08-25 12:59:54 +02:00
if (!success && !error.IsDefined()) {
r.Error(ACK_ERROR_NO_EXIST, "directory or file not found");
return CommandResult::ERROR;
2012-08-25 12:59:54 +02:00
}
return success ? CommandResult::OK : print_error(r, error);
2012-08-25 12:59:54 +02:00
}
CommandResult
handle_listplaylists(gcc_unused Client &client, gcc_unused Request args,
Response &r)
2012-08-25 12:59:54 +02:00
{
print_spl_list(r, ListPlaylistFiles());
return CommandResult::OK;
2012-08-25 12:59:54 +02:00
}