diff options
-rw-r--r-- | include/datetime/exporter.h | 26 | ||||
-rw-r--r-- | src/exporter.cpp | 170 | ||||
-rw-r--r-- | src/main.cpp | 2 | ||||
-rw-r--r-- | tests/test-exporter.cpp | 2 |
4 files changed, 112 insertions, 88 deletions
diff --git a/include/datetime/exporter.h b/include/datetime/exporter.h index c228cc1..8ae70b1 100644 --- a/include/datetime/exporter.h +++ b/include/datetime/exporter.h @@ -25,8 +25,6 @@ #include <core/signal.h> -#include <gio/gio.h> // GActionGroup - #include <memory> // std::shared_ptr #include <vector> @@ -40,31 +38,21 @@ namespace datetime { class Exporter { public: - Exporter() =default; + Exporter(); ~Exporter(); - core::Signal<> name_lost; + core::Signal<>& name_lost(); void publish(const std::shared_ptr<Actions>& actions, const std::vector<std::shared_ptr<Menu>>& menus); private: - static void on_bus_acquired(GDBusConnection*, const gchar *name, gpointer gthis); - void on_bus_acquired(GDBusConnection*, const gchar *name); - - static void on_name_lost(GDBusConnection*, const gchar *name, gpointer gthis); - void on_name_lost(GDBusConnection*, const gchar *name); - - std::set<guint> m_exported_menu_ids; - guint m_own_id = 0; - guint m_exported_actions_id = 0; - GDBusConnection * m_dbus_connection = nullptr; - std::shared_ptr<Actions> m_actions; - std::vector<std::shared_ptr<Menu>> m_menus; + class Impl; + std::unique_ptr<Impl> p; - // we've got raw pointers and gsignal tags in here, so disable copying - Exporter(const Exporter&) =delete; - Exporter& operator=(const Exporter&) =delete; + // disable copying + Exporter(const Exporter&) =delete; + Exporter& operator=(const Exporter&) =delete; }; } // namespace datetime diff --git a/src/exporter.cpp b/src/exporter.cpp index ccd6e5c..a4919e9 100644 --- a/src/exporter.cpp +++ b/src/exporter.cpp @@ -31,108 +31,144 @@ namespace datetime { **** ***/ -Exporter::~Exporter() +class Exporter::Impl { - if (m_dbus_connection != nullptr) - { - for(auto& id : m_exported_menu_ids) - g_dbus_connection_unexport_menu_model(m_dbus_connection, id); +public: - if (m_exported_actions_id) - g_dbus_connection_unexport_action_group(m_dbus_connection, m_exported_actions_id); + Impl (Exporter* owner): + m_owner(owner) + { } - if (m_own_id) - g_bus_unown_name(m_own_id); + ~Impl() + { + if (m_bus != nullptr) + { + for(auto& id : m_exported_menu_ids) + g_dbus_connection_unexport_menu_model(m_bus, id); - g_clear_object(&m_dbus_connection); -} + if (m_exported_actions_id) + g_dbus_connection_unexport_action_group(m_bus, m_exported_actions_id); + } -/*** -**** -***/ + if (m_own_id) + g_bus_unown_name(m_own_id); -void -Exporter::on_bus_acquired(GDBusConnection* connection, const gchar* name, gpointer gthis) -{ - g_debug("bus acquired: %s", name); - static_cast<Exporter*>(gthis)->on_bus_acquired(connection, name); -} + g_clear_object(&m_bus); + } -void -Exporter::on_bus_acquired(GDBusConnection* connection, const gchar* /*name*/) -{ - m_dbus_connection = static_cast<GDBusConnection*>(g_object_ref(G_OBJECT(connection))); - - // export the actions - GError * error = nullptr; - const auto id = g_dbus_connection_export_action_group(m_dbus_connection, - BUS_PATH, - m_actions->action_group(), - &error); - if (id) + core::Signal<> name_lost; + + void publish(const std::shared_ptr<Actions>& actions, + const std::vector<std::shared_ptr<Menu>>& menus) { - m_exported_actions_id = id; + m_actions = actions; + m_menus = menus; + m_own_id = g_bus_own_name(G_BUS_TYPE_SESSION, + BUS_NAME, + G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT, + on_bus_acquired, + nullptr, + on_name_lost, + this, + nullptr); } - else + +private: + + static void on_bus_acquired(GDBusConnection* connection, + const gchar* name, + gpointer gthis) { - g_warning("cannot export action group: %s", error->message); - g_clear_error(&error); + g_debug("bus acquired: %s", name); + static_cast<Impl*>(gthis)->on_bus_acquired(connection, name); } - // export the menus - for(auto& menu : m_menus) + void on_bus_acquired(GDBusConnection* bus, const gchar* /*name*/) { - const auto path = std::string(BUS_PATH) + "/" + menu->name(); - const auto id = g_dbus_connection_export_menu_model(m_dbus_connection, path.c_str(), menu->menu_model(), &error); + m_bus = static_cast<GDBusConnection*>(g_object_ref(G_OBJECT(bus))); + + // export the actions + GError * error = nullptr; + const auto id = g_dbus_connection_export_action_group(m_bus, + BUS_PATH, + m_actions->action_group(), + &error); if (id) { - m_exported_menu_ids.insert(id); + m_exported_actions_id = id; } else { - if (error != nullptr) - g_warning("cannot export %s menu: %s", menu->name().c_str(), error->message); + g_warning("cannot export action group: %s", error->message); g_clear_error(&error); } + + // export the menus + for(auto& menu : m_menus) + { + const auto path = std::string(BUS_PATH) + "/" + menu->name(); + const auto id = g_dbus_connection_export_menu_model(m_bus, path.c_str(), menu->menu_model(), &error); + if (id) + { + m_exported_menu_ids.insert(id); + } + else + { + if (error != nullptr) + g_warning("cannot export %s menu: %s", menu->name().c_str(), error->message); + g_clear_error(&error); + } + } } -} + + /*** + **** + ***/ + + static void on_name_lost(GDBusConnection*, const gchar* name, gpointer gthis) + { + g_debug("name lost: %s", name); + static_cast<Impl*>(gthis)->name_lost(); + } + + /*** + **** + ***/ + + Exporter const * m_owner; + std::set<guint> m_exported_menu_ids; + guint m_own_id = 0; + guint m_exported_actions_id = 0; + GDBusConnection* m_bus = nullptr; + std::shared_ptr<Actions> m_actions; + std::vector<std::shared_ptr<Menu>> m_menus; +}; + /*** **** ***/ -void -Exporter::on_name_lost(GDBusConnection* connection, const gchar* name, gpointer gthis) +Exporter::Exporter(): + p(new Impl(this)) { - g_debug("name lost: %s", name); - static_cast<Exporter*>(gthis)->on_name_lost(connection, name); } -void -Exporter::on_name_lost(GDBusConnection* /*connection*/, const gchar* /*name*/) + +Exporter::~Exporter() { - name_lost(); } -/*** -**** -***/ +core::Signal<>& Exporter::name_lost() +{ + return p->name_lost; +} -void -Exporter::publish(const std::shared_ptr<Actions>& actions, - const std::vector<std::shared_ptr<Menu>>& menus) +void Exporter::publish(const std::shared_ptr<Actions>& actions, + const std::vector<std::shared_ptr<Menu>>& menus) { - m_actions = actions; - m_menus = menus; - m_own_id = g_bus_own_name(G_BUS_TYPE_SESSION, - BUS_NAME, - G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT, - on_bus_acquired, - nullptr, - on_name_lost, - this, - nullptr); + p->publish(actions, menus); } /*** diff --git a/src/main.cpp b/src/main.cpp index 1940eb6..04845ce 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -164,7 +164,7 @@ main(int /*argc*/, char** /*argv*/) // export them & run until we lose the busname auto loop = g_main_loop_new(nullptr, false); Exporter exporter; - exporter.name_lost.connect([loop](){ + exporter.name_lost().connect([loop](){ g_message("%s exiting; failed/lost bus ownership", GETTEXT_PACKAGE); g_main_loop_quit(loop); }); diff --git a/tests/test-exporter.cpp b/tests/test-exporter.cpp index a255ef9..4fb2c54 100644 --- a/tests/test-exporter.cpp +++ b/tests/test-exporter.cpp @@ -122,7 +122,7 @@ TEST_F(ExporterFixture, Publish) // try closing the connection prematurely // to test Exporter's name-lost signal bool name_lost = false; - exporter.name_lost.connect([this,&name_lost](){ + exporter.name_lost().connect([this,&name_lost](){ name_lost = true; g_main_loop_quit(loop); }); |