Skip to content

Commit 94b61c2

Browse files
committed
Power profiles daemon: address review comments
Adding : - A missing try/catch - Glib::Error catch - Remove the useless destructor - Populate the profiles vector more efficiently - Numerous nits
1 parent b620eb9 commit 94b61c2

File tree

2 files changed

+46
-57
lines changed

2 files changed

+46
-57
lines changed

include/modules/power_profiles_daemon.hpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ struct Profile {
1515
class PowerProfilesDaemon : public ALabel {
1616
public:
1717
PowerProfilesDaemon(const std::string &, const Json::Value &);
18-
~PowerProfilesDaemon() override;
1918
auto update() -> void override;
2019
void profileChangedCb(const Gio::DBus::Proxy::MapChangedProperties &,
2120
const std::vector<Glib::ustring> &);
@@ -38,8 +37,7 @@ class PowerProfilesDaemon : public ALabel {
3837
std::vector<Profile>::iterator activeProfile_;
3938
// Current CSS class applied to the label
4039
std::string currentStyle_;
41-
// Format strings
42-
std::string labelFormat_;
40+
// Format string
4341
std::string tooltipFormat_;
4442
// DBus Proxy used to track the current active profile
4543
Glib::RefPtr<Gio::DBus::Proxy> powerProfilesProxy_;

src/modules/power_profiles_daemon.cpp

+45-54
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,14 @@
11
#include "modules/power_profiles_daemon.hpp"
22

3-
// In the 80000 version of fmt library authors decided to optimize imports
4-
// and moved declarations required for fmt::dynamic_format_arg_store in new
5-
// header fmt/args.h
6-
#if (FMT_VERSION >= 80000)
73
#include <fmt/args.h>
8-
#else
9-
#include <fmt/core.h>
10-
#endif
11-
124
#include <glibmm.h>
135
#include <glibmm/variant.h>
146
#include <spdlog/spdlog.h>
157

168
namespace waybar::modules {
179

1810
PowerProfilesDaemon::PowerProfilesDaemon(const std::string& id, const Json::Value& config)
19-
: ALabel(config, "power-profiles-daemon", id, "{profile}", 0, false, true), connected_(false) {
20-
if (config_["format"].isString()) {
21-
format_ = config_["format"].asString();
22-
} else {
23-
format_ = "{icon}";
24-
}
25-
11+
: ALabel(config, "power-profiles-daemon", id, "{icon}", 0, false, true), connected_(false) {
2612
if (config_["tooltip-format"].isString()) {
2713
tooltipFormat_ = config_["tooltip-format"].asString();
2814
} else {
@@ -58,27 +44,19 @@ PowerProfilesDaemon::PowerProfilesDaemon(const std::string& id, const Json::Valu
5844
sigc::mem_fun(*this, &PowerProfilesDaemon::busConnectedCb));
5945
}
6046

61-
PowerProfilesDaemon::~PowerProfilesDaemon() {
62-
if (powerProfileChangeSignal_.connected()) {
63-
powerProfileChangeSignal_.disconnect();
64-
}
65-
if (powerProfilesProxy_) {
66-
powerProfilesProxy_.reset();
67-
}
68-
}
69-
7047
void PowerProfilesDaemon::busConnectedCb(Glib::RefPtr<Gio::AsyncResult>& r) {
7148
try {
7249
powerProfilesProxy_ = Gio::DBus::Proxy::create_for_bus_finish(r);
7350
using GetAllProfilesVar = Glib::Variant<std::tuple<Glib::ustring>>;
7451
auto callArgs = GetAllProfilesVar::create(std::make_tuple("net.hadess.PowerProfiles"));
75-
76-
auto container = Glib::VariantBase::cast_dynamic<Glib::VariantContainerBase>(callArgs);
7752
powerProfilesProxy_->call("org.freedesktop.DBus.Properties.GetAll",
78-
sigc::mem_fun(*this, &PowerProfilesDaemon::getAllPropsCb), container);
53+
sigc::mem_fun(*this, &PowerProfilesDaemon::getAllPropsCb), callArgs);
7954
// Connect active profile callback
8055
} catch (const std::exception& e) {
81-
spdlog::error("Failed to create the power profiles daemon DBus proxy");
56+
spdlog::error("Failed to create the power profiles daemon DBus proxy: {}", e.what());
57+
} catch (const Glib::Error& e) {
58+
spdlog::error("Failed to create the power profiles daemon DBus proxy: {}",
59+
std::string(e.what()));
8260
}
8361
}
8462

@@ -99,6 +77,8 @@ void PowerProfilesDaemon::getAllPropsCb(Glib::RefPtr<Gio::AsyncResult>& r) {
9977
dp.emit();
10078
} catch (const std::exception& err) {
10179
spdlog::error("Failed to query power-profiles-daemon via dbus: {}", err.what());
80+
} catch (const Glib::Error& err) {
81+
spdlog::error("Failed to query power-profiles-daemon via dbus: {}", std::string(err.what()));
10282
}
10383
}
10484

@@ -111,18 +91,22 @@ void PowerProfilesDaemon::populateInitState() {
11191
using ProfilesType = std::vector<std::map<Glib::ustring, Glib::Variant<std::string>>>;
11292
Glib::Variant<ProfilesType> profilesVariant;
11393
powerProfilesProxy_->get_cached_property(profilesVariant, "Profiles");
114-
Glib::ustring name;
115-
Glib::ustring driver;
116-
Profile profile;
11794
for (auto& variantDict : profilesVariant.get()) {
95+
Glib::ustring name;
96+
Glib::ustring driver;
11897
if (auto p = variantDict.find("Profile"); p != variantDict.end()) {
11998
name = p->second.get();
12099
}
121100
if (auto d = variantDict.find("Driver"); d != variantDict.end()) {
122101
driver = d->second.get();
123102
}
124-
profile = {name, driver};
125-
availableProfiles_.push_back(profile);
103+
if (!name.empty()) {
104+
availableProfiles_.emplace_back(std::move(name), std::move(driver));
105+
} else {
106+
spdlog::error(
107+
"Power profiles daemon: power-profiles-daemon sent us an empty power profile name. "
108+
"Something is wrong.");
109+
}
126110
}
127111

128112
// Find the index of the current activated mode (to toggle)
@@ -157,12 +141,14 @@ void PowerProfilesDaemon::switchToProfile(std::string const& str) {
157141
auto pred = [str](Profile const& p) { return p.name == str; };
158142
this->activeProfile_ = std::find_if(availableProfiles_.begin(), availableProfiles_.end(), pred);
159143
if (activeProfile_ == availableProfiles_.end()) {
160-
throw std::runtime_error("FATAL, can't find the active profile in the available profiles list");
144+
spdlog::error(
145+
"Power profile daemon: can't find the active profile {} in the available profiles list",
146+
str);
161147
}
162148
}
163149

164150
auto PowerProfilesDaemon::update() -> void {
165-
if (connected_) {
151+
if (connected_ && activeProfile_ != availableProfiles_.end()) {
166152
auto profile = (*activeProfile_);
167153
// Set label
168154
fmt::dynamic_format_arg_store<fmt::format_context> store;
@@ -180,35 +166,40 @@ auto PowerProfilesDaemon::update() -> void {
180166
}
181167
label_.get_style_context()->add_class(profile.name);
182168
currentStyle_ = profile.name;
183-
184-
ALabel::update();
169+
} else {
170+
event_box_.set_visible(false);
185171
}
172+
173+
ALabel::update();
186174
}
187175

188176
bool PowerProfilesDaemon::handleToggle(GdkEventButton* const& e) {
189-
if (connected_) {
190-
if (e->type == GdkEventType::GDK_BUTTON_PRESS && powerProfilesProxy_) {
191-
activeProfile_++;
192-
if (activeProfile_ == availableProfiles_.end()) {
193-
activeProfile_ = availableProfiles_.begin();
194-
}
195-
196-
using VarStr = Glib::Variant<Glib::ustring>;
197-
using SetPowerProfileVar = Glib::Variant<std::tuple<Glib::ustring, Glib::ustring, VarStr>>;
198-
VarStr activeProfileVariant = VarStr::create(activeProfile_->name);
199-
auto callArgs = SetPowerProfileVar::create(
200-
std::make_tuple("net.hadess.PowerProfiles", "ActiveProfile", activeProfileVariant));
201-
auto container = Glib::VariantBase::cast_dynamic<Glib::VariantContainerBase>(callArgs);
202-
powerProfilesProxy_->call("org.freedesktop.DBus.Properties.Set",
203-
sigc::mem_fun(*this, &PowerProfilesDaemon::setPropCb), container);
177+
if (e->type == GdkEventType::GDK_BUTTON_PRESS && connected_) {
178+
activeProfile_++;
179+
if (activeProfile_ == availableProfiles_.end()) {
180+
activeProfile_ = availableProfiles_.begin();
204181
}
182+
183+
using VarStr = Glib::Variant<Glib::ustring>;
184+
using SetPowerProfileVar = Glib::Variant<std::tuple<Glib::ustring, Glib::ustring, VarStr>>;
185+
VarStr activeProfileVariant = VarStr::create(activeProfile_->name);
186+
auto callArgs = SetPowerProfileVar::create(
187+
std::make_tuple("net.hadess.PowerProfiles", "ActiveProfile", activeProfileVariant));
188+
powerProfilesProxy_->call("org.freedesktop.DBus.Properties.Set",
189+
sigc::mem_fun(*this, &PowerProfilesDaemon::setPropCb), callArgs);
205190
}
206191
return true;
207192
}
208193

209194
void PowerProfilesDaemon::setPropCb(Glib::RefPtr<Gio::AsyncResult>& r) {
210-
auto _ = powerProfilesProxy_->call_finish(r);
211-
update();
195+
try {
196+
auto _ = powerProfilesProxy_->call_finish(r);
197+
update();
198+
} catch (const std::exception& e) {
199+
spdlog::error("Failed to set the the active power profile: {}", e.what());
200+
} catch (const Glib::Error& e) {
201+
spdlog::error("Failed to set the active power profile: {}", std::string(e.what()));
202+
}
212203
}
213204

214205
} // namespace waybar::modules

0 commit comments

Comments
 (0)