PlaylistSong.cxx 3.51 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"
Max Kellermann's avatar
Max Kellermann committed
22
#include "Mapper.hxx"
23
#include "DatabaseSong.hxx"
Max Kellermann's avatar
Max Kellermann committed
24
#include "ls.hxx"
25
#include "tag/Tag.hxx"
26
#include "tag/TagBuilder.hxx"
27
#include "fs/AllocatedPath.hxx"
28
#include "fs/Traits.hxx"
Max Kellermann's avatar
Max Kellermann committed
29
#include "util/UriUtil.hxx"
30
#include "util/Error.hxx"
31
#include "DetachedSong.hxx"
32 33 34 35 36

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

static void
37
merge_song_metadata(DetachedSong &add, const DetachedSong &base)
38
{
39
	{
40 41
		TagBuilder builder(add.GetTag());
		builder.Complement(base.GetTag());
42
		add.SetTag(builder.Commit());
43 44
	}

45
	add.SetLastModified(base.GetLastModified());
46 47
}

48 49
static void
apply_song_metadata(DetachedSong &dest, const DetachedSong &src)
50
{
51 52
	if (!src.GetTag().IsDefined() &&
	    src.GetStartMS() == 0 && src.GetEndMS() == 0)
53
		return;
54

55
	merge_song_metadata(dest, src);
56

57
	if (dest.GetTag().IsDefined() && dest.GetTag().time > 0 &&
58
	    src.GetStartMS() > 0 && src.GetEndMS() == 0 &&
59
	    src.GetStartMS() / 1000 < (unsigned)dest.GetTag().time)
60 61 62
		/* 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 */
63 64
		dest.WritableTag().time =
			dest.GetTag().time - src.GetStartMS() / 1000;
65 66
}

67 68
static bool
playlist_check_load_song(DetachedSong &song)
69
{
70
	const char *const uri = song.GetURI();
71 72

	if (uri_has_scheme(uri)) {
73
		return true;
74
	} else if (PathTraitsUTF8::IsAbsolute(uri)) {
75 76 77 78 79 80
		DetachedSong tmp(uri);
		if (!tmp.Update())
			return false;

		apply_song_metadata(song, tmp);
		return true;
81
	} else {
82 83 84
		DetachedSong *tmp = DatabaseDetachSong(uri, IgnoreError());
		if (tmp == nullptr)
			return false;
85

86 87 88 89
		apply_song_metadata(song, *tmp);
		delete tmp;
		return true;
	}
90 91
}

92 93
bool
playlist_check_translate_song(DetachedSong &song, const char *base_uri,
94
			      bool secure)
95
{
96
	const char *const uri = song.GetURI();
97

98 99 100
	if (uri_has_scheme(uri))
		/* valid remote song? */
		return uri_supported_scheme(uri);
101

102
	if (base_uri != nullptr && strcmp(base_uri, ".") == 0)
103
		/* PathTraitsUTF8::GetParent() returns "." when there
Max Kellermann's avatar
Max Kellermann committed
104 105
		   is no directory name in the given path; clear that
		   now, because it would break the database lookup
106
		   functions */
107
		base_uri = nullptr;
108

109
	if (PathTraitsUTF8::IsAbsolute(uri)) {
110
		/* XXX fs_charset vs utf8? */
111
		const char *suffix = map_to_relative_path(uri);
112
		assert(suffix != nullptr);
113

114
		if (suffix != uri)
115 116
			song.SetURI(std::string(suffix));
		else if (!secure)
117
			/* local files must be relative to the music
118
			   directory when "secure" is enabled */
119
			return false;
120

121
		base_uri = nullptr;
122 123
	}

124
	if (base_uri != nullptr) {
125
		song.SetURI(PathTraitsUTF8::Build(base_uri, uri));
126 127
		/* repeat the above checks */
		return playlist_check_translate_song(song, nullptr, secure);
128
	}
129

130
	return playlist_check_load_song(song);
131
}