2010-02-08 10:28:12 +01:00
|
|
|
/*
|
2013-01-02 18:38:32 +01:00
|
|
|
* Copyright (C) 2003-2013 The Music Player Daemon Project
|
2010-02-08 10:28:12 +01:00
|
|
|
* 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"
|
2013-01-02 18:38:32 +01:00
|
|
|
#include "PlaylistSong.hxx"
|
2013-01-02 22:43:56 +01:00
|
|
|
#include "Mapper.hxx"
|
2013-01-03 00:30:15 +01:00
|
|
|
#include "DatabasePlugin.hxx"
|
|
|
|
#include "DatabaseGlue.hxx"
|
2013-01-03 17:34:51 +01:00
|
|
|
#include "ls.hxx"
|
2013-09-05 18:22:02 +02:00
|
|
|
#include "tag/Tag.hxx"
|
2013-01-21 19:14:37 +01:00
|
|
|
#include "fs/Path.hxx"
|
2013-04-08 23:30:21 +02:00
|
|
|
#include "util/UriUtil.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Error.hxx"
|
2013-07-28 13:25:12 +02:00
|
|
|
#include "Song.hxx"
|
2010-02-08 10:28:12 +01:00
|
|
|
|
2013-01-08 16:38:00 +01:00
|
|
|
#include <glib.h>
|
|
|
|
|
2010-02-08 10:28:12 +01:00
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
static void
|
2013-07-28 13:25:12 +02:00
|
|
|
merge_song_metadata(Song *dest, const Song *base,
|
|
|
|
const Song *add)
|
2010-02-08 10:28:12 +01:00
|
|
|
{
|
|
|
|
dest->tag = base->tag != NULL
|
|
|
|
? (add->tag != NULL
|
2013-07-30 20:11:57 +02:00
|
|
|
? Tag::Merge(*base->tag, *add->tag)
|
|
|
|
: new Tag(*base->tag))
|
2010-02-08 10:28:12 +01:00
|
|
|
: (add->tag != NULL
|
2013-07-30 20:11:57 +02:00
|
|
|
? new Tag(*add->tag)
|
2010-02-08 10:28:12 +01:00
|
|
|
: NULL);
|
|
|
|
|
|
|
|
dest->mtime = base->mtime;
|
|
|
|
dest->start_ms = add->start_ms;
|
|
|
|
dest->end_ms = add->end_ms;
|
|
|
|
}
|
|
|
|
|
2013-07-28 13:25:12 +02:00
|
|
|
static Song *
|
|
|
|
apply_song_metadata(Song *dest, const Song *src)
|
2010-02-08 10:28:12 +01:00
|
|
|
{
|
2013-07-28 13:25:12 +02:00
|
|
|
Song *tmp;
|
2010-02-08 10:28:12 +01:00
|
|
|
|
|
|
|
assert(dest != NULL);
|
|
|
|
assert(src != NULL);
|
|
|
|
|
|
|
|
if (src->tag == NULL && src->start_ms == 0 && src->end_ms == 0)
|
|
|
|
return dest;
|
|
|
|
|
2013-07-28 13:25:12 +02:00
|
|
|
if (dest->IsInDatabase()) {
|
2013-01-23 21:26:38 +01:00
|
|
|
const Path &path_fs = map_song_fs(dest);
|
|
|
|
if (path_fs.IsNull())
|
2010-02-08 10:28:12 +01:00
|
|
|
return dest;
|
|
|
|
|
2013-01-23 21:26:38 +01:00
|
|
|
std::string path_utf8 = path_fs.ToUTF8();
|
|
|
|
if (path_utf8.empty())
|
|
|
|
path_utf8 = path_fs.c_str();
|
2012-10-05 17:01:04 +02:00
|
|
|
|
2013-07-28 13:25:12 +02:00
|
|
|
tmp = Song::NewFile(path_utf8.c_str(), NULL);
|
2010-07-25 13:35:59 +02:00
|
|
|
|
2010-02-08 10:28:12 +01:00
|
|
|
merge_song_metadata(tmp, dest, src);
|
|
|
|
} else {
|
2013-07-28 13:25:12 +02:00
|
|
|
tmp = Song::NewFile(dest->uri, NULL);
|
2010-02-08 10:28:12 +01:00
|
|
|
merge_song_metadata(tmp, dest, src);
|
|
|
|
}
|
|
|
|
|
2010-11-08 20:16:26 +01:00
|
|
|
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;
|
|
|
|
|
2013-07-28 13:25:12 +02:00
|
|
|
dest->Free();
|
2010-02-08 10:28:12 +01:00
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2013-07-28 13:25:12 +02:00
|
|
|
static Song *
|
|
|
|
playlist_check_load_song(const Song *song, const char *uri, bool secure)
|
2012-08-14 02:17:20 +02:00
|
|
|
{
|
2013-07-28 13:25:12 +02:00
|
|
|
Song *dest;
|
2012-08-14 02:17:20 +02:00
|
|
|
|
|
|
|
if (uri_has_scheme(uri)) {
|
2013-07-28 13:25:12 +02:00
|
|
|
dest = Song::NewRemote(uri);
|
2012-08-14 02:17:20 +02:00
|
|
|
} else if (g_path_is_absolute(uri) && secure) {
|
2013-07-28 13:25:12 +02:00
|
|
|
dest = Song::LoadFile(uri, nullptr);
|
2012-08-14 02:17:20 +02:00
|
|
|
if (dest == NULL)
|
|
|
|
return NULL;
|
|
|
|
} else {
|
2013-08-10 18:02:44 +02:00
|
|
|
const Database *db = GetDatabase(IgnoreError());
|
2013-01-03 00:30:15 +01:00
|
|
|
if (db == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
Song *tmp = db->GetSong(uri, IgnoreError());
|
2012-08-15 23:28:19 +02:00
|
|
|
if (tmp == NULL)
|
2012-08-14 02:17:20 +02:00
|
|
|
/* not found in database */
|
|
|
|
return NULL;
|
2012-08-15 23:28:19 +02:00
|
|
|
|
2013-07-28 13:25:12 +02:00
|
|
|
dest = tmp->DupDetached();
|
2013-01-03 00:30:15 +01:00
|
|
|
db->ReturnSong(tmp);
|
2012-08-14 02:17:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return apply_song_metadata(dest, song);
|
|
|
|
}
|
|
|
|
|
2013-07-28 13:25:12 +02:00
|
|
|
Song *
|
|
|
|
playlist_check_translate_song(Song *song, const char *base_uri,
|
2010-12-22 19:46:41 +01:00
|
|
|
bool secure)
|
2010-02-08 10:28:12 +01:00
|
|
|
{
|
2013-07-28 13:25:12 +02:00
|
|
|
if (song->IsInDatabase())
|
2010-02-08 10:28:12 +01:00
|
|
|
/* already ok */
|
|
|
|
return song;
|
|
|
|
|
2012-08-14 02:14:24 +02:00
|
|
|
const char *uri = song->uri;
|
2010-02-08 10:28:12 +01:00
|
|
|
|
|
|
|
if (uri_has_scheme(uri)) {
|
|
|
|
if (uri_supported_scheme(uri))
|
|
|
|
/* valid remote song */
|
|
|
|
return song;
|
|
|
|
else {
|
|
|
|
/* unsupported remote song */
|
2013-07-28 13:25:12 +02:00
|
|
|
song->Free();
|
2010-02-08 10:28:12 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-09 18:00:41 +02:00
|
|
|
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;
|
|
|
|
|
2010-02-08 10:28:12 +01:00
|
|
|
if (g_path_is_absolute(uri)) {
|
|
|
|
/* XXX fs_charset vs utf8? */
|
2012-08-14 02:12:36 +02:00
|
|
|
const char *suffix = map_to_relative_path(uri);
|
|
|
|
assert(suffix != NULL);
|
2010-02-08 10:28:12 +01:00
|
|
|
|
2012-08-14 02:12:36 +02:00
|
|
|
if (suffix != uri)
|
|
|
|
uri = suffix;
|
2010-12-22 19:46:41 +01:00
|
|
|
else if (!secure) {
|
2010-02-08 10:28:12 +01:00
|
|
|
/* local files must be relative to the music
|
2010-12-22 19:46:41 +01:00
|
|
|
directory when "secure" is enabled */
|
2013-07-28 13:25:12 +02:00
|
|
|
song->Free();
|
2010-02-08 10:28:12 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-10-06 22:21:24 +02:00
|
|
|
base_uri = NULL;
|
2010-02-08 10:28:12 +01:00
|
|
|
}
|
|
|
|
|
2012-08-14 02:14:24 +02:00
|
|
|
char *allocated = NULL;
|
2010-02-08 10:28:12 +01:00
|
|
|
if (base_uri != NULL)
|
2012-08-14 02:14:24 +02:00
|
|
|
uri = allocated = g_build_filename(base_uri, uri, NULL);
|
2010-02-08 10:28:12 +01:00
|
|
|
|
2013-07-28 13:25:12 +02:00
|
|
|
Song *dest = playlist_check_load_song(song, uri, secure);
|
|
|
|
song->Free();
|
2012-08-14 02:14:24 +02:00
|
|
|
g_free(allocated);
|
2010-02-08 10:28:12 +01:00
|
|
|
return dest;
|
|
|
|
}
|