Song.cxx 3.62 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2013 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"
21
#include "Song.hxx"
22
#include "Directory.hxx"
23
#include "tag/Tag.hxx"
24

25 26
#include <glib.h>

Max Kellermann's avatar
Max Kellermann committed
27
#include <assert.h>
Max Kellermann's avatar
Max Kellermann committed
28
#include <string.h>
Max Kellermann's avatar
Max Kellermann committed
29

30
Directory detached_root;
31

32
static Song *
33
song_alloc(const char *uri, Directory *parent)
34
{
35
	size_t uri_length;
36

37 38 39
	assert(uri);
	uri_length = strlen(uri);
	assert(uri_length);
40

41
	Song *song = (Song *)
Max Kellermann's avatar
Max Kellermann committed
42 43 44
		g_malloc(sizeof(*song) - sizeof(song->uri) + uri_length + 1);

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

	return song;
}
52

53 54
Song *
Song::NewRemote(const char *uri)
55
{
Max Kellermann's avatar
Max Kellermann committed
56
	return song_alloc(uri, nullptr);
57 58
}

59 60
Song *
Song::NewFile(const char *path, Directory *parent)
61
{
Max Kellermann's avatar
Max Kellermann committed
62
	assert((parent == nullptr) == (*path == '/'));
63 64 65 66

	return song_alloc(path, parent);
}

67 68
Song *
Song::ReplaceURI(const char *new_uri)
69
{
70 71 72 73 74 75
	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);
76 77 78
	return new_song;
}

79 80
Song *
Song::NewDetached(const char *uri)
81 82 83 84 85 86
{
	assert(uri != nullptr);

	return song_alloc(uri, &detached_root);
}

87 88
Song *
Song::DupDetached() const
89
{
90 91
	Song *song;
	if (IsInDatabase()) {
92 93
		const auto new_uri = GetURI();
		song = NewDetached(new_uri.c_str());
94
	} else
95
		song = song_alloc(uri, nullptr);
96

Max Kellermann's avatar
Max Kellermann committed
97
	song->tag = tag != nullptr ? new Tag(*tag) : nullptr;
98 99 100
	song->mtime = mtime;
	song->start_ms = start_ms;
	song->end_ms = end_ms;
101 102 103 104

	return song;
}

105
void
106
Song::Free()
Avuton Olrich's avatar
Avuton Olrich committed
107
{
Max Kellermann's avatar
Max Kellermann committed
108
	delete tag;
109
	g_free(this);
110 111
}

112 113 114 115 116 117 118 119
void
Song::ReplaceTag(Tag &&_tag)
{
	if (tag == nullptr)
		tag = new Tag();
	*tag = std::move(_tag);
}

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

gcc_pure
static inline bool
129
directory_is_same(const Directory *a, const Directory *b)
130 131 132 133 134 135 136 137
{
	return a == b ||
		(a != nullptr && b != nullptr &&
		 directory_equals(*a, *b));

}

bool
138
SongEquals(const Song &a, const Song &b)
139
{
140 141 142
	if (a.parent != nullptr && b.parent != nullptr &&
	    !directory_equals(*a.parent, *b.parent) &&
	    (a.parent == &detached_root || b.parent == &detached_root)) {
143 144
		/* must compare the full URI if one of the objects is
		   "detached" */
145 146
		const auto au = a.GetURI();
		const auto bu = b.GetURI();
147
		return au == bu;
148 149
	}

150 151
	return directory_is_same(a.parent, b.parent) &&
		strcmp(a.uri, b.uri) == 0;
152 153
}

154
std::string
155
Song::GetURI() const
Avuton Olrich's avatar
Avuton Olrich committed
156
{
157
	assert(*uri);
158

159
	if (!IsInDatabase() || parent->IsRoot())
160 161 162 163 164 165 166 167 168 169 170
		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;
	}
171
}
172 173

double
174
Song::GetDuration() const
175
{
176 177
	if (end_ms > 0)
		return (end_ms - start_ms) / 1000.0;
178

179
	if (tag == nullptr)
180 181
		return 0;

182
	return tag->time - start_ms / 1000.0;
183
}