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