From ffadd5c1a74b2e1e81bf0dac9ac9eed6037acb8c Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 24 May 2019 09:49:09 +0200 Subject: [PATCH] refactor: avoid useless class vars --- include/client.hpp | 15 ++++++------- src/client.cpp | 54 ++++++++++++++++++++++++---------------------- 2 files changed, 35 insertions(+), 34 deletions(-) diff --git a/include/client.hpp b/include/client.hpp index 5ba251cd..715ae586 100644 --- a/include/client.hpp +++ b/include/client.hpp @@ -26,14 +26,15 @@ class Client { private: Client() = default; - void setupConfigs(const std::string &config, const std::string &style); - void bindInterfaces(); - const std::string getValidPath(const std::vector &paths); + std::tuple getConfigs(const std::string &config, + const std::string &style) const; + void bindInterfaces(); + const std::string getValidPath(const std::vector &paths) const; void handleOutput(std::unique_ptr &output); bool isValidOutput(const Json::Value &config, std::unique_ptr &output); - auto setupConfig() -> void; - auto setupCss() -> void; - std::unique_ptr& getOutput(uint32_t wl_name); + auto setupConfig(const std::string &config_file) -> void; + auto setupCss(const std::string &css_file) -> void; + std::unique_ptr &getOutput(uint32_t wl_name); std::vector getOutputConfigs(std::unique_ptr &output); static void handleGlobal(void *data, struct wl_registry *registry, uint32_t name, @@ -46,8 +47,6 @@ class Client { static void handleDescription(void *, struct zxdg_output_v1 *, const char *); Json::Value config_; - std::string css_file_; - std::string config_file_; Glib::RefPtr style_context_; Glib::RefPtr css_provider_; std::vector> outputs_; diff --git a/src/client.cpp b/src/client.cpp index f5d65e07..b103bf78 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -10,7 +10,7 @@ waybar::Client *waybar::Client::inst() { return c; } -const std::string waybar::Client::getValidPath(const std::vector &paths) { +const std::string waybar::Client::getValidPath(const std::vector &paths) const { wordexp_t p; for (const std::string &path : paths) { @@ -172,31 +172,33 @@ void waybar::Client::handleDescription(void * /*data*/, struct zxdg_output_v1 * // Nothing here } -void waybar::Client::setupConfigs(const std::string &config, const std::string &style) { - config_file_ = config.empty() ? getValidPath({ - "$XDG_CONFIG_HOME/waybar/config", - "$HOME/.config/waybar/config", - "$HOME/waybar/config", - "/etc/xdg/waybar/config", - "./resources/config", +std::tuple waybar::Client::getConfigs( + const std::string &config, const std::string &style) const { + auto config_file = config.empty() ? getValidPath({ + "$XDG_CONFIG_HOME/waybar/config", + "$HOME/.config/waybar/config", + "$HOME/waybar/config", + "/etc/xdg/waybar/config", + "./resources/config", + }) + : config; + auto css_file = style.empty() ? getValidPath({ + "$XDG_CONFIG_HOME/waybar/style.css", + "$HOME/.config/waybar/style.css", + "$HOME/waybar/style.css", + "/etc/xdg/waybar/style.css", + "./resources/style.css", }) - : config; - css_file_ = style.empty() ? getValidPath({ - "$XDG_CONFIG_HOME/waybar/style.css", - "$HOME/.config/waybar/style.css", - "$HOME/waybar/style.css", - "/etc/xdg/waybar/style.css", - "./resources/style.css", - }) - : style; - if (css_file_.empty() || config_file_.empty()) { + : style; + if (css_file.empty() || config_file.empty()) { throw std::runtime_error("Missing required resources files"); } - spdlog::info("Resources files: {}, {}", config_file_, css_file_); + spdlog::info("Resources files: {}, {}", config_file, css_file); + return {config_file, css_file}; } -auto waybar::Client::setupConfig() -> void { - std::ifstream file(config_file_); +auto waybar::Client::setupConfig(const std::string &config_file) -> void { + std::ifstream file(config_file); if (!file.is_open()) { throw std::runtime_error("Can't open config file"); } @@ -205,12 +207,12 @@ auto waybar::Client::setupConfig() -> void { config_ = parser.parse(str); } -auto waybar::Client::setupCss() -> void { +auto waybar::Client::setupCss(const std::string &css_file) -> void { css_provider_ = Gtk::CssProvider::create(); style_context_ = Gtk::StyleContext::create(); // Load our css file, wherever that may be hiding - if (!css_provider_->load_from_path(css_file_)) { + if (!css_provider_->load_from_path(css_file)) { throw std::runtime_error("Can't open style file"); } } @@ -268,9 +270,9 @@ int waybar::Client::main(int argc, char *argv[]) { throw std::runtime_error("Bar need to run under Wayland"); } wl_display = gdk_wayland_display_get_wl_display(gdk_display->gobj()); - setupConfigs(config, style); - setupConfig(); - setupCss(); + auto [config_file, css_file] = getConfigs(config, style); + setupConfig(config_file); + setupCss(css_file); bindInterfaces(); gtk_app->hold(); gtk_app->run();