MPD  0.20.15
DetachedSong.hxx
Go to the documentation of this file.
1 /*
2  * Copyright 2003-2017 The Music Player Daemon Project
3  * http://www.musicpd.org
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #ifndef MPD_DETACHED_SONG_HXX
21 #define MPD_DETACHED_SONG_HXX
22 
23 #include "check.h"
24 #include "tag/Tag.hxx"
25 #include "Chrono.hxx"
26 #include "Compiler.h"
27 
28 #include <string>
29 #include <utility>
30 
31 #include <time.h>
32 
33 struct LightSong;
34 class Storage;
35 class Path;
36 
37 class DetachedSong {
38  friend DetachedSong DatabaseDetachSong(const Storage &db,
39  const LightSong &song);
40 
52  std::string uri;
53 
62  std::string real_uri;
63 
64  Tag tag;
65 
66  time_t mtime = 0;
67 
71  SongTime start_time = SongTime::zero();
72 
77  SongTime end_time = SongTime::zero();
78 
79  explicit DetachedSong(const LightSong &other);
80 
81 public:
82  explicit DetachedSong(const DetachedSong &) = default;
83 
84  explicit DetachedSong(const char *_uri)
85  :uri(_uri) {}
86 
87  explicit DetachedSong(const std::string &_uri)
88  :uri(_uri) {}
89 
90  explicit DetachedSong(std::string &&_uri)
91  :uri(std::move(_uri)) {}
92 
93  template<typename U>
94  DetachedSong(U &&_uri, Tag &&_tag)
95  :uri(std::forward<U>(_uri)),
96  tag(std::move(_tag)) {}
97 
98  DetachedSong(DetachedSong &&) = default;
99 
100  ~DetachedSong();
101 
102  gcc_pure
103  const char *GetURI() const noexcept {
104  return uri.c_str();
105  }
106 
107  template<typename T>
108  void SetURI(T &&_uri) {
109  uri = std::forward<T>(_uri);
110  }
111 
116  gcc_pure
117  bool HasRealURI() const noexcept {
118  return !real_uri.empty();
119  }
120 
125  gcc_pure
126  const char *GetRealURI() const noexcept {
127  return (HasRealURI() ? real_uri : uri).c_str();
128  }
129 
130  template<typename T>
131  void SetRealURI(T &&_uri) {
132  real_uri = std::forward<T>(_uri);
133  }
134 
139  gcc_pure
140  bool IsSame(const DetachedSong &other) const noexcept {
141  return uri == other.uri &&
142  start_time == other.start_time &&
143  end_time == other.end_time;
144  }
145 
147  bool IsURI(const char *other_uri) const noexcept {
148  return uri == other_uri;
149  }
150 
151  gcc_pure
152  bool IsRemote() const noexcept;
153 
154  gcc_pure
155  bool IsFile() const noexcept {
156  return !IsRemote();
157  }
158 
159  gcc_pure
160  bool IsAbsoluteFile() const noexcept;
161 
162  gcc_pure
163  bool IsInDatabase() const noexcept;
164 
165  const Tag &GetTag() const noexcept {
166  return tag;
167  }
168 
169  Tag &WritableTag() noexcept {
170  return tag;
171  }
172 
173  void SetTag(const Tag &_tag) {
174  tag = Tag(_tag);
175  }
176 
177  void SetTag(Tag &&_tag) {
178  tag = std::move(_tag);
179  }
180 
181  void MoveTagFrom(DetachedSong &&other) {
182  tag = std::move(other.tag);
183  }
184 
190  tag.MoveItemsFrom(std::move(other.tag));
191  }
192 
193  time_t GetLastModified() const {
194  return mtime;
195  }
196 
197  void SetLastModified(time_t _value) {
198  mtime = _value;
199  }
200 
202  return start_time;
203  }
204 
205  void SetStartTime(SongTime _value) {
206  start_time = _value;
207  }
208 
210  return end_time;
211  }
212 
213  void SetEndTime(SongTime _value) {
214  end_time = _value;
215  }
216 
217  gcc_pure
218  SignedSongTime GetDuration() const noexcept;
219 
225  bool Update();
226 
230  bool LoadFile(Path path);
231 };
232 
233 #endif
bool Update()
Update the #tag and #mtime.
#define gcc_nonnull_all
Definition: Compiler.h:122
SongTime GetStartTime() const
time_t GetLastModified() const
void SetTag(Tag &&_tag)
static constexpr SongTime zero()
Definition: Chrono.hxx:41
gcc_pure SignedSongTime GetDuration() const noexcept
The meta information about a song file.
Definition: Tag.hxx:34
A time stamp within a song.
Definition: Chrono.hxx:31
gcc_pure const char * GetURI() const noexcept
gcc_pure bool IsSame(const DetachedSong &other) const noexcept
Returns true if both objects refer to the same physical song.
gcc_pure bool IsRemote() const noexcept
void SetLastModified(time_t _value)
gcc_pure bool IsFile() const noexcept
void SetEndTime(SongTime _value)
DetachedSong(const std::string &_uri)
gcc_pure bool HasRealURI() const noexcept
Does this object have a "real" URI different from the displayed URI?
bool LoadFile(Path path)
Load #tag and #mtime from a local file.
const char * uri
Definition: LightSong.hxx:48
void SetRealURI(T &&_uri)
gcc_pure gcc_nonnull_all bool IsURI(const char *other_uri) const noexcept
SongTime GetEndTime() const
SongTime start_time
Start of this sub-song within the file.
Definition: LightSong.hxx:70
A path name in the native file system character set.
Definition: Path.hxx:39
DetachedSong(const char *_uri)
void MoveTagFrom(DetachedSong &&other)
void MoveItemsFrom(Tag &&other)
Similar to the move operator, but move only the TagItem array.
Definition: Tag.hxx:88
DetachedSong(std::string &&_uri)
SongTime end_time
End of this sub-song within the file.
Definition: LightSong.hxx:76
gcc_pure bool IsAbsoluteFile() const noexcept
friend DetachedSong DatabaseDetachSong(const Storage &db, const LightSong &song)
"Detach" the Song object, i.e.
const Tag * tag
Must not be nullptr.
Definition: LightSong.hxx:63
A variant of SongTime that is based on a signed integer.
Definition: Chrono.hxx:115
void SetTag(const Tag &_tag)
const Tag & GetTag() const noexcept
void SetStartTime(SongTime _value)
Tag & WritableTag() noexcept
gcc_pure const char * GetRealURI() const noexcept
Returns "real" URI (#real_uri) and falls back to just GetURI().
#define gcc_pure
Definition: Compiler.h:116
DetachedSong(U &&_uri, Tag &&_tag)
const Storage const char * uri
gcc_pure bool IsInDatabase() const noexcept
void SetURI(T &&_uri)
void MoveTagItemsFrom(DetachedSong &&other)
Similar to the MoveTagFrom(), but move only the TagItem array.
A reference to a song file.
Definition: LightSong.hxx:40