PlaylistSong.cxx 2.86 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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
	{
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
static void
apply_song_metadata(DetachedSong &dest, const DetachedSong &src)
47
{
48 49
	if (!src.GetTag().IsDefined() &&
	    src.GetStartMS() == 0 && src.GetEndMS() == 0)
50
		return;
51

52
	merge_song_metadata(dest, src);
53

54
	if (dest.GetTag().IsDefined() && dest.GetTag().time > 0 &&
55
	    src.GetStartMS() > 0 && src.GetEndMS() == 0 &&
56
	    src.GetStartMS() / 1000 < (unsigned)dest.GetTag().time)
57 58 59
		/* the range is open-ended, and the playlist plugin
		   did not know the total length of the song file
		   (e.g. last track on a CUE file); fix it up here */
60 61
		dest.WritableTag().time =
			dest.GetTag().time - src.GetStartMS() / 1000;
62 63
}

64
static bool
65
playlist_check_load_song(DetachedSong &song, const SongLoader &loader)
66
{
67 68
	DetachedSong *tmp = loader.LoadSong(song.GetURI(), IgnoreError());
	if (tmp == nullptr)
69
		return false;
70 71

	song.SetURI(tmp->GetURI());
72 73 74
	if (!song.HasRealURI() && tmp->HasRealURI())
		song.SetRealURI(tmp->GetRealURI());

75 76 77
	apply_song_metadata(song, *tmp);
	delete tmp;
	return true;
78 79
}

80 81
bool
playlist_check_translate_song(DetachedSong &song, const char *base_uri,
82
			      const SongLoader &loader)
83
{
84
	if (base_uri != nullptr && strcmp(base_uri, ".") == 0)
85
		/* PathTraitsUTF8::GetParent() returns "." when there
Max Kellermann's avatar
Max Kellermann committed
86 87
		   is no directory name in the given path; clear that
		   now, because it would break the database lookup
88
		   functions */
89
		base_uri = nullptr;
90

91 92 93
	const char *uri = song.GetURI();
	if (base_uri != nullptr && !uri_has_scheme(uri) &&
	    !PathTraitsUTF8::IsAbsolute(uri))
94
		song.SetURI(PathTraitsUTF8::Build(base_uri, uri));
95

96
	return playlist_check_load_song(song, loader);
97
}