PlaylistSong.cxx 4.28 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.h"
27 28

extern "C" {
29 30
#include "song.h"
#include "uri.h"
31
#include "path.h"
32
}
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

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

static void
merge_song_metadata(struct song *dest, const struct song *base,
		    const struct song *add)
{
	dest->tag = base->tag != NULL
		? (add->tag != NULL
		   ? tag_merge(base->tag, add->tag)
		   : tag_dup(base->tag))
		: (add->tag != NULL
		   ? tag_dup(add->tag)
		   : NULL);

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

static struct song *
apply_song_metadata(struct song *dest, const struct song *src)
{
	struct song *tmp;

	assert(dest != NULL);
	assert(src != NULL);

	if (src->tag == NULL && src->start_ms == 0 && src->end_ms == 0)
		return dest;

	if (song_in_database(dest)) {
		char *path_fs = map_song_fs(dest);
		if (path_fs == NULL)
			return dest;

70 71 72 73 74 75 76 77
		char *path_utf8 = fs_charset_to_utf8(path_fs);
		if (path_utf8 != NULL)
			g_free(path_fs);
		else
			path_utf8 = path_fs;

		tmp = song_file_new(path_utf8, NULL);
		g_free(path_utf8);
78

79 80 81 82 83 84
		merge_song_metadata(tmp, dest, src);
	} else {
		tmp = song_file_new(dest->uri, NULL);
		merge_song_metadata(tmp, dest, src);
	}

85 86 87 88 89 90 91 92
	if (dest->tag != NULL && dest->tag->time > 0 &&
	    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;

93
	song_free(dest);
94 95 96
	return tmp;
}

97
static struct song *
98
playlist_check_load_song(const struct song *song, const char *uri, bool secure)
99 100 101 102 103 104 105 106 107 108
{
	struct song *dest;

	if (uri_has_scheme(uri)) {
		dest = song_remote_new(uri);
	} else if (g_path_is_absolute(uri) && secure) {
		dest = song_file_load(uri, NULL);
		if (dest == NULL)
			return NULL;
	} else {
109 110 111 112 113
		const Database *db = GetDatabase(nullptr);
		if (db == nullptr)
			return nullptr;

		struct song *tmp = db->GetSong(uri, nullptr);
114
		if (tmp == NULL)
115 116
			/* not found in database */
			return NULL;
117 118

		dest = song_dup_detached(tmp);
119
		db->ReturnSong(tmp);
120 121 122 123 124
	}

	return apply_song_metadata(dest, song);
}

125
struct song *
126 127
playlist_check_translate_song(struct song *song, const char *base_uri,
			      bool secure)
128 129 130 131 132
{
	if (song_in_database(song))
		/* already ok */
		return song;

133
	const char *uri = song->uri;
134 135 136 137 138 139 140 141 142 143 144 145

	if (uri_has_scheme(uri)) {
		if (uri_supported_scheme(uri))
			/* valid remote song */
			return song;
		else {
			/* unsupported remote song */
			song_free(song);
			return NULL;
		}
	}

146 147 148 149 150 151 152
	if (base_uri != NULL && strcmp(base_uri, ".") == 0)
		/* g_path_get_dirname() returns "." when there is no
		   directory name in the given path; clear that now,
		   because it would break the database lookup
		   functions */
		base_uri = NULL;

153 154
	if (g_path_is_absolute(uri)) {
		/* XXX fs_charset vs utf8? */
155 156
		const char *suffix = map_to_relative_path(uri);
		assert(suffix != NULL);
157

158 159
		if (suffix != uri)
			uri = suffix;
160
		else if (!secure) {
161
			/* local files must be relative to the music
162
			   directory when "secure" is enabled */
163 164 165 166
			song_free(song);
			return NULL;
		}

167
		base_uri = NULL;
168 169
	}

170
	char *allocated = NULL;
171
	if (base_uri != NULL)
172
		uri = allocated = g_build_filename(base_uri, uri, NULL);
173

174
	struct song *dest = playlist_check_load_song(song, uri, secure);
175
	song_free(song);
176
	g_free(allocated);
177 178
	return dest;
}