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"
|
2004-02-24 00:41:20 +01:00
|
|
|
#include "song.h"
|
2009-02-25 16:44:06 +01:00
|
|
|
#include "uri.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
#include "directory.h"
|
2009-02-15 17:48:37 +01:00
|
|
|
#include "tag.h"
|
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>
|
|
|
|
|
2008-10-08 11:05:25 +02:00
|
|
|
static struct song *
|
2009-10-13 18:01:06 +02:00
|
|
|
song_alloc(const char *uri, struct directory *parent)
|
2008-10-06 18:46:52 +02:00
|
|
|
{
|
2009-10-13 18:01:06 +02:00
|
|
|
size_t uri_length;
|
2008-10-08 10:49:11 +02:00
|
|
|
struct song *song;
|
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);
|
|
|
|
song = g_malloc(sizeof(*song) - sizeof(song->uri) + uri_length + 1);
|
2008-10-06 18:46:52 +02:00
|
|
|
|
|
|
|
song->tag = NULL;
|
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
|
|
|
|
2008-10-08 10:49:11 +02:00
|
|
|
struct song *
|
2009-10-13 18:01:06 +02:00
|
|
|
song_remote_new(const char *uri)
|
2008-10-08 11:05:25 +02:00
|
|
|
{
|
2009-10-13 18:01:06 +02:00
|
|
|
return song_alloc(uri, NULL);
|
2008-10-08 11:05:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct song *
|
|
|
|
song_file_new(const char *path, struct directory *parent)
|
|
|
|
{
|
2008-10-15 22:35:13 +02:00
|
|
|
assert((parent == NULL) == (*path == '/'));
|
2008-10-08 11:05:25 +02:00
|
|
|
|
|
|
|
return song_alloc(path, parent);
|
|
|
|
}
|
|
|
|
|
2008-10-08 10:49:11 +02:00
|
|
|
void
|
2008-10-08 11:05:34 +02:00
|
|
|
song_free(struct song *song)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
if (song->tag)
|
2008-08-29 09:38:21 +02:00
|
|
|
tag_free(song->tag);
|
2009-01-03 14:51:30 +01:00
|
|
|
g_free(song);
|
2004-04-10 19:56:01 +02:00
|
|
|
}
|
|
|
|
|
2008-10-08 10:49:11 +02:00
|
|
|
char *
|
2009-01-04 19:09:34 +01:00
|
|
|
song_get_uri(const struct song *song)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-10-08 11:06:37 +02:00
|
|
|
assert(song != NULL);
|
2009-10-13 18:01:06 +02:00
|
|
|
assert(*song->uri);
|
2008-08-26 08:27:11 +02:00
|
|
|
|
2009-01-04 19:08:52 +01:00
|
|
|
if (!song_in_database(song) || directory_is_root(song->parent))
|
2009-10-13 18:01:06 +02:00
|
|
|
return g_strdup(song->uri);
|
2007-12-28 03:56:25 +01:00
|
|
|
else
|
2009-01-04 19:09:34 +01:00
|
|
|
return g_strconcat(directory_get_path(song->parent),
|
2009-10-13 18:01:06 +02:00
|
|
|
"/", song->uri, NULL);
|
2004-11-11 03:36:25 +01:00
|
|
|
}
|
2009-12-26 13:56:35 +01:00
|
|
|
|
|
|
|
double
|
|
|
|
song_get_duration(const struct song *song)
|
|
|
|
{
|
2009-12-25 22:59:13 +01:00
|
|
|
if (song->end_ms > 0)
|
|
|
|
return (song->end_ms - song->start_ms) / 1000.0;
|
|
|
|
|
2009-12-26 13:56:35 +01:00
|
|
|
if (song->tag == NULL)
|
|
|
|
return 0;
|
|
|
|
|
2009-12-25 22:59:13 +01:00
|
|
|
return song->tag->time - song->start_ms / 1000.0;
|
2009-12-26 13:56:35 +01:00
|
|
|
}
|