PlaylistSong.cxx 2.21 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2019 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"
Max Kellermann's avatar
Max Kellermann committed
25
#include "util/UriUtil.hxx"
26
#include "song/DetachedSong.hxx"
27 28 29 30

#include <string.h>

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

39
	add.SetLastModified(base.GetLastModified());
40 41
}

42
static bool
43
playlist_check_load_song(DetachedSong &song, const SongLoader &loader) noexcept
44 45
try {
	DetachedSong tmp = loader.LoadSong(song.GetURI());
46

47 48 49
	song.SetURI(tmp.GetURI());
	if (!song.HasRealURI() && tmp.HasRealURI())
		song.SetRealURI(tmp.GetRealURI());
50

51
	merge_song_metadata(song, tmp);
52
	return true;
53
} catch (...) {
54
	return false;
55 56
}

57 58
bool
playlist_check_translate_song(DetachedSong &song, const char *base_uri,
59
			      const SongLoader &loader) noexcept
60
{
61
	if (base_uri != nullptr && strcmp(base_uri, ".") == 0)
62
		/* PathTraitsUTF8::GetParent() returns "." when there
Max Kellermann's avatar
Max Kellermann committed
63 64
		   is no directory name in the given path; clear that
		   now, because it would break the database lookup
65
		   functions */
66
		base_uri = nullptr;
67

68 69 70
	const char *uri = song.GetURI();
	if (base_uri != nullptr && !uri_has_scheme(uri) &&
	    !PathTraitsUTF8::IsAbsolute(uri))
71
		song.SetURI(PathTraitsUTF8::Build(base_uri, uri));
72

73
	return playlist_check_load_song(song, loader);
74
}