PlaylistSong.cxx 4.32 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2013 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 24
#include "DatabasePlugin.hxx"
#include "DatabaseGlue.hxx"
Max Kellermann's avatar
Max Kellermann committed
25
#include "ls.hxx"
26
#include "tag/Tag.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 "Song.hxx"
32

Max Kellermann's avatar
Max Kellermann committed
33 34
#include <glib.h>

35 36 37 38
#include <assert.h>
#include <string.h>

static void
39 40
merge_song_metadata(Song *dest, const Song *base,
		    const Song *add)
41
{
42 43
	dest->tag = base->tag != nullptr
		? (add->tag != nullptr
Max Kellermann's avatar
Max Kellermann committed
44 45
		   ? Tag::Merge(*base->tag, *add->tag)
		   : new Tag(*base->tag))
46
		: (add->tag != nullptr
Max Kellermann's avatar
Max Kellermann committed
47
		   ? new Tag(*add->tag)
48
		   : nullptr);
49 50 51 52 53 54

	dest->mtime = base->mtime;
	dest->start_ms = add->start_ms;
	dest->end_ms = add->end_ms;
}

55 56
static Song *
apply_song_metadata(Song *dest, const Song *src)
57
{
58
	Song *tmp;
59

60 61
	assert(dest != nullptr);
	assert(src != nullptr);
62

63
	if (src->tag == nullptr && src->start_ms == 0 && src->end_ms == 0)
64 65
		return dest;

66
	if (dest->IsInDatabase()) {
67
		const auto path_fs = map_song_fs(*dest);
68
		if (path_fs.IsNull())
69 70
			return dest;

71 72 73
		std::string path_utf8 = path_fs.ToUTF8();
		if (path_utf8.empty())
			path_utf8 = path_fs.c_str();
74

75
		tmp = Song::NewFile(path_utf8.c_str(), nullptr);
76

77 78
		merge_song_metadata(tmp, dest, src);
	} else {
79
		tmp = Song::NewFile(dest->uri, nullptr);
80 81 82
		merge_song_metadata(tmp, dest, src);
	}

83
	if (dest->tag != nullptr && dest->tag->time > 0 &&
84 85 86 87 88 89 90
	    src->start_ms > 0 && src->end_ms == 0 &&
	    src->start_ms / 1000 < (unsigned)dest->tag->time)
		/* 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 */
		tmp->tag->time = dest->tag->time - src->start_ms / 1000;

91
	dest->Free();
92 93 94
	return tmp;
}

95 96
static Song *
playlist_check_load_song(const Song *song, const char *uri, bool secure)
97
{
98
	Song *dest;
99 100

	if (uri_has_scheme(uri)) {
101
		dest = Song::NewRemote(uri);
102
	} else if (PathTraits::IsAbsoluteUTF8(uri) && secure) {
103
		dest = Song::LoadFile(uri, nullptr);
104 105
		if (dest == nullptr)
			return nullptr;
106
	} else {
107
		const Database *db = GetDatabase();
108 109 110
		if (db == nullptr)
			return nullptr;

111
		Song *tmp = db->GetSong(uri, IgnoreError());
112
		if (tmp == nullptr)
113
			/* not found in database */
114
			return nullptr;
115

116
		dest = tmp->DupDetached();
117
		db->ReturnSong(tmp);
118 119 120 121 122
	}

	return apply_song_metadata(dest, song);
}

123 124
Song *
playlist_check_translate_song(Song *song, const char *base_uri,
125
			      bool secure)
126
{
127
	if (song->IsInDatabase())
128 129 130
		/* already ok */
		return song;

131
	const char *uri = song->uri;
132 133 134 135 136 137 138

	if (uri_has_scheme(uri)) {
		if (uri_supported_scheme(uri))
			/* valid remote song */
			return song;
		else {
			/* unsupported remote song */
139
			song->Free();
140
			return nullptr;
141 142 143
		}
	}

144
	if (base_uri != nullptr && strcmp(base_uri, ".") == 0)
Max Kellermann's avatar
Max Kellermann committed
145 146 147
		/* PathTraits::GetParentUTF8() returns "." when there
		   is no directory name in the given path; clear that
		   now, because it would break the database lookup
148
		   functions */
149
		base_uri = nullptr;
150

151
	if (PathTraits::IsAbsoluteUTF8(uri)) {
152
		/* XXX fs_charset vs utf8? */
153
		const char *suffix = map_to_relative_path(uri);
154
		assert(suffix != nullptr);
155

156 157
		if (suffix != uri)
			uri = suffix;
158
		else if (!secure) {
159
			/* local files must be relative to the music
160
			   directory when "secure" is enabled */
161
			song->Free();
162
			return nullptr;
163 164
		}

165
		base_uri = nullptr;
166 167
	}

168 169 170
	char *allocated = nullptr;
	if (base_uri != nullptr)
		uri = allocated = g_build_filename(base_uri, uri, nullptr);
171

172 173
	Song *dest = playlist_check_load_song(song, uri, secure);
	song->Free();
174
	g_free(allocated);
175 176
	return dest;
}