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"
|
|
|
|
#include "directory.h"
|
2012-08-08 20:12:20 +02:00
|
|
|
|
|
|
|
extern "C" {
|
2009-02-15 17:48:37 +01:00
|
|
|
#include "tag.h"
|
2012-08-08 20:12:20 +02:00
|
|
|
}
|
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>
|
|
|
|
|
2012-08-15 22:44:21 +02:00
|
|
|
struct directory detached_root;
|
|
|
|
|
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-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
|
|
|
|
2012-08-08 20:12:20 +02:00
|
|
|
struct song *song = (struct song *)
|
|
|
|
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
|
|
|
|
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
|
|
|
{
|
2012-08-08 20:12:20 +02:00
|
|
|
return song_alloc(uri, nullptr);
|
2008-10-08 11:05:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct song *
|
|
|
|
song_file_new(const char *path, struct directory *parent)
|
|
|
|
{
|
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);
|
|
|
|
}
|
|
|
|
|
2012-02-12 19:47:22 +01:00
|
|
|
struct song *
|
|
|
|
song_replace_uri(struct song *old_song, const char *uri)
|
|
|
|
{
|
|
|
|
struct song *new_song = song_alloc(uri, old_song->parent);
|
|
|
|
new_song->tag = old_song->tag;
|
|
|
|
new_song->mtime = old_song->mtime;
|
|
|
|
new_song->start_ms = old_song->start_ms;
|
|
|
|
new_song->end_ms = old_song->end_ms;
|
|
|
|
g_free(old_song);
|
|
|
|
return new_song;
|
|
|
|
}
|
|
|
|
|
2012-08-15 20:57:45 +02:00
|
|
|
struct song *
|
|
|
|
song_detached_new(const char *uri)
|
|
|
|
{
|
|
|
|
assert(uri != nullptr);
|
|
|
|
|
|
|
|
return song_alloc(uri, &detached_root);
|
|
|
|
}
|
|
|
|
|
2012-08-15 22:44:21 +02:00
|
|
|
struct song *
|
|
|
|
song_dup_detached(const struct song *src)
|
|
|
|
{
|
|
|
|
assert(src != nullptr);
|
|
|
|
|
|
|
|
struct song *song;
|
|
|
|
if (song_in_database(src)) {
|
|
|
|
char *uri = song_get_uri(src);
|
2012-08-15 20:57:45 +02:00
|
|
|
song = song_detached_new(uri);
|
2012-08-15 22:44:21 +02:00
|
|
|
g_free(uri);
|
|
|
|
} else
|
|
|
|
song = song_alloc(src->uri, nullptr);
|
|
|
|
|
|
|
|
song->tag = tag_dup(src->tag);
|
|
|
|
song->mtime = src->mtime;
|
|
|
|
song->start_ms = src->start_ms;
|
|
|
|
song->end_ms = src->end_ms;
|
|
|
|
|
|
|
|
return song;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-08-09 21:20:24 +02:00
|
|
|
gcc_pure
|
|
|
|
static inline bool
|
|
|
|
directory_equals(const struct directory &a, const struct directory &b)
|
|
|
|
{
|
|
|
|
return strcmp(a.path, b.path) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
gcc_pure
|
|
|
|
static inline bool
|
|
|
|
directory_is_same(const struct directory *a, const struct directory *b)
|
|
|
|
{
|
|
|
|
return a == b ||
|
|
|
|
(a != nullptr && b != nullptr &&
|
|
|
|
directory_equals(*a, *b));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
song_equals(const struct song *a, const struct song *b)
|
|
|
|
{
|
|
|
|
assert(a != nullptr);
|
|
|
|
assert(b != nullptr);
|
|
|
|
|
2012-08-15 22:44:21 +02:00
|
|
|
if (a->parent != nullptr && b->parent != nullptr &&
|
|
|
|
!directory_equals(*a->parent, *b->parent) &&
|
|
|
|
(a->parent == &detached_root || b->parent == &detached_root)) {
|
|
|
|
/* must compare the full URI if one of the objects is
|
|
|
|
"detached" */
|
|
|
|
char *au = song_get_uri(a);
|
|
|
|
char *bu = song_get_uri(b);
|
|
|
|
const bool result = strcmp(au, bu) == 0;
|
|
|
|
g_free(bu);
|
|
|
|
g_free(au);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-08-09 21:20:24 +02:00
|
|
|
return directory_is_same(a->parent, b->parent) &&
|
|
|
|
strcmp(a->uri, b->uri) == 0;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2012-08-08 20:12:20 +02:00
|
|
|
assert(song != nullptr);
|
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),
|
2012-08-08 20:12:20 +02:00
|
|
|
"/", song->uri, nullptr);
|
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;
|
|
|
|
|
2012-08-08 20:12:20 +02:00
|
|
|
if (song->tag == nullptr)
|
2009-12-26 13:56:35 +01:00
|
|
|
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
|
|
|
}
|