2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2011-01-29 10:13:54 +01:00
|
|
|
* Copyright (C) 2003-2011 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2004-02-24 00:41:20 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2004-02-24 00:41:20 +01:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2013-07-28 13:25:12 +02:00
|
|
|
#include "Song.hxx"
|
2013-01-02 22:52:08 +01:00
|
|
|
#include "Directory.hxx"
|
2013-09-05 18:22:02 +02:00
|
|
|
#include "tag/Tag.hxx"
|
2008-09-23 20:48:39 +02:00
|
|
|
|
2009-01-03 14:51:30 +01:00
|
|
|
#include <glib.h>
|
|
|
|
|
2008-12-29 17:28:32 +01:00
|
|
|
#include <assert.h>
|
2013-07-30 20:11:57 +02:00
|
|
|
#include <string.h>
|
2008-12-29 17:28:32 +01:00
|
|
|
|
2013-01-02 23:06:20 +01:00
|
|
|
Directory detached_root;
|
2012-08-15 22:44:21 +02:00
|
|
|
|
2013-07-28 13:25:12 +02:00
|
|
|
static Song *
|
2013-01-02 23:06:20 +01:00
|
|
|
song_alloc(const char *uri, Directory *parent)
|
2008-10-06 18:46:52 +02:00
|
|
|
{
|
2009-10-13 18:01:06 +02:00
|
|
|
size_t uri_length;
|
2008-10-07 21:20:34 +02:00
|
|
|
|
2009-10-13 18:01:06 +02:00
|
|
|
assert(uri);
|
|
|
|
uri_length = strlen(uri);
|
|
|
|
assert(uri_length);
|
2008-10-06 18:46:52 +02:00
|
|
|
|
2013-07-28 13:25:12 +02:00
|
|
|
Song *song = (Song *)
|
2012-08-08 20:12:20 +02:00
|
|
|
g_malloc(sizeof(*song) - sizeof(song->uri) + uri_length + 1);
|
|
|
|
|
|
|
|
song->tag = nullptr;
|
2009-10-13 18:01:06 +02:00
|
|
|
memcpy(song->uri, uri, uri_length + 1);
|
2008-10-08 11:05:34 +02:00
|
|
|
song->parent = parent;
|
2009-07-06 11:32:31 +02:00
|
|
|
song->mtime = 0;
|
2009-12-25 22:59:13 +01:00
|
|
|
song->start_ms = song->end_ms = 0;
|
2008-10-06 18:46:52 +02:00
|
|
|
|
|
|
|
return song;
|
|
|
|
}
|
2004-03-10 10:55:54 +01:00
|
|
|
|
2013-07-28 13:25:12 +02:00
|
|
|
Song *
|
|
|
|
Song::NewRemote(const char *uri)
|
2008-10-08 11:05:25 +02:00
|
|
|
{
|
2012-08-08 20:12:20 +02:00
|
|
|
return song_alloc(uri, nullptr);
|
2008-10-08 11:05:25 +02:00
|
|
|
}
|
|
|
|
|
2013-07-28 13:25:12 +02:00
|
|
|
Song *
|
|
|
|
Song::NewFile(const char *path, Directory *parent)
|
2008-10-08 11:05:25 +02:00
|
|
|
{
|
2012-08-08 20:12:20 +02:00
|
|
|
assert((parent == nullptr) == (*path == '/'));
|
2008-10-08 11:05:25 +02:00
|
|
|
|
|
|
|
return song_alloc(path, parent);
|
|
|
|
}
|
|
|
|
|
2013-07-28 13:25:12 +02:00
|
|
|
Song *
|
|
|
|
Song::ReplaceURI(const char *new_uri)
|
2012-02-12 19:47:22 +01:00
|
|
|
{
|
2013-07-28 13:25:12 +02:00
|
|
|
Song *new_song = song_alloc(new_uri, parent);
|
|
|
|
new_song->tag = tag;
|
|
|
|
new_song->mtime = mtime;
|
|
|
|
new_song->start_ms = start_ms;
|
|
|
|
new_song->end_ms = end_ms;
|
|
|
|
g_free(this);
|
2012-02-12 19:47:22 +01:00
|
|
|
return new_song;
|
|
|
|
}
|
|
|
|
|
2013-07-28 13:25:12 +02:00
|
|
|
Song *
|
|
|
|
Song::NewDetached(const char *uri)
|
2012-08-15 20:57:45 +02:00
|
|
|
{
|
|
|
|
assert(uri != nullptr);
|
|
|
|
|
|
|
|
return song_alloc(uri, &detached_root);
|
|
|
|
}
|
|
|
|
|
2013-07-28 13:25:12 +02:00
|
|
|
Song *
|
|
|
|
Song::DupDetached() const
|
2012-08-15 22:44:21 +02:00
|
|
|
{
|
2013-07-28 13:25:12 +02:00
|
|
|
Song *song;
|
|
|
|
if (IsInDatabase()) {
|
2013-10-17 01:01:15 +02:00
|
|
|
const auto new_uri = GetURI();
|
|
|
|
song = NewDetached(new_uri.c_str());
|
2012-08-15 22:44:21 +02:00
|
|
|
} else
|
2013-07-28 13:25:12 +02:00
|
|
|
song = song_alloc(uri, nullptr);
|
2012-08-15 22:44:21 +02:00
|
|
|
|
2013-07-30 20:11:57 +02:00
|
|
|
song->tag = tag != nullptr ? new Tag(*tag) : nullptr;
|
2013-07-28 13:25:12 +02:00
|
|
|
song->mtime = mtime;
|
|
|
|
song->start_ms = start_ms;
|
|
|
|
song->end_ms = end_ms;
|
2012-08-15 22:44:21 +02:00
|
|
|
|
|
|
|
return song;
|
|
|
|
}
|
|
|
|
|
2008-10-08 10:49:11 +02:00
|
|
|
void
|
2013-07-28 13:25:12 +02:00
|
|
|
Song::Free()
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2013-07-30 20:11:57 +02:00
|
|
|
delete tag;
|
2013-07-28 13:25:12 +02:00
|
|
|
g_free(this);
|
2004-04-10 19:56:01 +02:00
|
|
|
}
|
|
|
|
|
2012-08-09 21:20:24 +02:00
|
|
|
gcc_pure
|
|
|
|
static inline bool
|
2013-01-02 23:06:20 +01:00
|
|
|
directory_equals(const Directory &a, const Directory &b)
|
2012-08-09 21:20:24 +02:00
|
|
|
{
|
|
|
|
return strcmp(a.path, b.path) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
gcc_pure
|
|
|
|
static inline bool
|
2013-01-02 23:06:20 +01:00
|
|
|
directory_is_same(const Directory *a, const Directory *b)
|
2012-08-09 21:20:24 +02:00
|
|
|
{
|
|
|
|
return a == b ||
|
|
|
|
(a != nullptr && b != nullptr &&
|
|
|
|
directory_equals(*a, *b));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-10-21 23:19:15 +02:00
|
|
|
SongEquals(const Song &a, const Song &b)
|
2012-08-09 21:20:24 +02:00
|
|
|
{
|
2013-10-21 23:19:15 +02:00
|
|
|
if (a.parent != nullptr && b.parent != nullptr &&
|
|
|
|
!directory_equals(*a.parent, *b.parent) &&
|
|
|
|
(a.parent == &detached_root || b.parent == &detached_root)) {
|
2012-08-15 22:44:21 +02:00
|
|
|
/* must compare the full URI if one of the objects is
|
|
|
|
"detached" */
|
2013-10-21 23:19:15 +02:00
|
|
|
const auto au = a.GetURI();
|
|
|
|
const auto bu = b.GetURI();
|
2013-10-17 01:01:15 +02:00
|
|
|
return au == bu;
|
2012-08-15 22:44:21 +02:00
|
|
|
}
|
|
|
|
|
2013-10-21 23:19:15 +02:00
|
|
|
return directory_is_same(a.parent, b.parent) &&
|
|
|
|
strcmp(a.uri, b.uri) == 0;
|
2012-08-09 21:20:24 +02:00
|
|
|
}
|
|
|
|
|
2013-10-17 01:01:15 +02:00
|
|
|
std::string
|
2013-07-28 13:25:12 +02:00
|
|
|
Song::GetURI() const
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2013-07-28 13:25:12 +02:00
|
|
|
assert(*uri);
|
2008-08-26 08:27:11 +02:00
|
|
|
|
2013-07-28 13:25:12 +02:00
|
|
|
if (!IsInDatabase() || parent->IsRoot())
|
2013-10-17 01:01:15 +02:00
|
|
|
return std::string(uri);
|
|
|
|
else {
|
|
|
|
const char *path = parent->GetPath();
|
|
|
|
|
|
|
|
std::string result;
|
|
|
|
result.reserve(strlen(path) + 1 + strlen(uri));
|
|
|
|
result.assign(path);
|
|
|
|
result.push_back('/');
|
|
|
|
result.append(uri);
|
|
|
|
return result;
|
|
|
|
}
|
2004-11-11 03:36:25 +01:00
|
|
|
}
|
2009-12-26 13:56:35 +01:00
|
|
|
|
|
|
|
double
|
2013-07-28 13:25:12 +02:00
|
|
|
Song::GetDuration() const
|
2009-12-26 13:56:35 +01:00
|
|
|
{
|
2013-07-28 13:25:12 +02:00
|
|
|
if (end_ms > 0)
|
|
|
|
return (end_ms - start_ms) / 1000.0;
|
2009-12-25 22:59:13 +01:00
|
|
|
|
2013-07-28 13:25:12 +02:00
|
|
|
if (tag == nullptr)
|
2009-12-26 13:56:35 +01:00
|
|
|
return 0;
|
|
|
|
|
2013-07-28 13:25:12 +02:00
|
|
|
return tag->time - start_ms / 1000.0;
|
2009-12-26 13:56:35 +01:00
|
|
|
}
|