2010-02-08 10:28:12 +01:00
|
|
|
/*
|
2014-01-13 22:30:36 +01:00
|
|
|
* Copyright (C) 2003-2014 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"
|
2014-01-24 16:18:50 +01:00
|
|
|
#include "db/DatabaseSong.hxx"
|
2013-01-03 17:34:51 +01:00
|
|
|
#include "ls.hxx"
|
2013-09-05 18:22:02 +02:00
|
|
|
#include "tag/Tag.hxx"
|
2014-01-07 21:39:47 +01:00
|
|
|
#include "tag/TagBuilder.hxx"
|
2013-10-17 21:59:35 +02:00
|
|
|
#include "fs/AllocatedPath.hxx"
|
2013-10-17 23:23:25 +02:00
|
|
|
#include "fs/Traits.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"
|
2014-01-07 21:39:47 +01:00
|
|
|
#include "DetachedSong.hxx"
|
2010-02-08 10:28:12 +01:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
static void
|
2014-01-20 23:48:46 +01:00
|
|
|
merge_song_metadata(DetachedSong &add, const DetachedSong &base)
|
2010-02-08 10:28:12 +01:00
|
|
|
{
|
2014-01-07 21:39:47 +01:00
|
|
|
{
|
2014-01-20 19:50:19 +01:00
|
|
|
TagBuilder builder(add.GetTag());
|
|
|
|
builder.Complement(base.GetTag());
|
2014-01-20 23:48:46 +01:00
|
|
|
add.SetTag(builder.Commit());
|
2014-01-07 21:39:47 +01:00
|
|
|
}
|
|
|
|
|
2014-01-20 23:48:46 +01:00
|
|
|
add.SetLastModified(base.GetLastModified());
|
2010-02-08 10:28:12 +01:00
|
|
|
}
|
|
|
|
|
2014-01-20 23:48:46 +01:00
|
|
|
static void
|
|
|
|
apply_song_metadata(DetachedSong &dest, const DetachedSong &src)
|
2010-02-08 10:28:12 +01:00
|
|
|
{
|
2014-01-20 19:50:19 +01:00
|
|
|
if (!src.GetTag().IsDefined() &&
|
|
|
|
src.GetStartMS() == 0 && src.GetEndMS() == 0)
|
2014-01-20 23:48:46 +01:00
|
|
|
return;
|
2010-02-08 10:28:12 +01:00
|
|
|
|
2014-01-20 23:48:46 +01:00
|
|
|
merge_song_metadata(dest, src);
|
2010-02-08 10:28:12 +01:00
|
|
|
|
2014-01-20 23:48:46 +01:00
|
|
|
if (dest.GetTag().IsDefined() && dest.GetTag().time > 0 &&
|
2014-01-20 19:50:19 +01:00
|
|
|
src.GetStartMS() > 0 && src.GetEndMS() == 0 &&
|
2014-01-20 23:48:46 +01:00
|
|
|
src.GetStartMS() / 1000 < (unsigned)dest.GetTag().time)
|
2010-11-08 20:16:26 +01:00
|
|
|
/* 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 */
|
2014-01-20 23:48:46 +01:00
|
|
|
dest.WritableTag().time =
|
|
|
|
dest.GetTag().time - src.GetStartMS() / 1000;
|
2010-02-08 10:28:12 +01:00
|
|
|
}
|
|
|
|
|
2014-01-20 23:48:46 +01:00
|
|
|
static bool
|
|
|
|
playlist_check_load_song(DetachedSong &song)
|
2012-08-14 02:17:20 +02:00
|
|
|
{
|
2014-01-20 23:48:46 +01:00
|
|
|
const char *const uri = song.GetURI();
|
2012-08-14 02:17:20 +02:00
|
|
|
|
|
|
|
if (uri_has_scheme(uri)) {
|
2014-01-20 23:48:46 +01:00
|
|
|
return true;
|
2014-01-20 23:58:44 +01:00
|
|
|
} else if (PathTraitsUTF8::IsAbsolute(uri)) {
|
2014-01-20 23:48:46 +01:00
|
|
|
DetachedSong tmp(uri);
|
|
|
|
if (!tmp.Update())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
apply_song_metadata(song, tmp);
|
|
|
|
return true;
|
2012-08-14 02:17:20 +02:00
|
|
|
} else {
|
2014-01-30 20:29:48 +01:00
|
|
|
#ifdef ENABLE_DATABASE
|
2014-01-20 23:48:46 +01:00
|
|
|
DetachedSong *tmp = DatabaseDetachSong(uri, IgnoreError());
|
|
|
|
if (tmp == nullptr)
|
|
|
|
return false;
|
2012-08-14 02:17:20 +02:00
|
|
|
|
2014-01-20 23:48:46 +01:00
|
|
|
apply_song_metadata(song, *tmp);
|
|
|
|
delete tmp;
|
|
|
|
return true;
|
2014-01-30 20:29:48 +01:00
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
2014-01-20 23:48:46 +01:00
|
|
|
}
|
2012-08-14 02:17:20 +02:00
|
|
|
}
|
|
|
|
|
2014-01-20 23:48:46 +01:00
|
|
|
bool
|
|
|
|
playlist_check_translate_song(DetachedSong &song, const char *base_uri,
|
2010-12-22 19:46:41 +01:00
|
|
|
bool secure)
|
2010-02-08 10:28:12 +01:00
|
|
|
{
|
2014-01-20 23:48:46 +01:00
|
|
|
const char *const uri = song.GetURI();
|
2010-02-08 10:28:12 +01:00
|
|
|
|
2014-01-20 23:48:46 +01:00
|
|
|
if (uri_has_scheme(uri))
|
|
|
|
/* valid remote song? */
|
|
|
|
return uri_supported_scheme(uri);
|
2010-02-08 10:28:12 +01:00
|
|
|
|
2013-10-02 08:13:28 +02:00
|
|
|
if (base_uri != nullptr && strcmp(base_uri, ".") == 0)
|
2013-12-04 22:53:43 +01:00
|
|
|
/* PathTraitsUTF8::GetParent() returns "." when there
|
2013-10-21 10:26:53 +02:00
|
|
|
is no directory name in the given path; clear that
|
|
|
|
now, because it would break the database lookup
|
2011-05-09 18:00:41 +02:00
|
|
|
functions */
|
2013-10-02 08:13:28 +02:00
|
|
|
base_uri = nullptr;
|
2011-05-09 18:00:41 +02:00
|
|
|
|
2013-12-04 22:53:43 +01:00
|
|
|
if (PathTraitsUTF8::IsAbsolute(uri)) {
|
2010-02-08 10:28:12 +01:00
|
|
|
/* XXX fs_charset vs utf8? */
|
2012-08-14 02:12:36 +02:00
|
|
|
const char *suffix = map_to_relative_path(uri);
|
2013-10-02 08:13:28 +02:00
|
|
|
assert(suffix != nullptr);
|
2010-02-08 10:28:12 +01:00
|
|
|
|
2012-08-14 02:12:36 +02:00
|
|
|
if (suffix != uri)
|
2014-01-20 23:48:46 +01:00
|
|
|
song.SetURI(std::string(suffix));
|
|
|
|
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 */
|
2014-01-20 23:48:46 +01:00
|
|
|
return false;
|
2010-02-08 10:28:12 +01:00
|
|
|
|
2013-10-02 08:13:28 +02:00
|
|
|
base_uri = nullptr;
|
2010-02-08 10:28:12 +01:00
|
|
|
}
|
|
|
|
|
2013-12-05 00:02:10 +01:00
|
|
|
if (base_uri != nullptr) {
|
2014-01-20 23:48:46 +01:00
|
|
|
song.SetURI(PathTraitsUTF8::Build(base_uri, uri));
|
2014-01-20 22:17:59 +01:00
|
|
|
/* repeat the above checks */
|
|
|
|
return playlist_check_translate_song(song, nullptr, secure);
|
2013-12-05 00:02:10 +01:00
|
|
|
}
|
2010-02-08 10:28:12 +01:00
|
|
|
|
2014-01-20 23:48:46 +01:00
|
|
|
return playlist_check_load_song(song);
|
2010-02-08 10:28:12 +01:00
|
|
|
}
|