2014-02-02 14:37:52 +01:00
|
|
|
/*
|
2020-01-18 19:22:19 +01:00
|
|
|
* Copyright 2003-2020 The Music Player Daemon Project
|
2014-02-02 14:37:52 +01: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 "SongLoader.hxx"
|
2015-10-22 09:29:02 +02:00
|
|
|
#include "LocateUri.hxx"
|
2014-02-02 14:37:52 +01:00
|
|
|
#include "client/Client.hxx"
|
|
|
|
#include "db/DatabaseSong.hxx"
|
2014-02-07 19:01:06 +01:00
|
|
|
#include "storage/StorageInterface.hxx"
|
2018-08-02 13:45:43 +02:00
|
|
|
#include "song/DetachedSong.hxx"
|
2014-02-02 14:37:52 +01:00
|
|
|
#include "PlaylistError.hxx"
|
2018-11-19 12:49:45 +01:00
|
|
|
#include "config.h"
|
2014-02-02 14:37:52 +01:00
|
|
|
|
2020-03-12 23:20:59 +01:00
|
|
|
#include <cassert>
|
2014-02-02 14:37:52 +01:00
|
|
|
|
2014-02-01 00:26:34 +01:00
|
|
|
#ifdef ENABLE_DATABASE
|
|
|
|
|
|
|
|
SongLoader::SongLoader(const Client &_client)
|
2016-10-26 18:52:00 +02:00
|
|
|
:client(&_client), db(_client.GetDatabase()),
|
2014-02-07 00:29:07 +01:00
|
|
|
storage(_client.GetStorage()) {}
|
2014-02-01 00:26:34 +01:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2017-02-08 10:02:08 +01:00
|
|
|
DetachedSong
|
2016-03-19 00:13:57 +01:00
|
|
|
SongLoader::LoadFromDatabase(const char *uri) const
|
2015-10-22 09:34:13 +02:00
|
|
|
{
|
|
|
|
#ifdef ENABLE_DATABASE
|
|
|
|
if (db != nullptr)
|
2017-02-08 09:47:43 +01:00
|
|
|
return DatabaseDetachSong(*db, storage, uri);
|
2015-10-22 09:34:13 +02:00
|
|
|
#else
|
|
|
|
(void)uri;
|
|
|
|
#endif
|
|
|
|
|
2016-02-28 11:15:20 +01:00
|
|
|
throw PlaylistError(PlaylistResult::NO_SUCH_SONG, "No database");
|
2015-10-22 09:34:13 +02:00
|
|
|
}
|
|
|
|
|
2017-02-08 10:02:08 +01:00
|
|
|
DetachedSong
|
2016-03-19 00:13:57 +01:00
|
|
|
SongLoader::LoadFile(const char *path_utf8, Path path_fs) const
|
2014-02-02 14:37:52 +01:00
|
|
|
{
|
|
|
|
#ifdef ENABLE_DATABASE
|
2014-02-07 19:01:06 +01:00
|
|
|
if (storage != nullptr) {
|
2020-04-03 16:22:39 +02:00
|
|
|
const auto suffix = storage->MapToRelativeUTF8(path_utf8);
|
|
|
|
if (suffix.data() != nullptr)
|
2014-02-07 19:01:06 +01:00
|
|
|
/* this path was relative to the music
|
|
|
|
directory - obtain it from the database */
|
2020-04-03 16:22:39 +02:00
|
|
|
return LoadFromDatabase(std::string(suffix).c_str());
|
2014-02-07 19:01:06 +01:00
|
|
|
}
|
2014-02-02 14:37:52 +01:00
|
|
|
#endif
|
|
|
|
|
2016-02-28 11:08:00 +01:00
|
|
|
DetachedSong song(path_utf8);
|
2016-02-28 11:15:20 +01:00
|
|
|
if (!song.LoadFile(path_fs))
|
|
|
|
throw PlaylistError::NoSuchSong();
|
2014-02-02 14:37:52 +01:00
|
|
|
|
2017-02-08 10:02:08 +01:00
|
|
|
return song;
|
2014-02-02 14:37:52 +01:00
|
|
|
}
|
|
|
|
|
2017-02-08 10:02:08 +01:00
|
|
|
DetachedSong
|
2016-03-19 00:13:57 +01:00
|
|
|
SongLoader::LoadSong(const LocatedUri &located_uri) const
|
2015-10-22 09:29:02 +02:00
|
|
|
{
|
|
|
|
switch (located_uri.type) {
|
|
|
|
case LocatedUri::Type::ABSOLUTE:
|
2017-02-08 10:02:08 +01:00
|
|
|
return DetachedSong(located_uri.canonical_uri);
|
2015-10-22 09:29:02 +02:00
|
|
|
|
|
|
|
case LocatedUri::Type::RELATIVE:
|
2016-03-19 00:13:57 +01:00
|
|
|
return LoadFromDatabase(located_uri.canonical_uri);
|
2015-10-22 09:29:02 +02:00
|
|
|
|
|
|
|
case LocatedUri::Type::PATH:
|
2016-03-19 00:13:57 +01:00
|
|
|
return LoadFile(located_uri.canonical_uri, located_uri.path);
|
2015-10-22 09:29:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
gcc_unreachable();
|
|
|
|
}
|
|
|
|
|
2017-02-08 10:02:08 +01:00
|
|
|
DetachedSong
|
2016-10-27 21:59:17 +02:00
|
|
|
SongLoader::LoadSong(const char *uri_utf8) const
|
2014-02-02 14:37:52 +01:00
|
|
|
{
|
2014-12-26 14:28:01 +01:00
|
|
|
#if !CLANG_CHECK_VERSION(3,6)
|
|
|
|
/* disabled on clang due to -Wtautological-pointer-compare */
|
2014-02-02 14:37:52 +01:00
|
|
|
assert(uri_utf8 != nullptr);
|
2014-12-26 14:28:01 +01:00
|
|
|
#endif
|
2014-02-02 14:37:52 +01:00
|
|
|
|
2019-04-18 10:03:15 +02:00
|
|
|
const auto located_uri = LocateUri(UriPluginKind::INPUT,
|
|
|
|
uri_utf8, client
|
2015-10-22 09:29:02 +02:00
|
|
|
#ifdef ENABLE_DATABASE
|
2016-10-27 21:59:17 +02:00
|
|
|
, storage
|
2015-10-22 09:29:02 +02:00
|
|
|
#endif
|
2016-10-27 21:59:17 +02:00
|
|
|
);
|
2016-03-19 00:13:57 +01:00
|
|
|
return LoadSong(located_uri);
|
2014-02-02 14:37:52 +01:00
|
|
|
}
|