Music Hub  ..
A session-wide music playback service
track_list.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 
19 #ifndef MPRIS_TRACK_LIST_H_
20 #define MPRIS_TRACK_LIST_H_
21 
22 #include <core/dbus/macros.h>
23 
24 #include <core/dbus/types/any.h>
25 #include <core/dbus/macros.h>
26 #include <core/dbus/types/object_path.h>
27 #include <core/dbus/object.h>
28 #include <core/dbus/property.h>
29 #include <core/dbus/types/variant.h>
30 
31 #include <boost/utility/identity_type.hpp>
32 
33 #include <string>
34 #include <tuple>
35 #include <vector>
36 
37 namespace dbus = core::dbus;
38 
39 namespace mpris
40 {
41 struct TrackList
42 {
43  typedef std::map<std::string, core::dbus::types::Variant> Dictionary;
44 
45  static const std::string& name()
46  {
47  static const std::string s{"org.mpris.MediaPlayer2.TrackList"};
48  return s;
49  }
50 
51  struct Error
52  {
54  {
55  static constexpr const char* name
56  {
57  "mpris.TrackList.Error.InsufficientPermissionsToAddTrack"
58  };
59  };
60 
62  {
63  static constexpr const char* name
64  {
65  "mpris.TrackList.Error.FailedToMoveTrack"
66  };
67  };
68 
70  {
71  static constexpr const char* name
72  {
73  "mpris.TrackList.Error.FailedToFindMoveTrackSource"
74  };
75  };
76 
78  {
79  static constexpr const char* name
80  {
81  "mpris.TrackList.Error.FailedToFindMoveTrackDest"
82  };
83  };
84 
86  {
87  static constexpr const char* name
88  {
89  "mpris.TrackList.Error.TrackNotFound"
90  };
91  };
92  };
93 
94  DBUS_CPP_METHOD_DEF(GetTracksMetadata, TrackList)
95  DBUS_CPP_METHOD_DEF(GetTracksUri, TrackList)
96  DBUS_CPP_METHOD_DEF(AddTrack, TrackList)
97  DBUS_CPP_METHOD_DEF(AddTracks, TrackList)
98  DBUS_CPP_METHOD_DEF(MoveTrack, TrackList)
99  DBUS_CPP_METHOD_DEF(RemoveTrack, TrackList)
100  DBUS_CPP_METHOD_DEF(GoTo, TrackList)
101  DBUS_CPP_METHOD_DEF(Reset, TrackList)
102 
103  struct Signals
104  {
105  Signals() = delete;
106 
107  DBUS_CPP_SIGNAL_DEF
108  (
109  TrackListReplaced,
110  TrackList,
111  BOOST_IDENTITY_TYPE((std::tuple<std::vector<core::ubuntu::media::Track::Id>, core::ubuntu::media::Track::Id>))
112  )
113 
114  DBUS_CPP_SIGNAL_DEF
115  (
116  TrackAdded,
119  )
120 
121  DBUS_CPP_SIGNAL_DEF
122  (
123  TracksAdded,
124  TrackList,
126  )
127 
128  DBUS_CPP_SIGNAL_DEF
129  (
130  TrackMoved,
132  BOOST_IDENTITY_TYPE((std::tuple<core::ubuntu::media::Track::Id, core::ubuntu::media::Track::Id>))
133  )
134 
135  DBUS_CPP_SIGNAL_DEF
136  (
137  TrackRemoved,
138  TrackList,
140  )
141 
142  DBUS_CPP_SIGNAL_DEF
143  (
144  TrackChanged,
145  TrackList,
147  )
148 
149  DBUS_CPP_SIGNAL_DEF
150  (
151  TrackListReset,
152  TrackList,
153  void
154  )
155 
156  DBUS_CPP_SIGNAL_DEF
157  (
158  TrackMetadataChanged,
160  BOOST_IDENTITY_TYPE((std::tuple<std::map<std::string, dbus::types::Variant>, dbus::types::ObjectPath>))
161  )
162  };
163 
164  struct Properties
165  {
166  Properties() = delete;
167 
168  DBUS_CPP_READABLE_PROPERTY_DEF(Tracks, TrackList, std::vector<core::ubuntu::media::Track::Id>)
169  DBUS_CPP_READABLE_PROPERTY_DEF(CanEditTracks, TrackList, bool)
170  };
171 
172  struct Skeleton
173  {
174  static const std::vector<std::string>& the_empty_list_of_invalidated_properties()
175  {
176  static const std::vector<std::string> instance; return instance;
177  }
178 
179  // Object instance creation time properties go here.
181  {
182  // The dbus object that should implement org.mpris.MediaPlayer2
183  core::dbus::Object::Ptr object;
184  // Default values assigned to exported dbus interface properties on construction
185  struct Defaults
186  {
187  Properties::Tracks::ValueType tracks{std::vector<core::ubuntu::media::Track::Id>()};
188  Properties::CanEditTracks::ValueType can_edit_tracks{true};
189  } defaults;
190  };
191 
192  Skeleton(const Configuration& configuration)
193  : configuration(configuration),
194  properties
195  {
196  configuration.object->template get_property<Properties::Tracks>(),
197  configuration.object->template get_property<Properties::CanEditTracks>(),
198  },
199  signals
200  {
201  configuration.object->template get_signal<Signals::TrackListReplaced>(),
202  configuration.object->template get_signal<Signals::TrackAdded>(),
203  configuration.object->template get_signal<Signals::TracksAdded>(),
204  configuration.object->template get_signal<Signals::TrackMoved>(),
205  configuration.object->template get_signal<Signals::TrackRemoved>(),
206  configuration.object->template get_signal<Signals::TrackChanged>(),
207  configuration.object->template get_signal<Signals::TrackListReset>(),
208  configuration.object->template get_signal<Signals::TrackMetadataChanged>(),
209  configuration.object->template get_signal<core::dbus::interfaces::Properties::Signals::PropertiesChanged>()
210  }
211  {
212  // Set the default value of the properties on the MPRIS TrackList dbus interface
213  properties.tracks->set(configuration.defaults.tracks);
214  properties.can_edit_tracks->set(configuration.defaults.can_edit_tracks);
215  }
216 
217  template<typename Property>
218  void on_property_value_changed(const typename Property::ValueType& value)
219  {
220  Dictionary dict;
221  dict[Property::name()] = dbus::types::Variant::encode(value);
222 
223  signals.properties_changed->emit(std::make_tuple(
224  dbus::traits::Service<TrackList>::interface_name(),
225  dict,
226  the_empty_list_of_invalidated_properties()));
227  }
228 
229  std::map<std::string, core::dbus::types::Variant> get_all_properties()
230  {
231  std::map<std::string, core::dbus::types::Variant> dict;
232  dict[Properties::Tracks::name()] = core::dbus::types::Variant::encode(properties.tracks->get());
233  dict[Properties::CanEditTracks::name()] = core::dbus::types::Variant::encode(properties.can_edit_tracks->get());
234 
235  return dict;
236  }
237 
239 
240  struct
241  {
242  std::shared_ptr<core::dbus::Property<Properties::Tracks>> tracks;
243  std::shared_ptr<core::dbus::Property<Properties::CanEditTracks>> can_edit_tracks;
244  } properties;
245 
246  struct
247  {
248  core::dbus::Signal<Signals::TrackListReplaced, Signals::TrackListReplaced::ArgumentType>::Ptr tracklist_replaced;
249  core::dbus::Signal<Signals::TrackAdded, Signals::TrackAdded::ArgumentType>::Ptr track_added;
250  core::dbus::Signal<Signals::TracksAdded, Signals::TracksAdded::ArgumentType>::Ptr tracks_added;
251  core::dbus::Signal<Signals::TrackMoved, Signals::TrackMoved::ArgumentType>::Ptr track_moved;
252  core::dbus::Signal<Signals::TrackRemoved, Signals::TrackRemoved::ArgumentType>::Ptr track_removed;
253  core::dbus::Signal<Signals::TrackChanged, Signals::TrackChanged::ArgumentType>::Ptr track_changed;
254  core::dbus::Signal<Signals::TrackListReset, Signals::TrackListReset::ArgumentType>::Ptr track_list_reset;
255  core::dbus::Signal<Signals::TrackMetadataChanged, Signals::TrackMetadataChanged::ArgumentType>::Ptr track_metadata_changed;
256 
257  dbus::Signal <core::dbus::interfaces::Properties::Signals::PropertiesChanged,
258  core::dbus::interfaces::Properties::Signals::PropertiesChanged::ArgumentType
260  } signals;
261  };
262 };
263 }
264 
265 #endif // MPRIS_TRACK_LIST_H_
std::shared_ptr< core::dbus::Property< Properties::Tracks > > tracks
Definition: track_list.h:242
dbus::Signal< core::dbus::interfaces::Properties::Signals::PropertiesChanged, core::dbus::interfaces::Properties::Signals::PropertiesChanged::ArgumentType >::Ptr properties_changed
Definition: track_list.h:259
core::dbus::Signal< Signals::TracksAdded, Signals::TracksAdded::ArgumentType >::Ptr tracks_added
Definition: track_list.h:250
static const std::vector< std::string > & the_empty_list_of_invalidated_properties()
Definition: track_list.h:174
core::dbus::Signal< Signals::TrackListReplaced, Signals::TrackListReplaced::ArgumentType >::Ptr tracklist_replaced
Definition: track_list.h:248
core::dbus::Signal< Signals::TrackListReset, Signals::TrackListReset::ArgumentType >::Ptr track_list_reset
Definition: track_list.h:254
core::dbus::Signal< Signals::TrackMetadataChanged, Signals::TrackMetadataChanged::ArgumentType >::Ptr track_metadata_changed
Definition: track_list.h:255
std::map< std::string, core::dbus::types::Variant > get_all_properties()
Definition: track_list.h:229
Skeleton(const Configuration &configuration)
Definition: track_list.h:192
core::dbus::Signal< Signals::TrackAdded, Signals::TrackAdded::ArgumentType >::Ptr track_added
Definition: track_list.h:249
static const std::string & name()
Definition: track_list.h:45
std::map< std::string, core::dbus::types::Variant > Dictionary
Definition: track_list.h:43
void on_property_value_changed(const typename Property::ValueType &value)
Definition: track_list.h:218
Configuration configuration
Definition: track_list.h:238
std::vector< Track::UriType > ContainerURI
Definition: track_list.h:44
core::dbus::Signal< Signals::TrackRemoved, Signals::TrackRemoved::ArgumentType >::Ptr track_removed
Definition: track_list.h:252
core::dbus::Signal< Signals::TrackMoved, Signals::TrackMoved::ArgumentType >::Ptr track_moved
Definition: track_list.h:251
core::dbus::Signal< Signals::TrackChanged, Signals::TrackChanged::ArgumentType >::Ptr track_changed
Definition: track_list.h:253
std::shared_ptr< core::dbus::Property< Properties::CanEditTracks > > can_edit_tracks
Definition: track_list.h:243