Skip to content

Commit

Permalink
modules: use scope_exit for deletion to make code more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
taminob committed Oct 20, 2023
1 parent 7bb0b70 commit be8ab85
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
7 changes: 6 additions & 1 deletion src/modules/bluetooth.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "modules/bluetooth.hpp"
#include "util/scope_guard.hpp"

#include <fmt/format.h>
#include <spdlog/spdlog.h>
Expand All @@ -12,14 +13,18 @@ using GDBusManager = std::unique_ptr<GDBusObjectManager, void (*)(GDBusObjectMan

auto generateManager() -> GDBusManager {
GError* error = nullptr;
waybar::util::scope_guard error_deleter([error]() {
if (error) {
g_error_free(error);
}
});
GDBusObjectManager* manager = g_dbus_object_manager_client_new_for_bus_sync(
G_BUS_TYPE_SYSTEM,
GDBusObjectManagerClientFlags::G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_DO_NOT_AUTO_START,
"org.bluez", "/", NULL, NULL, NULL, NULL, &error);

if (error) {
spdlog::error("g_dbus_object_manager_client_new_for_bus_sync() failed: {}", error->message);
g_error_free(error);
}

auto destructor = [](GDBusObjectManager* manager) {
Expand Down
15 changes: 11 additions & 4 deletions src/modules/sni/host.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "modules/sni/host.hpp"
#include "util/scope_guard.hpp"

#include <spdlog/spdlog.h>

Expand Down Expand Up @@ -57,17 +58,20 @@ void Host::nameVanished(const Glib::RefPtr<Gio::DBus::Connection>& conn, const G

void Host::proxyReady(GObject* src, GAsyncResult* res, gpointer data) {
GError* error = nullptr;
waybar::util::scope_guard error_deleter([error]() {
if (error != nullptr) {
g_error_free(error);
}
});
SnWatcher* watcher = sn_watcher_proxy_new_finish(res, &error);
if (g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
spdlog::error("Host: {}", error->message);
g_error_free(error);
return;
}
auto host = static_cast<SNI::Host*>(data);
host->watcher_ = watcher;
if (error != nullptr) {
spdlog::error("Host: {}", error->message);
g_error_free(error);
return;
}
sn_watcher_call_register_host(host->watcher_, host->object_path_.c_str(), host->cancellable_,
Expand All @@ -76,16 +80,19 @@ void Host::proxyReady(GObject* src, GAsyncResult* res, gpointer data) {

void Host::registerHost(GObject* src, GAsyncResult* res, gpointer data) {
GError* error = nullptr;
waybar::util::scope_guard error_deleter([error]() {
if (error != nullptr) {
g_error_free(error);
}
});
sn_watcher_call_register_host_finish(SN_WATCHER(src), res, &error);
if (g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
spdlog::error("Host: {}", error->message);
g_error_free(error);
return;
}
auto host = static_cast<SNI::Host*>(data);
if (error != nullptr) {
spdlog::error("Host: {}", error->message);
g_error_free(error);
return;
}
g_signal_connect(host->watcher_, "item-registered", G_CALLBACK(&Host::itemRegistered), data);
Expand Down
7 changes: 6 additions & 1 deletion src/modules/sni/watcher.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "modules/sni/watcher.hpp"
#include "util/scope_guard.hpp"

#include <spdlog/spdlog.h>

Expand Down Expand Up @@ -29,14 +30,18 @@ Watcher::~Watcher() {

void Watcher::busAcquired(const Glib::RefPtr<Gio::DBus::Connection>& conn, Glib::ustring name) {
GError* error = nullptr;
waybar::util::scope_guard error_deleter([error]() {
if (error) {
g_error_free(error);
}
});
g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(watcher_), conn->gobj(),
"/StatusNotifierWatcher", &error);
if (error != nullptr) {
// Don't print an error when a watcher is already present
if (error->code != 2) {
spdlog::error("Watcher: {}", error->message);
}
g_error_free(error);
return;
}
g_signal_connect_swapped(watcher_, "handle-register-item",
Expand Down

0 comments on commit be8ab85

Please sign in to comment.