Song.cxx 3.86 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2011 The Music Player Daemon Project
3
 * http://www.musicpd.org
Warren Dukes's avatar
Warren Dukes committed
4 5 6 7 8 9 10 11 12 13
 *
 * 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.
14 15 16 17
 *
 * 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.
Warren Dukes's avatar
Warren Dukes committed
18 19
 */

20
#include "config.h"
Warren Dukes's avatar
Warren Dukes committed
21
#include "song.h"
22
#include "Directory.hxx"
Max Kellermann's avatar
Max Kellermann committed
23 24

extern "C" {
25
#include "tag.h"
Max Kellermann's avatar
Max Kellermann committed
26
}
27

28 29
#include <glib.h>

Max Kellermann's avatar
Max Kellermann committed
30 31
#include <assert.h>

32
Directory detached_root;
33

34
static struct song *
35
song_alloc(const char *uri, Directory *parent)
36
{
37
	size_t uri_length;
38

39 40 41
	assert(uri);
	uri_length = strlen(uri);
	assert(uri_length);
42

Max Kellermann's avatar
Max Kellermann committed
43 44 45 46
	struct song *song = (struct song *)
		g_malloc(sizeof(*song) - sizeof(song->uri) + uri_length + 1);

	song->tag = nullptr;
47
	memcpy(song->uri, uri, uri_length + 1);
Max Kellermann's avatar
Max Kellermann committed
48
	song->parent = parent;
49
	song->mtime = 0;
50
	song->start_ms = song->end_ms = 0;
51 52 53

	return song;
}
54

55
struct song *
56
song_remote_new(const char *uri)
57
{
Max Kellermann's avatar
Max Kellermann committed
58
	return song_alloc(uri, nullptr);
59 60 61
}

struct song *
62
song_file_new(const char *path, Directory *parent)
63
{
Max Kellermann's avatar
Max Kellermann committed
64
	assert((parent == nullptr) == (*path == '/'));
65 66 67 68

	return song_alloc(path, parent);
}

69 70 71 72 73 74 75 76 77 78 79 80
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;
}

81 82 83 84 85 86 87 88
struct song *
song_detached_new(const char *uri)
{
	assert(uri != nullptr);

	return song_alloc(uri, &detached_root);
}

89 90 91 92 93 94 95 96
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);
97
		song = song_detached_new(uri);
98 99 100 101 102 103 104 105 106 107 108 109
		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;
}

110
void
Max Kellermann's avatar
Max Kellermann committed
111
song_free(struct song *song)
Avuton Olrich's avatar
Avuton Olrich committed
112 113
{
	if (song->tag)
114
		tag_free(song->tag);
115
	g_free(song);
116 117
}

118 119
gcc_pure
static inline bool
120
directory_equals(const Directory &a, const Directory &b)
121 122 123 124 125 126
{
	return strcmp(a.path, b.path) == 0;
}

gcc_pure
static inline bool
127
directory_is_same(const Directory *a, const Directory *b)
128 129 130 131 132 133 134 135 136 137 138 139 140
{
	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);

141 142 143 144 145 146 147 148 149 150 151 152 153
	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;
	}

154 155 156 157
	return directory_is_same(a->parent, b->parent) &&
		strcmp(a->uri, b->uri) == 0;
}

158
char *
159
song_get_uri(const struct song *song)
Avuton Olrich's avatar
Avuton Olrich committed
160
{
Max Kellermann's avatar
Max Kellermann committed
161
	assert(song != nullptr);
162
	assert(*song->uri);
163

164
	if (!song_in_database(song) || song->parent->IsRoot())
165
		return g_strdup(song->uri);
Eric Wong's avatar
Eric Wong committed
166
	else
167
		return g_strconcat(song->parent->GetPath(),
Max Kellermann's avatar
Max Kellermann committed
168
				   "/", song->uri, nullptr);
169
}
170 171 172 173

double
song_get_duration(const struct song *song)
{
174 175 176
	if (song->end_ms > 0)
		return (song->end_ms - song->start_ms) / 1000.0;

Max Kellermann's avatar
Max Kellermann committed
177
	if (song->tag == nullptr)
178 179
		return 0;

180
	return song->tag->time - song->start_ms / 1000.0;
181
}