PlaylistSong.cxx 2.87 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * 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.
 */

20
#include "PlaylistSong.hxx"
21
#include "SongLoader.hxx"
22
#include "tag/Tag.hxx"
23
#include "tag/Builder.hxx"
24
#include "fs/Traits.hxx"
25
#include "song/DetachedSong.hxx"
Max Kellermann's avatar
Max Kellermann committed
26
#include "util/UriExtract.hxx"
27

28 29 30
#include <algorithm>
#include <string>

31 32 33
#include <string.h>

static void
34
merge_song_metadata(DetachedSong &add, const DetachedSong &base) noexcept
35
{
36
	if (base.GetTag().IsDefined()) {
37 38
		TagBuilder builder(add.GetTag());
		builder.Complement(base.GetTag());
39
		add.SetTag(builder.Commit());
40 41
	}

42
	add.SetLastModified(base.GetLastModified());
43 44 45 46 47 48 49

	if (add.GetStartTime().IsZero()) {
		add.SetStartTime(base.GetStartTime());
	}
	if (add.GetEndTime().IsZero()) {
		add.SetEndTime(base.GetEndTime());
	}
50 51
}

52
static bool
53
playlist_check_load_song(DetachedSong &song, const SongLoader &loader) noexcept
54 55
try {
	DetachedSong tmp = loader.LoadSong(song.GetURI());
56

57 58 59
	song.SetURI(tmp.GetURI());
	if (!song.HasRealURI() && tmp.HasRealURI())
		song.SetRealURI(tmp.GetRealURI());
60

61
	merge_song_metadata(song, tmp);
62
	return true;
63
} catch (...) {
64
	return false;
65 66
}

67
bool
68
playlist_check_translate_song(DetachedSong &song, std::string_view base_uri,
69
			      const SongLoader &loader) noexcept
70
{
71
	if (base_uri.compare(".") == 0)
72
		/* PathTraitsUTF8::GetParent() returns "." when there
Max Kellermann's avatar
Max Kellermann committed
73 74
		   is no directory name in the given path; clear that
		   now, because it would break the database lookup
75
		   functions */
76
		base_uri = {};
77

78
	const char *uri = song.GetURI();
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

#ifdef _WIN32
	if (!PathTraitsUTF8::IsAbsolute(uri) && strchr(uri, '\\') != nullptr) {
		/* Windows uses the backslash as path separator, but
		   the MPD protocol uses the (forward) slash by
		   definition; to allow backslashes in relative URIs
		   loaded from playlist files, this step converts all
		   backslashes to (forward) slashes */

		std::string new_uri(uri);
		std::replace(new_uri.begin(), new_uri.end(), '\\', '/');
		song.SetURI(std::move(new_uri));
		uri = song.GetURI();
	}
#endif

95
	if (base_uri.data() != nullptr && !uri_has_scheme(uri) &&
96
	    !PathTraitsUTF8::IsAbsolute(uri))
97
		song.SetURI(PathTraitsUTF8::Build(base_uri, uri));
98

99
	return playlist_check_load_song(song, loader);
100
}