SongPrint.cxx 3.39 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2015 The Music Player Daemon Project
3
 * http://www.musicpd.org
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.
18 19
 */

20
#include "config.h"
Max Kellermann's avatar
Max Kellermann committed
21
#include "SongPrint.hxx"
Max Kellermann's avatar
Max Kellermann committed
22
#include "db/LightSong.hxx"
23
#include "storage/StorageInterface.hxx"
24
#include "DetachedSong.hxx"
Max Kellermann's avatar
Max Kellermann committed
25 26
#include "TimePrint.hxx"
#include "TagPrint.hxx"
27
#include "client/Client.hxx"
28
#include "fs/Traits.hxx"
Max Kellermann's avatar
Max Kellermann committed
29
#include "util/UriUtil.hxx"
30

31 32
#define SONG_FILE "file: "

33
static void
34
song_print_uri(Client &client, const char *uri, bool base)
35
{
36 37 38 39 40
	std::string allocated;

	if (base) {
		uri = PathTraitsUTF8::GetBase(uri);
	} else {
41
#ifdef ENABLE_DATABASE
42 43 44 45 46 47
		const Storage *storage = client.GetStorage();
		if (storage != nullptr) {
			const char *suffix = storage->MapToRelativeUTF8(uri);
			if (suffix != nullptr)
				uri = suffix;
		}
48 49
#endif

50 51 52 53
		allocated = uri_remove_auth(uri);
		if (!allocated.empty())
			uri = allocated.c_str();
	}
54

55
	client_printf(client, "%s%s\n", SONG_FILE, uri);
56 57
}

58
void
59
song_print_uri(Client &client, const LightSong &song, bool base)
60
{
61
	if (!base && song.directory != nullptr) {
62
		client_printf(client, "%s%s/%s\n", SONG_FILE,
63
			      song.directory, song.uri);
64
	} else
65
		song_print_uri(client, song.uri, base);
66
}
67

68
void
69
song_print_uri(Client &client, const DetachedSong &song, bool base)
70
{
71
	song_print_uri(client, song.GetURI(), base);
72 73
}

74
void
75
song_print_info(Client &client, const LightSong &song, bool base)
76
{
77
	song_print_uri(client, song, base);
78

79 80 81 82
	const unsigned start_ms = song.start_time.ToMS();
	const unsigned end_ms = song.end_time.ToMS();

	if (end_ms > 0)
83
		client_printf(client, "Range: %u.%03u-%u.%03u\n",
84 85 86 87 88
			      start_ms / 1000,
			      start_ms % 1000,
			      end_ms / 1000,
			      end_ms % 1000);
	else if (start_ms > 0)
89
		client_printf(client, "Range: %u.%03u-\n",
90 91
			      start_ms / 1000,
			      start_ms % 1000);
92

93 94
	if (song.mtime > 0)
		time_print(client, "Last-Modified", song.mtime);
95

96
	tag_print(client, *song.tag);
97
}
98 99

void
100
song_print_info(Client &client, const DetachedSong &song, bool base)
101
{
102
	song_print_uri(client, song, base);
103

104 105
	const unsigned start_ms = song.GetStartTime().ToMS();
	const unsigned end_ms = song.GetEndTime().ToMS();
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

	if (end_ms > 0)
		client_printf(client, "Range: %u.%03u-%u.%03u\n",
			      start_ms / 1000,
			      start_ms % 1000,
			      end_ms / 1000,
			      end_ms % 1000);
	else if (start_ms > 0)
		client_printf(client, "Range: %u.%03u-\n",
			      start_ms / 1000,
			      start_ms % 1000);

	if (song.GetLastModified() > 0)
		time_print(client, "Last-Modified", song.GetLastModified());

121 122
	tag_print_values(client, song.GetTag());

123 124
	const auto duration = song.GetDuration();
	if (!duration.IsNegative())
125 126 127 128
		client_printf(client, "Time: %i\n"
			      "duration: %1.3f\n",
			      duration.RoundS(),
			      duration.ToDoubleS());
129
}