DetachedSong.hxx 5.93 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * http://www.musicpd.org
 *
 * 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.
 *
 * 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.
 */

#ifndef MPD_DETACHED_SONG_HXX
#define MPD_DETACHED_SONG_HXX

#include "tag/Tag.hxx"
24
#include "pcm/AudioFormat.hxx"
25
#include "Chrono.hxx"
26

27
#include <chrono>
28 29 30
#include <string>
#include <utility>

31
struct LightSong;
32
class Storage;
33
class Path;
34 35

class DetachedSong {
36 37
	friend DetachedSong DatabaseDetachSong(const Storage &db,
					       const LightSong &song);
38

39 40 41 42 43 44 45 46 47 48 49 50 51
	/**
	 * An UTF-8-encoded URI referring to the song file.  This can
	 * be one of:
	 *
	 * - an absolute URL with a scheme
	 *   (e.g. "http://example.com/foo.mp3")
	 *
	 * - an absolute file name
	 *
	 * - a file name relative to the music directory
	 */
	std::string uri;

52 53 54 55 56 57 58 59 60 61
	/**
	 * The "real" URI, the one to be used for opening the
	 * resource.  If this attribute is empty, then #uri shall be
	 * used.
	 *
	 * This attribute is used for songs from the database which
	 * have a relative URI.
	 */
	std::string real_uri;

62 63
	Tag tag;

64 65 66 67 68 69
	/**
	 * The time stamp of the last file modification.  A negative
	 * value means that this is unknown/unavailable.
	 */
	std::chrono::system_clock::time_point mtime =
		std::chrono::system_clock::time_point::min();
70 71

	/**
72
	 * Start of this sub-song within the file.
73
	 */
74
	SongTime start_time = SongTime::zero();
75 76

	/**
77
	 * End of this sub-song within the file.
78 79
	 * Unused if zero.
	 */
80
	SongTime end_time = SongTime::zero();
81

82 83 84 85 86 87
	/**
	 * The audio format of the song, if given by the decoder
	 * plugin.  May be undefined if unknown.
	 */
	AudioFormat audio_format = AudioFormat::Undefined();

88
public:
89
	explicit DetachedSong(const char *_uri) noexcept
90
		:uri(_uri) {}
91

92
	explicit DetachedSong(const std::string &_uri) noexcept
93
		:uri(_uri) {}
94

95
	explicit DetachedSong(std::string &&_uri) noexcept
96
		:uri(std::move(_uri)) {}
97 98

	template<typename U>
99
	DetachedSong(U &&_uri, Tag &&_tag) noexcept
100
		:uri(std::forward<U>(_uri)),
101
		 tag(std::move(_tag)) {}
102

103 104 105 106 107
	/**
	 * Copy data from a #LightSong instance.  Usually, you should
	 * call DatabaseDetachSong() instead, which initializes
	 * #real_uri properly using Storage::MapUTF8().
	 */
108
	explicit DetachedSong(const LightSong &other) noexcept;
109

110
	~DetachedSong() noexcept = default;
111

112 113 114 115
	/* these are declared because the user-defined destructor
	   above prevents them from being generated implicitly */
	explicit DetachedSong(const DetachedSong &) = default;
	DetachedSong(DetachedSong &&) = default;
116
	DetachedSong &operator=(DetachedSong &&) = default;
117

118
	[[gnu::pure]]
119
	explicit operator LightSong() const noexcept;
120

121
	[[gnu::pure]]
122
	const char *GetURI() const noexcept {
123 124 125 126
		return uri.c_str();
	}

	template<typename T>
127
	void SetURI(T &&_uri) noexcept {
128 129 130
		uri = std::forward<T>(_uri);
	}

131 132 133 134
	/**
	 * Does this object have a "real" URI different from the
	 * displayed URI?
	 */
135
	[[gnu::pure]]
136
	bool HasRealURI() const noexcept {
137 138 139 140 141 142 143
		return !real_uri.empty();
	}

	/**
	 * Returns "real" URI (#real_uri) and falls back to just
	 * GetURI().
	 */
144
	[[gnu::pure]]
145
	const char *GetRealURI() const noexcept {
146 147 148 149
		return (HasRealURI() ? real_uri : uri).c_str();
	}

	template<typename T>
150
	void SetRealURI(T &&_uri) noexcept {
151 152 153
		real_uri = std::forward<T>(_uri);
	}

154 155 156 157
	/**
	 * Returns true if both objects refer to the same physical
	 * song.
	 */
158
	[[gnu::pure]]
159
	bool IsSame(const DetachedSong &other) const noexcept {
160 161 162
		return uri == other.uri &&
			start_time == other.start_time &&
			end_time == other.end_time;
163 164
	}

165
	[[gnu::pure]] [[gnu::nonnull]]
166
	bool IsURI(const char *other_uri) const noexcept {
167 168 169
		return uri == other_uri;
	}

170
	[[gnu::pure]] [[gnu::nonnull]]
171 172 173 174
	bool IsRealURI(const char *other_uri) const noexcept {
		return (HasRealURI() ? real_uri : uri) == other_uri;
	}

175
	[[gnu::pure]]
176
	bool IsRemote() const noexcept;
177

178
	[[gnu::pure]]
179
	bool IsFile() const noexcept {
180 181 182
		return !IsRemote();
	}

183
	[[gnu::pure]]
184
	bool IsAbsoluteFile() const noexcept;
185

186
	[[gnu::pure]]
187
	bool IsInDatabase() const noexcept;
188

189
	const Tag &GetTag() const noexcept {
190 191 192
		return tag;
	}

193
	Tag &WritableTag() noexcept {
194 195 196
		return tag;
	}

197
	void SetTag(const Tag &_tag) noexcept {
198 199 200
		tag = Tag(_tag);
	}

201
	void SetTag(Tag &&_tag) noexcept {
202 203 204
		tag = std::move(_tag);
	}

205
	void MoveTagFrom(DetachedSong &&other) noexcept {
206 207 208
		tag = std::move(other.tag);
	}

209 210 211 212
	/**
	 * Similar to the MoveTagFrom(), but move only the #TagItem
	 * array.
	 */
213
	void MoveTagItemsFrom(DetachedSong &&other) noexcept {
214 215 216
		tag.MoveItemsFrom(std::move(other.tag));
	}

217
	std::chrono::system_clock::time_point GetLastModified() const noexcept {
218 219 220
		return mtime;
	}

221
	void SetLastModified(std::chrono::system_clock::time_point _value) noexcept {
222 223 224
		mtime = _value;
	}

225
	SongTime GetStartTime() const noexcept {
226
		return start_time;
227 228
	}

229
	void SetStartTime(SongTime _value) noexcept {
230
		start_time = _value;
231 232
	}

233
	SongTime GetEndTime() const noexcept {
234
		return end_time;
235 236
	}

237
	void SetEndTime(SongTime _value) noexcept {
238
		end_time = _value;
239 240
	}

241
	[[gnu::pure]]
242
	SignedSongTime GetDuration() const noexcept;
243

244 245 246 247 248 249 250 251
	const AudioFormat &GetAudioFormat() const noexcept {
		return audio_format;
	}

	void SetAudioFormat(const AudioFormat &src) noexcept {
		audio_format = src;
	}

252 253 254
	/**
	 * Update the #tag and #mtime.
	 *
255 256
	 * Throws on error.
	 *
257 258
	 * @return true on success
	 */
259
	bool Update();
260 261 262

	/**
	 * Load #tag and #mtime from a local file.
263 264
	 *
	 * Throws on error.
265
	 */
266
	bool LoadFile(Path path);
267 268 269
};

#endif