Music Hub  ..
A session-wide music playback service
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
track_list_implementation.cpp
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 #include <stdio.h>
20 #include <stdlib.h>
21 
23 
24 #include "engine.h"
25 
26 namespace dbus = core::dbus;
27 namespace media = core::ubuntu::media;
28 
30 {
31  typedef std::map<Track::Id, std::tuple<Track::UriType, Track::MetaData>> MetaDataCache;
32 
33  dbus::types::ObjectPath path;
34  MetaDataCache meta_data_cache;
35  std::shared_ptr<media::Engine::MetaDataExtractor> extractor;
36 };
37 
39  const dbus::types::ObjectPath& op,
40  const std::shared_ptr<media::Engine::MetaDataExtractor>& extractor)
41  : media::TrackListSkeleton(op),
42  d(new Private{op, Private::MetaDataCache{}, extractor})
43 {
44  can_edit_tracks().set(true);
45 }
46 
48 {
49 }
50 
52 {
53  auto it = d->meta_data_cache.find(id);
54 
55  if (it == d->meta_data_cache.end())
56  return Track::UriType{};
57 
58  return std::get<0>(it->second);
59 }
60 
62 {
63  auto it = d->meta_data_cache.find(id);
64 
65  if (it == d->meta_data_cache.end())
66  return Track::MetaData{};
67 
68  return std::get<1>(it->second);
69 }
70 
72  const media::Track::UriType& uri,
73  const media::Track::Id& position,
74  bool make_current)
75 {
76  static size_t track_counter = 0;
77 
78  std::stringstream ss; ss << d->path.as_string() << "/" << track_counter++;
79  Track::Id id{ss.str()};
80 
81  auto result = tracks().update([this, id, position, make_current](TrackList::Container& container)
82  {
83  auto it = std::find(container.begin(), container.end(), position);
84  container.insert(it, id);
85  return true;
86  });
87 
88  if (result)
89  {
90  if (d->meta_data_cache.count(id) == 0)
91  {
92  d->meta_data_cache[id] = std::make_tuple(
93  uri,
94  d->extractor->meta_data_for_track_with_uri(uri));
95  } else
96  {
97  std::get<0>(d->meta_data_cache[id]) = uri;
98  }
99 
100  if (make_current)
101  go_to(id);
102 
103  on_track_added()(id);
104  }
105 }
106 
108 {
109  auto result = tracks().update([id](TrackList::Container& container)
110  {
111  container.erase(std::find(container.begin(), container.end(), id));
112  return true;
113  });
114 
115  if (result)
116  {
117  d->meta_data_cache.erase(id);
118 
119  on_track_removed()(id);
120  }
121 
122 }
123 
125 {
126  (void) track;
127 }
void add_track_with_uri_at(const Track::UriType &uri, const Track::Id &position, bool make_current)
const core::Property< Container > & tracks() const
const core::Signal< Track::Id > & on_track_removed() const
Track::UriType query_uri_for_track(const Track::Id &id)
const core::Signal< Track::Id > & on_track_added() const
Track::MetaData query_meta_data_for_track(const Track::Id &id)
std::shared_ptr< media::Engine::MetaDataExtractor > extractor
std::string UriType
Definition: track.h:40
std::vector< Track::Id > Container
Definition: track_list.h:41
std::map< Track::Id, std::tuple< Track::UriType, Track::MetaData > > MetaDataCache
void go_to(const Track::Id &track)
const core::Property< bool > & can_edit_tracks() const
TrackListImplementation(const core::dbus::types::ObjectPath &op, const std::shared_ptr< Engine::MetaDataExtractor > &extractor)