fix: check json::value type

This commit is contained in:
Alexis 2018-10-26 09:27:16 +02:00
parent fd76e98552
commit 3f269ff463
10 changed files with 27 additions and 27 deletions

View File

@ -4,15 +4,15 @@
waybar::ALabel::ALabel(const Json::Value& config, const std::string format) waybar::ALabel::ALabel(const Json::Value& config, const std::string format)
: config_(config), : config_(config),
format_(config_["format"] ? config_["format"].asString() : format), format_(config_["format"].isString() ? config_["format"].asString() : format),
default_format_(format_) default_format_(format_)
{ {
event_box_.add(label_); event_box_.add(label_);
if (config_["max-length"]) { if (config_["max-length"].isUInt()) {
label_.set_max_width_chars(config_["max-length"].asUInt()); label_.set_max_width_chars(config_["max-length"].asUInt());
label_.set_ellipsize(Pango::EllipsizeMode::ELLIPSIZE_END); label_.set_ellipsize(Pango::EllipsizeMode::ELLIPSIZE_END);
} }
if (config_["format-alt"]) { if (config_["format-alt"].isString()) {
event_box_.add_events(Gdk::BUTTON_PRESS_MASK); event_box_.add_events(Gdk::BUTTON_PRESS_MASK);
event_box_.signal_button_press_event() event_box_.signal_button_press_event()
.connect(sigc::mem_fun(*this, &ALabel::handleToggle)); .connect(sigc::mem_fun(*this, &ALabel::handleToggle));

View File

@ -53,8 +53,8 @@ waybar::Bar::Bar(const Client& client,
anchor |= ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP; anchor |= ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP;
} }
auto height = config_["height"] ? config_["height"].asUInt() : height_; auto height = config_["height"].isUInt() ? config_["height"].asUInt() : height_;
auto width = config_["width"] ? config_["width"].asUInt() : width_; auto width = config_["width"].isUInt() ? config_["width"].asUInt() : width_;
zwlr_layer_surface_v1_set_anchor(layer_surface, anchor); zwlr_layer_surface_v1_set_anchor(layer_surface, anchor);
zwlr_layer_surface_v1_set_exclusive_zone(layer_surface, height); zwlr_layer_surface_v1_set_exclusive_zone(layer_surface, height);
zwlr_layer_surface_v1_set_size(layer_surface, width, height); zwlr_layer_surface_v1_set_size(layer_surface, width, height);
@ -163,7 +163,7 @@ auto waybar::Bar::setupCss() -> void
void waybar::Bar::getModules(const Factory& factory, const std::string& pos) void waybar::Bar::getModules(const Factory& factory, const std::string& pos)
{ {
if (config_[pos]) { if (config_[pos].isArray()) {
for (const auto &name : config_[pos]) { for (const auto &name : config_[pos]) {
try { try {
auto module = factory.makeModule(name.asString()); auto module = factory.makeModule(name.asString());

View File

@ -4,7 +4,7 @@ waybar::modules::Battery::Battery(const Json::Value& config)
: ALabel(config, "{capacity}%") : ALabel(config, "{capacity}%")
{ {
try { try {
if (config_["bat"]) { if (config_["bat"].isString()) {
auto dir = data_dir_ / config_["bat"].asString(); auto dir = data_dir_ / config_["bat"].asString();
if (fs::is_directory(dir) && fs::exists(dir / "capacity") if (fs::is_directory(dir) && fs::exists(dir / "capacity")
&& fs::exists(dir / "status") && fs::exists(dir / "uevent")) { && fs::exists(dir / "status") && fs::exists(dir / "uevent")) {
@ -22,7 +22,7 @@ waybar::modules::Battery::Battery(const Json::Value& config)
throw std::runtime_error(e.what()); throw std::runtime_error(e.what());
} }
if (batteries_.empty()) { if (batteries_.empty()) {
if (config_["bat"]) { if (config_["bat"].isString()) {
throw std::runtime_error("No battery named " + config_["bat"].asString()); throw std::runtime_error("No battery named " + config_["bat"].asString());
} }
throw std::runtime_error("No batteries."); throw std::runtime_error("No batteries.");
@ -47,7 +47,7 @@ void waybar::modules::Battery::worker()
{ {
// Trigger first values // Trigger first values
update(); update();
uint32_t interval = config_["interval"] ? config_["interval"].asUInt() : 60; uint32_t interval = config_["interval"].isUInt() ? config_["interval"].asUInt() : 60;
threadTimer_ = [this, interval] { threadTimer_ = [this, interval] {
thread_.sleep_for(chrono::seconds(interval)); thread_.sleep_for(chrono::seconds(interval));
dp.emit(); dp.emit();
@ -88,7 +88,7 @@ auto waybar::modules::Battery::update() -> void
} else { } else {
label_.get_style_context()->remove_class("charging"); label_.get_style_context()->remove_class("charging");
} }
auto critical = config_["critical"] ? config_["critical"].asUInt() : 15; auto critical = config_["critical"].isUInt() ? config_["critical"].asUInt() : 15;
if (capacity <= critical && !charging) { if (capacity <= critical && !charging) {
label_.get_style_context()->add_class("warning"); label_.get_style_context()->add_class("warning");
} else { } else {

View File

@ -4,7 +4,7 @@ waybar::modules::Clock::Clock(const Json::Value& config)
: ALabel(config, "{:%H:%M}") : ALabel(config, "{:%H:%M}")
{ {
label_.set_name("clock"); label_.set_name("clock");
uint32_t interval = config_["interval"] ? config_["interval"].asUInt() : 60; uint32_t interval = config_["interval"].isUInt() ? config_["interval"].asUInt() : 60;
thread_ = [this, interval] { thread_ = [this, interval] {
auto now = waybar::chrono::clock::now(); auto now = waybar::chrono::clock::now();
dp.emit(); dp.emit();

View File

@ -4,7 +4,7 @@ waybar::modules::Cpu::Cpu(const Json::Value& config)
: ALabel(config, "{}%") : ALabel(config, "{}%")
{ {
label_.set_name("cpu"); label_.set_name("cpu");
uint32_t interval = config_["interval"] ? config_["interval"].asUInt() : 10; uint32_t interval = config_["interval"].isUInt() ? config_["interval"].asUInt() : 10;
thread_ = [this, interval] { thread_ = [this, interval] {
dp.emit(); dp.emit();
thread_.sleep_for(chrono::seconds(interval)); thread_.sleep_for(chrono::seconds(interval));

View File

@ -4,10 +4,10 @@ waybar::modules::Custom::Custom(const std::string name,
const Json::Value& config) const Json::Value& config)
: ALabel(config, "{}"), name_(name) : ALabel(config, "{}"), name_(name)
{ {
if (!config_["exec"]) { if (!config_["exec"].isString()) {
throw std::runtime_error(name_ + " has no exec path."); throw std::runtime_error(name_ + " has no exec path.");
} }
if (config_["interval"]) { if (config_["interval"].isUInt()) {
delayWorker(); delayWorker();
} else { } else {
continuousWorker(); continuousWorker();
@ -19,7 +19,7 @@ void waybar::modules::Custom::delayWorker()
auto interval = config_["interval"].asUInt(); auto interval = config_["interval"].asUInt();
thread_ = [this, interval] { thread_ = [this, interval] {
bool can_update = true; bool can_update = true;
if (config_["exec-if"]) { if (config_["exec-if"].isString()) {
auto res = waybar::util::command::exec(config_["exec-if"].asString()); auto res = waybar::util::command::exec(config_["exec-if"].asString());
if (res.exit_code != 0) { if (res.exit_code != 0) {
can_update = false; can_update = false;

View File

@ -4,7 +4,7 @@ waybar::modules::Memory::Memory(const Json::Value& config)
: ALabel(config, "{}%") : ALabel(config, "{}%")
{ {
label_.set_name("memory"); label_.set_name("memory");
uint32_t interval = config_["interval"] ? config_["interval"].asUInt() : 30; uint32_t interval = config_["interval"].isUInt() ? config_["interval"].asUInt() : 30;
thread_ = [this, interval] { thread_ = [this, interval] {
dp.emit(); dp.emit();
thread_.sleep_for(chrono::seconds(interval)); thread_.sleep_for(chrono::seconds(interval));

View File

@ -14,7 +14,7 @@ waybar::modules::Network::Network(const Json::Value& config)
sizeof(nladdr_)) != 0) { sizeof(nladdr_)) != 0) {
throw std::runtime_error("Can't bind network socket"); throw std::runtime_error("Can't bind network socket");
} }
if (config_["interface"]) { if (config_["interface"].isString()) {
ifid_ = if_nametoindex(config_["interface"].asCString()); ifid_ = if_nametoindex(config_["interface"].asCString());
ifname_ = config_["interface"].asString(); ifname_ = config_["interface"].asString();
if (ifid_ <= 0) { if (ifid_ <= 0) {
@ -56,7 +56,7 @@ waybar::modules::Network::Network(const Json::Value& config)
} }
} }
} }
if (ifid_ <= 0 && !config_["interface"]) { if (ifid_ <= 0 && !config_["interface"].isString()) {
// Need to wait before get external interface // Need to wait before get external interface
thread_.sleep_for(std::chrono::seconds(1)); thread_.sleep_for(std::chrono::seconds(1));
ifid_ = getExternalInterface(); ifid_ = getExternalInterface();
@ -84,15 +84,15 @@ auto waybar::modules::Network::update() -> void
{ {
auto format = format_; auto format = format_;
if (ifid_ <= 0) { if (ifid_ <= 0) {
format = config_["format-disconnected"] format = config_["format-disconnected"].isString()
? config_["format-disconnected"].asString() : format; ? config_["format-disconnected"].asString() : format;
label_.get_style_context()->add_class("disconnected"); label_.get_style_context()->add_class("disconnected");
} else { } else {
if (essid_.empty()) { if (essid_.empty()) {
format = config_["format-ethernet"] format = config_["format-ethernet"].isString()
? config_["format-ethernet"].asString() : format; ? config_["format-ethernet"].asString() : format;
} else { } else {
format = config_["format-wifi"] format = config_["format-wifi"].isString()
? config_["format-wifi"].asString() : format; ? config_["format-wifi"].asString() : format;
} }
label_.get_style_context()->remove_class("disconnected"); label_.get_style_context()->remove_class("disconnected");

View File

@ -131,10 +131,10 @@ auto waybar::modules::Pulseaudio::update() -> void
auto format = format_; auto format = format_;
if (muted_) { if (muted_) {
format = format =
config_["format-muted"] ? config_["format-muted"].asString() : format; config_["format-muted"].isString() ? config_["format-muted"].asString() : format;
label_.get_style_context()->add_class("muted"); label_.get_style_context()->add_class("muted");
} else if (port_name_.find("a2dp_sink") != std::string::npos) { } else if (port_name_.find("a2dp_sink") != std::string::npos) {
format = config_["format-bluetooth"] format = config_["format-bluetooth"].isString()
? config_["format-bluetooth"].asString() : format; ? config_["format-bluetooth"].asString() : format;
label_.get_style_context()->add_class("bluetooth"); label_.get_style_context()->add_class("bluetooth");
} else { } else {

View File

@ -79,7 +79,7 @@ auto waybar::modules::sway::Workspaces::update() -> void
box_.reorder_child(button, node["num"].asInt()); box_.reorder_child(button, node["num"].asInt());
} }
auto icon = getIcon(node["name"].asString(), node); auto icon = getIcon(node["name"].asString(), node);
if (config_["format"]) { if (config_["format"].isString()) {
auto format = config_["format"].asString(); auto format = config_["format"].asString();
button.set_label(fmt::format(format, fmt::arg("icon", icon), button.set_label(fmt::format(format, fmt::arg("icon", icon),
fmt::arg("name", node["name"].asString()), fmt::arg("name", node["name"].asString()),
@ -98,7 +98,7 @@ auto waybar::modules::sway::Workspaces::update() -> void
void waybar::modules::sway::Workspaces::addWorkspace(Json::Value node) void waybar::modules::sway::Workspaces::addWorkspace(Json::Value node)
{ {
auto icon = getIcon(node["name"].asString(), node); auto icon = getIcon(node["name"].asString(), node);
auto format = config_["format"] auto format = config_["format"].isString()
? fmt::format(config_["format"].asString(), fmt::arg("icon", icon), ? fmt::format(config_["format"].asString(), fmt::arg("icon", icon),
fmt::arg("name", node["name"].asString()), fmt::arg("name", node["name"].asString()),
fmt::arg("index", node["num"].asString())) fmt::arg("index", node["num"].asString()))
@ -141,10 +141,10 @@ std::string waybar::modules::sway::Workspaces::getIcon(std::string name,
name, "urgent", "focused", "visible", "default"}; name, "urgent", "focused", "visible", "default"};
for (auto const& key : keys) { for (auto const& key : keys) {
if (key == "focused" || key == "visible" || key == "urgent") { if (key == "focused" || key == "visible" || key == "urgent") {
if (config_["format-icons"][key] && node[key].asBool()) { if (config_["format-icons"][key].isString() && node[key].asBool()) {
return config_["format-icons"][key].asString(); return config_["format-icons"][key].asString();
} }
} else if (config_["format-icons"][key]) { } else if (config_["format-icons"][key].isString()) {
return config_["format-icons"][key].asString(); return config_["format-icons"][key].asString();
} }
} }