SongLoader.cxx 2.62 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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 "SongLoader.hxx"
22
#include "LocateUri.hxx"
23 24
#include "client/Client.hxx"
#include "db/DatabaseSong.hxx"
25
#include "storage/StorageInterface.hxx"
26 27 28 29 30
#include "DetachedSong.hxx"
#include "PlaylistError.hxx"

#include <assert.h>

31 32 33
#ifdef ENABLE_DATABASE

SongLoader::SongLoader(const Client &_client)
34
	:client(&_client), db(_client.GetDatabase()),
35
	 storage(_client.GetStorage()) {}
36 37 38

#endif

39
DetachedSong
40
SongLoader::LoadFromDatabase(const char *uri) const
41 42 43
{
#ifdef ENABLE_DATABASE
	if (db != nullptr)
44
		return DatabaseDetachSong(*db, storage, uri);
45 46 47 48
#else
	(void)uri;
#endif

49
	throw PlaylistError(PlaylistResult::NO_SUCH_SONG, "No database");
50 51
}

52
DetachedSong
53
SongLoader::LoadFile(const char *path_utf8, Path path_fs) const
54 55
{
#ifdef ENABLE_DATABASE
56 57 58 59 60
	if (storage != nullptr) {
		const char *suffix = storage->MapToRelativeUTF8(path_utf8);
		if (suffix != nullptr)
			/* this path was relative to the music
			   directory - obtain it from the database */
61
			return LoadFromDatabase(suffix);
62
	}
63 64
#endif

65
	DetachedSong song(path_utf8);
66 67
	if (!song.LoadFile(path_fs))
		throw PlaylistError::NoSuchSong();
68

69
	return song;
70 71
}

72
DetachedSong
73
SongLoader::LoadSong(const LocatedUri &located_uri) const
74 75 76
{
	switch (located_uri.type) {
	case LocatedUri::Type::ABSOLUTE:
77
		return DetachedSong(located_uri.canonical_uri);
78 79

	case LocatedUri::Type::RELATIVE:
80
		return LoadFromDatabase(located_uri.canonical_uri);
81 82

	case LocatedUri::Type::PATH:
83
		return LoadFile(located_uri.canonical_uri, located_uri.path);
84 85 86 87 88
	}

	gcc_unreachable();
}

89
DetachedSong
90
SongLoader::LoadSong(const char *uri_utf8) const
91
{
92 93
#if !CLANG_CHECK_VERSION(3,6)
	/* disabled on clang due to -Wtautological-pointer-compare */
94
	assert(uri_utf8 != nullptr);
95
#endif
96

97
	const auto located_uri = LocateUri(uri_utf8, client
98
#ifdef ENABLE_DATABASE
99
					   , storage
100
#endif
101
					   );
102
	return LoadSong(located_uri);
103
}