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  DBUS_CPP_METHOD_DEF(GetTracksMetadata, TrackList)
52  DBUS_CPP_METHOD_DEF(GetTracksUri, TrackList)
53  DBUS_CPP_METHOD_DEF(AddTrack, TrackList)
54  DBUS_CPP_METHOD_DEF(RemoveTrack, TrackList)
55  DBUS_CPP_METHOD_DEF(GoTo, TrackList)
56  DBUS_CPP_METHOD_DEF(Reset, TrackList)
57 
58  struct Signals
59  {
60  Signals() = delete;
61 
62  DBUS_CPP_SIGNAL_DEF
63  (
64  TrackListReplaced,
65  TrackList,
66  BOOST_IDENTITY_TYPE((std::tuple<std::vector<core::ubuntu::media::Track::Id>, core::ubuntu::media::Track::Id>))
67  )
68 
69  DBUS_CPP_SIGNAL_DEF
70  (
71  TrackAdded,
74  )
75 
76  DBUS_CPP_SIGNAL_DEF
77  (
78  TrackRemoved,
79  TrackList,
80  core::ubuntu::media::Track::Id
81  )
82 
83  DBUS_CPP_SIGNAL_DEF
84  (
85  TrackChanged,
86  TrackList,
87  core::ubuntu::media::Track::Id
88  )
89 
90  DBUS_CPP_SIGNAL_DEF
91  (
92  TrackMetadataChanged,
93  TrackList,
94  BOOST_IDENTITY_TYPE((std::tuple<std::map<std::string, dbus::types::Variant>, dbus::types::ObjectPath>))
95  )
96  };
97 
98  struct Properties
99  {
100  Properties() = delete;
101 
102  DBUS_CPP_READABLE_PROPERTY_DEF(Tracks, TrackList, std::vector<core::ubuntu::media::Track::Id>)
103  DBUS_CPP_READABLE_PROPERTY_DEF(CanEditTracks, TrackList, bool)
104  };
105 
106  struct Skeleton
107  {
108  static const std::vector<std::string>& the_empty_list_of_invalidated_properties()
109  {
110  static const std::vector<std::string> instance; return instance;
111  }
112 
113  // Object instance creation time properties go here.
115  {
116  // The dbus object that should implement org.mpris.MediaPlayer2
117  core::dbus::Object::Ptr object;
118  // Default values assigned to exported dbus interface properties on construction
119  struct Defaults
120  {
121  Properties::Tracks::ValueType tracks{std::vector<core::ubuntu::media::Track::Id>()};
122  Properties::CanEditTracks::ValueType can_edit_tracks{true};
123  } defaults;
124  };
125 
127  : configuration(configuration),
128  properties
129  {
130  configuration.object->template get_property<Properties::Tracks>(),
131  configuration.object->template get_property<Properties::CanEditTracks>(),
132  },
133  signals
134  {
135  configuration.object->template get_signal<Signals::TrackListReplaced>(),
136  configuration.object->template get_signal<Signals::TrackAdded>(),
137  configuration.object->template get_signal<Signals::TrackRemoved>(),
138  configuration.object->template get_signal<Signals::TrackChanged>(),
139  configuration.object->template get_signal<Signals::TrackMetadataChanged>(),
140  configuration.object->template get_signal<core::dbus::interfaces::Properties::Signals::PropertiesChanged>()
141  }
142  {
143  // Set the default value of the properties on the MPRIS TrackList dbus interface
145  properties.can_edit_tracks->set(configuration.defaults.can_edit_tracks);
146  }
147 
148  template<typename Property>
149  void on_property_value_changed(const typename Property::ValueType& value)
150  {
151  Dictionary dict;
152  dict[Property::name()] = dbus::types::Variant::encode(value);
153 
154  signals.properties_changed->emit(std::make_tuple(
155  dbus::traits::Service<TrackList>::interface_name(),
156  dict,
158  }
159 
160  std::map<std::string, core::dbus::types::Variant> get_all_properties()
161  {
162  std::map<std::string, core::dbus::types::Variant> dict;
163  dict[Properties::Tracks::name()] = core::dbus::types::Variant::encode(properties.tracks->get());
164  dict[Properties::CanEditTracks::name()] = core::dbus::types::Variant::encode(properties.can_edit_tracks->get());
165 
166  return dict;
167  }
168 
170 
171  struct
172  {
173  std::shared_ptr<core::dbus::Property<Properties::Tracks>> tracks;
174  std::shared_ptr<core::dbus::Property<Properties::CanEditTracks>> can_edit_tracks;
175  } properties;
176 
177  struct
178  {
179  core::dbus::Signal<Signals::TrackListReplaced, Signals::TrackListReplaced::ArgumentType>::Ptr tracklist_replaced;
180  core::dbus::Signal<Signals::TrackAdded, Signals::TrackAdded::ArgumentType>::Ptr track_added;
181  core::dbus::Signal<Signals::TrackRemoved, Signals::TrackRemoved::ArgumentType>::Ptr track_removed;
182  core::dbus::Signal<Signals::TrackChanged, Signals::TrackChanged::ArgumentType>::Ptr track_changed;
183  core::dbus::Signal<Signals::TrackMetadataChanged, Signals::TrackMetadataChanged::ArgumentType>::Ptr track_metadata_changed;
184 
185  dbus::Signal <core::dbus::interfaces::Properties::Signals::PropertiesChanged,
186  core::dbus::interfaces::Properties::Signals::PropertiesChanged::ArgumentType
188  } signals;
189  };
190 };
191 }
192 
193 #endif // MPRIS_TRACK_LIST_H_
struct mpris::TrackList::Skeleton::@20 signals
std::shared_ptr< core::dbus::Property< Properties::Tracks > > tracks
Definition: track_list.h:173
dbus::Signal< core::dbus::interfaces::Properties::Signals::PropertiesChanged, core::dbus::interfaces::Properties::Signals::PropertiesChanged::ArgumentType >::Ptr properties_changed
Definition: track_list.h:187
static const std::vector< std::string > & the_empty_list_of_invalidated_properties()
Definition: track_list.h:108
Definition: player.h:33
STL namespace.
core::dbus::Signal< Signals::TrackListReplaced, Signals::TrackListReplaced::ArgumentType >::Ptr tracklist_replaced
Definition: track_list.h:179
core::dbus::Signal< Signals::TrackMetadataChanged, Signals::TrackMetadataChanged::ArgumentType >::Ptr track_metadata_changed
Definition: track_list.h:183
std::map< std::string, core::dbus::types::Variant > get_all_properties()
Definition: track_list.h:160
Properties::CanEditTracks::ValueType can_edit_tracks
Definition: track_list.h:122
Skeleton(const Configuration &configuration)
Definition: track_list.h:126
core::dbus::Signal< Signals::TrackAdded, Signals::TrackAdded::ArgumentType >::Ptr track_added
Definition: track_list.h:180
struct mpris::TrackList::Skeleton::Configuration::Defaults defaults
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:149
Configuration configuration
Definition: track_list.h:169
core::dbus::Signal< Signals::TrackRemoved, Signals::TrackRemoved::ArgumentType >::Ptr track_removed
Definition: track_list.h:181
struct mpris::TrackList::Skeleton::@19 properties
core::dbus::Signal< Signals::TrackChanged, Signals::TrackChanged::ArgumentType >::Ptr track_changed
Definition: track_list.h:182
std::shared_ptr< core::dbus::Property< Properties::CanEditTracks > > can_edit_tracks
Definition: track_list.h:174