2012-08-25 12:59:54 +02:00
|
|
|
/*
|
2022-07-14 17:58:12 +02:00
|
|
|
* Copyright 2003-2022 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"
|
2021-10-18 22:07:25 +02:00
|
|
|
#include "PositionArg.hxx"
|
2015-08-11 22:11:28 +02:00
|
|
|
#include "Request.hxx"
|
2019-03-29 17:01:31 +01:00
|
|
|
#include "Instance.hxx"
|
2021-10-25 08:43:42 +02:00
|
|
|
#include "db/Interface.hxx"
|
2019-03-25 18:48:31 +01:00
|
|
|
#include "db/Selection.hxx"
|
2014-01-24 16:18:50 +01:00
|
|
|
#include "db/DatabasePlaylist.hxx"
|
2021-10-25 08:43:42 +02:00
|
|
|
#include "db/DatabaseSong.hxx"
|
2012-09-27 22:55:57 +02:00
|
|
|
#include "PlaylistSave.hxx"
|
2012-09-28 00:40:00 +02:00
|
|
|
#include "PlaylistFile.hxx"
|
2016-04-13 19:47:41 +02:00
|
|
|
#include "PlaylistError.hxx"
|
2014-01-27 11:05:21 +01:00
|
|
|
#include "db/PlaylistVector.hxx"
|
2014-02-02 14:37:52 +01:00
|
|
|
#include "SongLoader.hxx"
|
2019-03-29 17:01:31 +01:00
|
|
|
#include "song/DetachedSong.hxx"
|
2014-07-11 20:01:53 +02:00
|
|
|
#include "BulkEdit.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"
|
2015-08-06 22:10:25 +02:00
|
|
|
#include "client/Response.hxx"
|
2014-10-25 23:49:35 +02:00
|
|
|
#include "Mapper.hxx"
|
|
|
|
#include "fs/AllocatedPath.hxx"
|
2019-05-08 15:47:58 +02:00
|
|
|
#include "time/ChronoUtil.hxx"
|
2019-08-09 15:54:13 +02:00
|
|
|
#include "util/UriExtract.hxx"
|
2019-02-13 21:55:15 +01:00
|
|
|
#include "LocateUri.hxx"
|
2012-08-25 12:59:54 +02:00
|
|
|
|
2021-05-21 20:35:29 +02:00
|
|
|
#include <fmt/format.h>
|
|
|
|
|
2014-10-25 23:49:35 +02:00
|
|
|
bool
|
2017-05-08 14:44:49 +02:00
|
|
|
playlist_commands_available() noexcept
|
2014-10-25 23:49:35 +02:00
|
|
|
{
|
|
|
|
return !map_spl_path().IsNull();
|
|
|
|
}
|
|
|
|
|
2012-08-25 12:59:54 +02:00
|
|
|
static void
|
2015-08-06 22:10:25 +02:00
|
|
|
print_spl_list(Response &r, const PlaylistVector &list)
|
2012-08-25 12:59:54 +02:00
|
|
|
{
|
2012-09-27 23:48:29 +02:00
|
|
|
for (const auto &i : list) {
|
2021-05-21 20:35:29 +02:00
|
|
|
r.Fmt(FMT_STRING("playlist: {}\n"), i.name);
|
2012-08-25 12:59:54 +02:00
|
|
|
|
2017-02-11 23:09:33 +01:00
|
|
|
if (!IsNegative(i.mtime))
|
2015-08-06 22:10:25 +02:00
|
|
|
time_print(r, "Last-Modified", i.mtime);
|
2012-08-25 12:59:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
CommandResult
|
2020-03-12 20:56:11 +01:00
|
|
|
handle_save(Client &client, Request args, [[maybe_unused]] Response &r)
|
2012-08-25 12:59:54 +02:00
|
|
|
{
|
2022-10-02 10:19:26 +02:00
|
|
|
PlaylistSaveMode mode = PlaylistSaveMode::CREATE;
|
|
|
|
|
|
|
|
const char *mode_arg = args.GetOptional(1);
|
|
|
|
if (mode_arg != nullptr) {
|
|
|
|
if (StringIsEqual(mode_arg, "create"))
|
|
|
|
mode = PlaylistSaveMode::CREATE;
|
|
|
|
else if (StringIsEqual(mode_arg, "append"))
|
|
|
|
mode = PlaylistSaveMode::APPEND;
|
|
|
|
else if (StringIsEqual(mode_arg, "replace"))
|
|
|
|
mode = PlaylistSaveMode::REPLACE;
|
|
|
|
else
|
|
|
|
throw std::invalid_argument("Unrecognized save mode, expected one of 'create', 'append', 'replace'");
|
|
|
|
}
|
|
|
|
|
|
|
|
spl_save_playlist(args.front(), mode, client.GetPlaylist());
|
|
|
|
|
2015-12-26 06:30:25 +01:00
|
|
|
return CommandResult::OK;
|
2012-08-25 12:59:54 +02:00
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
CommandResult
|
2020-03-12 20:56:11 +01:00
|
|
|
handle_load(Client &client, Request args, [[maybe_unused]] Response &r)
|
2012-08-25 12:59:54 +02:00
|
|
|
{
|
2019-04-18 10:03:15 +02:00
|
|
|
const auto uri = LocateUri(UriPluginKind::PLAYLIST, args.front(),
|
|
|
|
&client
|
2019-02-13 21:55:15 +01:00
|
|
|
#ifdef ENABLE_DATABASE
|
|
|
|
, nullptr
|
|
|
|
#endif
|
|
|
|
);
|
2015-12-18 09:50:48 +01:00
|
|
|
RangeArg range = args.ParseOptional(1, RangeArg::All());
|
2012-08-25 12:59:54 +02:00
|
|
|
|
2021-10-14 15:11:04 +02:00
|
|
|
auto &partition = client.GetPartition();
|
|
|
|
const ScopeBulkEdit bulk_edit(partition);
|
2014-07-11 20:01:53 +02:00
|
|
|
|
2019-03-29 17:01:31 +01:00
|
|
|
auto &playlist = client.GetPlaylist();
|
|
|
|
const unsigned old_size = playlist.GetLength();
|
|
|
|
|
2022-07-04 18:38:57 +02:00
|
|
|
const unsigned position = args.size() > 2
|
2021-10-18 22:07:25 +02:00
|
|
|
? ParseInsertPosition(args[2], partition.playlist)
|
2021-10-14 15:11:04 +02:00
|
|
|
: old_size;
|
|
|
|
|
2014-02-02 14:37:52 +01:00
|
|
|
const SongLoader loader(client);
|
2019-02-13 21:55:15 +01:00
|
|
|
playlist_open_into_queue(uri,
|
2016-02-28 10:51:07 +01:00
|
|
|
range.start, range.end,
|
2019-03-29 17:01:31 +01:00
|
|
|
playlist,
|
2017-02-20 12:44:09 +01:00
|
|
|
client.GetPlayerControl(), loader);
|
2019-03-29 17:01:31 +01:00
|
|
|
|
|
|
|
/* invoke the RemoteTagScanner on all newly added songs */
|
|
|
|
auto &instance = client.GetInstance();
|
|
|
|
const unsigned new_size = playlist.GetLength();
|
|
|
|
for (unsigned i = old_size; i < new_size; ++i)
|
2021-05-18 18:45:07 +02:00
|
|
|
instance.LookupRemoteTag(playlist.queue.Get(i).GetRealURI());
|
2019-03-29 17:01:31 +01:00
|
|
|
|
2021-10-14 15:11:04 +02:00
|
|
|
if (position < old_size) {
|
|
|
|
const RangeArg move_range{old_size, new_size};
|
|
|
|
|
|
|
|
try {
|
|
|
|
partition.MoveRange(move_range, position);
|
|
|
|
} catch (...) {
|
|
|
|
/* ignore - shall we handle it? */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-10 19:02:33 +02:00
|
|
|
return CommandResult::OK;
|
2012-08-25 12:59:54 +02:00
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
CommandResult
|
2015-08-13 12:48:31 +02:00
|
|
|
handle_listplaylist(Client &client, Request args, Response &r)
|
2012-08-25 12:59:54 +02:00
|
|
|
{
|
2019-04-18 10:03:15 +02:00
|
|
|
const auto name = LocateUri(UriPluginKind::PLAYLIST, args.front(),
|
|
|
|
&client
|
2019-02-13 21:55:15 +01:00
|
|
|
#ifdef ENABLE_DATABASE
|
|
|
|
, nullptr
|
|
|
|
#endif
|
|
|
|
);
|
2014-12-06 00:08:08 +01:00
|
|
|
|
2017-02-25 09:58:58 +01:00
|
|
|
if (playlist_file_print(r, client.GetPartition(), SongLoader(client),
|
|
|
|
name, false))
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::OK;
|
2012-08-25 12:59:54 +02:00
|
|
|
|
2016-04-13 19:47:41 +02:00
|
|
|
throw PlaylistError::NoSuchList();
|
2012-08-25 12:59:54 +02:00
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
CommandResult
|
2015-08-13 12:48:31 +02:00
|
|
|
handle_listplaylistinfo(Client &client, Request args, Response &r)
|
2012-08-25 12:59:54 +02:00
|
|
|
{
|
2019-04-18 10:03:15 +02:00
|
|
|
const auto name = LocateUri(UriPluginKind::PLAYLIST, args.front(),
|
|
|
|
&client
|
2019-02-13 21:55:15 +01:00
|
|
|
#ifdef ENABLE_DATABASE
|
|
|
|
, nullptr
|
|
|
|
#endif
|
|
|
|
);
|
2014-12-06 00:08:08 +01:00
|
|
|
|
2017-02-25 09:58:58 +01:00
|
|
|
if (playlist_file_print(r, client.GetPartition(), SongLoader(client),
|
2015-08-06 22:10:25 +02:00
|
|
|
name, true))
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::OK;
|
2012-08-25 12:59:54 +02:00
|
|
|
|
2016-04-13 19:47:41 +02:00
|
|
|
throw PlaylistError::NoSuchList();
|
2012-08-25 12:59:54 +02:00
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
CommandResult
|
2020-03-12 20:56:11 +01:00
|
|
|
handle_rm([[maybe_unused]] Client &client, Request args, [[maybe_unused]] Response &r)
|
2012-08-25 12:59:54 +02:00
|
|
|
{
|
2014-12-06 00:08:08 +01:00
|
|
|
const char *const name = args.front();
|
|
|
|
|
2015-12-28 07:22:10 +01:00
|
|
|
spl_delete(name);
|
|
|
|
return CommandResult::OK;
|
2012-08-25 12:59:54 +02:00
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
CommandResult
|
2020-03-12 20:56:11 +01:00
|
|
|
handle_rename([[maybe_unused]] Client &client, Request args, [[maybe_unused]] Response &r)
|
2012-08-25 12:59:54 +02:00
|
|
|
{
|
2014-12-06 00:08:08 +01:00
|
|
|
const char *const old_name = args[0];
|
|
|
|
const char *const new_name = args[1];
|
|
|
|
|
2015-12-28 07:22:10 +01:00
|
|
|
spl_rename(old_name, new_name);
|
|
|
|
return CommandResult::OK;
|
2012-08-25 12:59:54 +02:00
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
CommandResult
|
2020-03-12 20:56:11 +01:00
|
|
|
handle_playlistdelete([[maybe_unused]] Client &client,
|
|
|
|
Request args, [[maybe_unused]] Response &r)
|
2014-12-06 00:08:08 +01:00
|
|
|
{
|
|
|
|
const char *const name = args[0];
|
2021-10-25 12:16:20 +02:00
|
|
|
const auto range = args.ParseRange(1);
|
2012-08-25 12:59:54 +02:00
|
|
|
|
2021-10-25 12:11:45 +02:00
|
|
|
PlaylistFileEditor editor(name, PlaylistFileEditor::LoadMode::YES);
|
2021-10-25 12:16:20 +02:00
|
|
|
editor.RemoveRange(range);
|
2021-10-25 12:11:45 +02:00
|
|
|
editor.Save();
|
2015-12-26 06:30:25 +01:00
|
|
|
return CommandResult::OK;
|
2012-08-25 12:59:54 +02:00
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
CommandResult
|
2020-03-12 20:56:11 +01:00
|
|
|
handle_playlistmove([[maybe_unused]] Client &client,
|
|
|
|
Request args, [[maybe_unused]] Response &r)
|
2012-08-25 12:59:54 +02:00
|
|
|
{
|
2014-12-06 00:08:08 +01:00
|
|
|
const char *const name = args.front();
|
2015-12-18 09:50:48 +01:00
|
|
|
unsigned from = args.ParseUnsigned(1);
|
|
|
|
unsigned to = args.ParseUnsigned(2);
|
2012-08-25 12:59:54 +02:00
|
|
|
|
2021-10-25 12:11:45 +02:00
|
|
|
if (from == to)
|
|
|
|
/* this doesn't check whether the playlist exists, but
|
|
|
|
what the hell.. */
|
|
|
|
return CommandResult::OK;
|
|
|
|
|
|
|
|
PlaylistFileEditor editor(name, PlaylistFileEditor::LoadMode::YES);
|
|
|
|
editor.MoveIndex(from, to);
|
|
|
|
editor.Save();
|
2015-12-26 06:30:25 +01:00
|
|
|
return CommandResult::OK;
|
2012-08-25 12:59:54 +02:00
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
CommandResult
|
2020-03-12 20:56:11 +01:00
|
|
|
handle_playlistclear([[maybe_unused]] Client &client,
|
|
|
|
Request args, [[maybe_unused]] Response &r)
|
2012-08-25 12:59:54 +02:00
|
|
|
{
|
2014-12-06 00:08:08 +01:00
|
|
|
const char *const name = args.front();
|
|
|
|
|
2015-12-28 07:22:10 +01:00
|
|
|
spl_clear(name);
|
|
|
|
return CommandResult::OK;
|
2012-08-25 12:59:54 +02:00
|
|
|
}
|
|
|
|
|
2021-10-25 08:43:42 +02:00
|
|
|
static CommandResult
|
|
|
|
handle_playlistadd_position(Client &client, const char *playlist_name,
|
|
|
|
const char *uri, unsigned position,
|
|
|
|
Response &r)
|
|
|
|
{
|
|
|
|
PlaylistFileEditor editor{
|
|
|
|
playlist_name,
|
|
|
|
PlaylistFileEditor::LoadMode::TRY,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (position > editor.size()) {
|
|
|
|
r.Error(ACK_ERROR_ARG, "Bad position");
|
|
|
|
return CommandResult::ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (uri_has_scheme(uri)) {
|
|
|
|
editor.Insert(position, uri);
|
|
|
|
} else {
|
|
|
|
#ifdef ENABLE_DATABASE
|
|
|
|
const DatabaseSelection selection(uri, true, nullptr);
|
|
|
|
|
2021-11-11 09:44:47 +01:00
|
|
|
if (SearchInsertIntoPlaylist(client.GetDatabaseOrThrow(),
|
|
|
|
client.GetStorage(),
|
|
|
|
selection,
|
|
|
|
editor, position) == 0)
|
|
|
|
/* no song was found, don't need to save */
|
|
|
|
return CommandResult::OK;
|
2021-10-25 08:43:42 +02:00
|
|
|
#else
|
|
|
|
(void)client;
|
|
|
|
r.Error(ACK_ERROR_NO_EXIST, "No database");
|
|
|
|
return CommandResult::ERROR;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
editor.Save();
|
|
|
|
|
|
|
|
return CommandResult::OK;
|
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
CommandResult
|
2020-03-12 20:56:11 +01:00
|
|
|
handle_playlistadd(Client &client, Request args, [[maybe_unused]] Response &r)
|
2012-08-25 12:59:54 +02:00
|
|
|
{
|
2014-12-06 00:08:08 +01:00
|
|
|
const char *const playlist = args[0];
|
|
|
|
const char *const uri = args[1];
|
2012-08-25 12:59:54 +02:00
|
|
|
|
2022-07-04 18:38:57 +02:00
|
|
|
if (args.size() >= 3)
|
2021-10-25 08:43:42 +02:00
|
|
|
return handle_playlistadd_position(client, playlist, uri,
|
|
|
|
args.ParseUnsigned(2), r);
|
|
|
|
|
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);
|
2016-10-27 21:59:17 +02:00
|
|
|
spl_append_uri(playlist, loader, uri);
|
2014-01-30 20:29:48 +01:00
|
|
|
} else {
|
|
|
|
#ifdef ENABLE_DATABASE
|
2016-10-26 18:47:19 +02:00
|
|
|
const Database &db = client.GetDatabaseOrThrow();
|
2019-03-25 18:48:31 +01:00
|
|
|
const DatabaseSelection selection(uri, true, nullptr);
|
2014-02-01 01:11:50 +01:00
|
|
|
|
2017-02-08 09:47:43 +01:00
|
|
|
search_add_to_playlist(db, client.GetStorage(),
|
2019-03-25 18:48:31 +01:00
|
|
|
playlist, selection);
|
2014-01-30 20:29:48 +01:00
|
|
|
#else
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Error(ACK_ERROR_NO_EXIST, "directory or file not found");
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::ERROR;
|
2016-10-29 10:21:57 +02:00
|
|
|
#endif
|
2012-08-25 12:59:54 +02:00
|
|
|
}
|
|
|
|
|
2016-10-29 10:21:57 +02:00
|
|
|
return CommandResult::OK;
|
2012-08-25 12:59:54 +02:00
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
CommandResult
|
2020-03-12 20:56:11 +01:00
|
|
|
handle_listplaylists([[maybe_unused]] Client &client, [[maybe_unused]] Request args,
|
2015-08-13 12:48:31 +02:00
|
|
|
Response &r)
|
2012-08-25 12:59:54 +02:00
|
|
|
{
|
2015-12-29 12:41:45 +01:00
|
|
|
print_spl_list(r, ListPlaylistFiles());
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::OK;
|
2012-08-25 12:59:54 +02:00
|
|
|
}
|