2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2014-01-13 22:30:36 +01:00
|
|
|
* Copyright (C) 2003-2014 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"
|
2014-01-07 23:33:46 +01:00
|
|
|
#include "util/Alloc.hxx"
|
2014-01-07 21:39:47 +01:00
|
|
|
#include "DetachedSong.hxx"
|
2009-01-03 14:51:30 +01:00
|
|
|
|
2008-12-29 17:28:32 +01:00
|
|
|
#include <assert.h>
|
2013-07-30 20:11:57 +02:00
|
|
|
#include <string.h>
|
2014-01-07 23:33:46 +01:00
|
|
|
#include <stdlib.h>
|
2008-12-29 17:28:32 +01: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 *)
|
2014-01-07 23:33:46 +01:00
|
|
|
xalloc(sizeof(*song) - sizeof(song->uri) + uri_length + 1);
|
2012-08-08 20:12:20 +02:00
|
|
|
|
|
|
|
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 *
|
2014-01-07 21:39:47 +01:00
|
|
|
Song::NewFrom(DetachedSong &&other, Directory *parent)
|
2008-10-08 11:05:25 +02:00
|
|
|
{
|
2014-01-07 21:39:47 +01:00
|
|
|
Song *song = song_alloc(other.GetURI(), parent);
|
|
|
|
song->tag = new Tag(std::move(other.WritableTag()));
|
|
|
|
song->mtime = other.GetLastModified();
|
|
|
|
song->start_ms = other.GetStartMS();
|
|
|
|
song->end_ms = other.GetEndMS();
|
|
|
|
return song;
|
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
|
|
|
{
|
|
|
|
return song_alloc(path, parent);
|
|
|
|
}
|
|
|
|
|
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;
|
2014-01-07 23:33:46 +01:00
|
|
|
free(this);
|
2004-04-10 19:56:01 +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
|
|
|
|
2014-01-07 21:39:47 +01:00
|
|
|
if (parent == nullptr || 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
|
|
|
}
|