Music Hub  ..
A session-wide music playback service
battery_observer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright © 2014 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 
20 
21 #include <core/dbus/macros.h>
22 #include <core/dbus/object.h>
23 #include <core/dbus/property.h>
24 
25 namespace media = core::ubuntu::media;
26 
27 namespace com { namespace canonical { namespace indicator { namespace power {
28 struct Battery
29 {
30  static std::string& name()
31  {
32  static std::string s = "com.canonical.indicator.power.Battery";
33  return s;
34  }
35 
36  static const core::dbus::types::ObjectPath& path()
37  {
38  static core::dbus::types::ObjectPath p{"/com/canonical/indicator/power/Battery"};
39  return p;
40  }
41 
42  // Possible values: "ok", "low", "very_low", "critical"
43  DBUS_CPP_READABLE_PROPERTY_DEF(PowerLevel, Battery, std::string)
44  DBUS_CPP_READABLE_PROPERTY_DEF(IsWarning, Battery, bool)
45 }; // IndicatorPower
46 }}}}
47 
48 namespace
49 {
50 namespace impl
51 {
52 struct BatteryObserver : public media::power::BatteryObserver
53 {
54  static core::ubuntu::media::power::Level power_level_from_string(const std::string& s)
55  {
56  static const std::map<std::string, core::ubuntu::media::power::Level> lut =
57  {
62  };
63 
64  if (lut.count(s) == 0)
66 
67  return lut.at(s);
68  }
69 
70  BatteryObserver(const core::dbus::Object::Ptr& object)
71  : object{object},
72  properties
73  {
74  core::Property<core::ubuntu::media::power::Level>{core::ubuntu::media::power::Level::unknown},
75  object->get_property<com::canonical::indicator::power::Battery::PowerLevel>(),
76  object->get_property<com::canonical::indicator::power::Battery::IsWarning>(),
77  },
78  connections
79  {
80  properties.power_level->changed().connect([this](const std::string& value)
81  {
82  properties.typed_power_level = BatteryObserver::power_level_from_string(value);
83  })
84  }
85  {
86  }
87 
88  const core::Property<core::ubuntu::media::power::Level>& level() const override
89  {
90  return properties.typed_power_level;
91  }
92 
93  const core::Property<bool>& is_warning_active() const override
94  {
95  return *properties.is_warning;
96  }
97 
98  // The object representing the remote indicator instance.
99  core::dbus::Object::Ptr object;
100  // All properties go here.
101  struct
102  {
103  // We have to translate from the raw strings coming in via the bus to
104  // the strongly typed enumeration exposed via the interface.
105  core::Property<core::ubuntu::media::power::Level> typed_power_level;
106  std::shared_ptr<core::dbus::Property<com::canonical::indicator::power::Battery::PowerLevel>> power_level;
107 
108  std::shared_ptr<core::dbus::Property<com::canonical::indicator::power::Battery::IsWarning>> is_warning;
109  } properties;
110  // Our event connections
111  struct
112  {
113  core::ScopedConnection power_level;
114  } connections;
115 
116 };
117 }
118 }
119 
120 media::power::BatteryObserver::Ptr media::power::make_platform_default_battery_observer(media::helper::ExternalServices& es)
121 {
122  auto service = core::dbus::Service::use_service<com::canonical::indicator::power::Battery>(es.session);
123  auto object = service->object_for_path(com::canonical::indicator::power::Battery::path());
124 
125  return std::make_shared<impl::BatteryObserver>(object);
126 }
127 
static const core::dbus::types::ObjectPath & path()
core::ubuntu::media::power::BatteryObserver::Ptr make_platform_default_battery_observer(core::ubuntu::media::helper::ExternalServices &)