PlaylistSong.cxx 2.28 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2015 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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"
21
#include "PlaylistSong.hxx"
22
#include "SongLoader.hxx"
23
#include "tag/Tag.hxx"
24
#include "tag/TagBuilder.hxx"
25
#include "fs/Traits.hxx"
Max Kellermann's avatar
Max Kellermann committed
26
#include "util/UriUtil.hxx"
27
#include "util/Error.hxx"
28
#include "DetachedSong.hxx"
29 30 31 32 33

#include <assert.h>
#include <string.h>

static void
34
merge_song_metadata(DetachedSong &add, const DetachedSong &base)
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
static bool
46
playlist_check_load_song(DetachedSong &song, const SongLoader &loader)
47
{
48 49
	DetachedSong *tmp = loader.LoadSong(song.GetURI(), IgnoreError());
	if (tmp == nullptr)
50
		return false;
51 52

	song.SetURI(tmp->GetURI());
53 54 55
	if (!song.HasRealURI() && tmp->HasRealURI())
		song.SetRealURI(tmp->GetRealURI());

56
	merge_song_metadata(song, *tmp);
57 58
	delete tmp;
	return true;
59 60
}

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

72 73 74
	const char *uri = song.GetURI();
	if (base_uri != nullptr && !uri_has_scheme(uri) &&
	    !PathTraitsUTF8::IsAbsolute(uri))
75
		song.SetURI(PathTraitsUTF8::Build(base_uri, uri));
76

77
	return playlist_check_load_song(song, loader);
78
}