Music Hub  ..
A session-wide music playback service
track.h
Go to the documentation of this file.
1 /*
2  * Copyright © 2013 Canonical Ltd.
3  *
4  * This program is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License version 3,
6  * as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * Authored by: Thomas Voß <thomas.voss@canonical.com>
17  */
18 #ifndef CORE_UBUNTU_MEDIA_TRACK_H_
19 #define CORE_UBUNTU_MEDIA_TRACK_H_
20 
21 #include <chrono>
22 #include <functional>
23 #include <map>
24 #include <memory>
25 #include <sstream>
26 #include <string>
27 #include <vector>
28 
29 namespace core
30 {
31 namespace ubuntu
32 {
33 namespace media
34 {
35 template<typename T> class Property;
36 
37 class Track
38 {
39 public:
40  typedef std::string UriType;
41  typedef std::string Id;
42  typedef std::map<std::string, std::string> MetaDataType;
43 
44  class MetaData
45  {
46  public:
47  static constexpr const char* TrackArtlUrlKey = "mpris:artUrl";
48  static constexpr const char* TrackLengthKey = "mpris:length";
49  static constexpr const char* TrackIdKey = "mpris:trackid";
50 
51  bool operator==(const MetaData& rhs) const
52  {
53  return map == rhs.map;
54  }
55 
56  bool operator!=(const MetaData& rhs) const
57  {
58  return map != rhs.map;
59  }
60 
61  template<typename Tag>
62  std::size_t count() const
63  {
64  return count(Tag::name());
65  }
66 
67  template<typename Tag>
68  void set(const typename Tag::ValueType& value)
69  {
70  std::stringstream ss; ss << value;
71  set(Tag::name(), ss.str());
72  }
73 
74  template<typename Tag>
75  typename Tag::ValueType get() const
76  {
77  std::stringstream ss(get(Tag::name()));
78  typename Tag::ValueType value; ss >> value;
79 
80  return std::move(value);
81  }
82 
83  std::size_t count(const std::string& key) const
84  {
85  return map.count(key);
86  }
87 
88  void set(const std::string& key, const std::string& value)
89  {
90  map[key] = value;
91  }
92 
93  const std::string& get(const std::string& key) const
94  {
95  return map.at(key);
96  }
97 
98  bool is_set(const std::string& key) const
99  {
100  try {
101  return !map.at(key).empty();
102  } catch (const std::out_of_range& e) {
103  return false;
104  }
105  }
106 
107  const std::map<std::string, std::string>& operator*() const
108  {
109  return map;
110  }
111 
112  std::string encode(const std::string& key) const;
113 
114  const std::string& album() const;
115  const std::string& artist() const;
116  const std::string& title() const;
117  const std::string& track_id() const;
118  const std::string& track_length() const;
119  const std::string& art_url() const;
120  const std::string& last_used() const;
121 
122  void set_album(const std::string& album);
123  void set_artist(const std::string& artist);
124  void set_title(const std::string& title);
125  void set_track_id(const std::string& id);
126  void set_track_length(const std::string& id);
127  void set_art_url(const std::string& url);
128  void set_last_used(const std::string& datetime);
129 
130  private:
131  MetaDataType map;
132  };
133 
134  Track(const Id& id);
135  Track(const Track&) = delete;
136  virtual ~Track();
137 
138  Track& operator=(const Track&);
139  bool operator==(const Track&) const;
140 
141  virtual const Id& id() const;
142  virtual const UriType& uri() const;
143 
144 private:
145  struct Private;
146  std::unique_ptr<Private> d;
147 };
148 }
149 }
150 }
151 
152 #endif // CORE_UBUNTU_MEDIA_TRACK_H_
void set_artist(const std::string &artist)
Definition: metadata.cpp:87
bool operator==(const MetaData &rhs) const
Definition: track.h:51
const std::string & track_length() const
Definition: metadata.cpp:67
Track & operator=(const Track &)
const std::map< std::string, std::string > & operator*() const
Definition: track.h:107
Definition: player.h:33
void set_track_id(const std::string &id)
Definition: metadata.cpp:97
virtual const UriType & uri() const
bool operator!=(const MetaData &rhs) const
Definition: track.h:56
virtual const Id & id() const
std::string encode(const std::string &key) const
Definition: metadata.cpp:27
static constexpr const char * TrackLengthKey
Definition: track.h:48
std::map< std::string, std::string > MetaDataType
Definition: track.h:42
std::size_t count(const std::string &key) const
Definition: track.h:83
const std::string & art_url() const
Definition: metadata.cpp:72
void set_title(const std::string &title)
Definition: metadata.cpp:92
const std::string & title() const
Definition: metadata.cpp:57
void set_album(const std::string &album)
Definition: metadata.cpp:82
const std::string & album() const
Definition: metadata.cpp:47
static constexpr const char * TrackIdKey
Definition: track.h:49
void set_track_length(const std::string &id)
Definition: metadata.cpp:102
const std::string & artist() const
Definition: metadata.cpp:52
std::string UriType
Definition: track.h:40
static constexpr const char * TrackArtlUrlKey
Definition: track.h:47
const std::string & track_id() const
Definition: metadata.cpp:62
std::size_t count() const
Definition: track.h:62
void set_art_url(const std::string &url)
Definition: metadata.cpp:107
bool is_set(const std::string &key) const
Definition: track.h:98
void set_last_used(const std::string &datetime)
Definition: metadata.cpp:112
const std::string & last_used() const
Definition: metadata.cpp:77