diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9dc53d4..469379a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,5 +1,5 @@ find_package(PkgConfig) -pkg_check_modules(GDBUS REQUIRED gio-2.0 dbus-1 glibmm-2.4) +pkg_check_modules(GDBUS REQUIRED gio-2.0 dbus-1 glibmm-2.4 giomm-2.4) add_executable(slack-timedate slack-timedate.cpp slack-timedate.h diff --git a/src/slack-timedate.cpp b/src/slack-timedate.cpp index 45b8e50..f5a476e 100644 --- a/src/slack-timedate.cpp +++ b/src/slack-timedate.cpp @@ -114,41 +114,60 @@ static GVariant *slack_get_property (GDBusConnection *connection, const gchar *s return NULL; } -static void on_timedate_acquired (GDBusConnection *connection, const gchar *name, gpointer user_data) { - guint registration_id; - GDBusNodeInfo *introspection_data; - GError *slack_err; - const GDBusInterfaceVTable interface_vtable = { - slack_method_call, - slack_get_property, - NULL - }; +static void on_timedate_acquired(const Glib::RefPtr& connection, + const Glib::ustring& name, gpointer user_data) +{ + Glib::RefPtr introspection_data; + static const auto interface_vtable = Gio::DBus::InterfaceVTable( + [user_data](const Glib::RefPtr& connection, + const Glib::ustring& sender, + const Glib::ustring& obj_path, + const Glib::ustring& interface_name, + const Glib::ustring& method_name, + const Glib::VariantContainerBase& parameters, + const Glib::RefPtr& invocation) { - slack_err = NULL; - introspection_data = g_dbus_node_info_new_for_xml (INTROSPECTION_XML, &slack_err); - if (introspection_data == NULL) - g_error ("Failed to parse D-Bus introspection XML: %s\n", slack_err->message); + auto parameters_copy = parameters.gobj_copy(); + slack_method_call(connection->gobj(), sender.data(), obj_path.data(), interface_name.data(), + method_name.data(), parameters_copy, invocation->gobj(), user_data); + g_free(parameters_copy); + }, + [user_data](Glib::VariantBase& result, + const Glib::RefPtr& connection, + const Glib::ustring& sender, + const Glib::ustring& obj_path, + const Glib::ustring& interface_name, + const Glib::ustring& prop_name) { - slack_err = NULL; - registration_id = g_dbus_connection_register_object (connection, - BUS_PATH, - introspection_data->interfaces[0], - &interface_vtable, - user_data, - NULL, - &slack_err); + auto property_result = slack_get_property(connection->gobj(), sender.data(), obj_path.data(), + interface_name.data(), prop_name.data(), nullptr, user_data); + result.init(property_result, true); + } + ); - if (registration_id <= 0) { - g_critical ("Failed to register callbacks for the exported object with the D-Bus interface: %s\n", slack_err->message); - g_error_free (slack_err); - } - - g_dbus_node_info_unref (introspection_data); + try + { + introspection_data = Gio::DBus::NodeInfo::create_for_xml(INTROSPECTION_XML); + } + catch (Glib::Error& slack_err) + { + g_error("Failed to parse D-Bus introspection XML: %s\n", slack_err.what().data()); + } + try + { + guint registration_id = connection->register_object(BUS_PATH, + introspection_data->lookup_interface(), interface_vtable); + } + catch (Glib::Error& slack_err) + { + g_critical("Failed to register callbacks for the exported object with the D-Bus interface: %s\n", + slack_err.what().data()); + } } -static void on_timedate_lost (GDBusConnection *connection, const gchar *name, gpointer user_data) { - g_warning ("Failed to acquire the service %s.\n", name); - exit (1); +static void on_timedate_lost(const Glib::RefPtr& connection, const Glib::ustring& name) { + g_warning("Failed to acquire the service %s.\n", name.data()); + exit(1); } gboolean timeout_callback (Glib::RefPtr loop2quit) { @@ -158,21 +177,21 @@ gboolean timeout_callback (Glib::RefPtr loop2quit) { int main(int argc, char **argv) { - auto timedate1 = new dlackware::timedate::timedate1(); - guint owner_id = g_bus_own_name (G_BUS_TYPE_SYSTEM, - BUS_NAME, - static_cast(G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT | G_BUS_NAME_OWNER_FLAGS_REPLACE), - on_timedate_acquired, - NULL, - on_timedate_lost, - timedate1, - [](void *memory) { delete reinterpret_cast(memory); }); + Gio::init(); + auto timedate1 = std::make_unique(); + + guint owner_id = Gio::DBus::own_name(Gio::DBus::BUS_TYPE_SYSTEM, BUS_NAME, + [&timedate1](const Glib::RefPtr& connection, const Glib::ustring& name) { + on_timedate_acquired(connection, name, timedate1.get()); + }, Gio::DBus::SlotNameAcquired(), &on_timedate_lost, + Gio::DBus::BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT | Gio::DBus::BUS_NAME_OWNER_FLAGS_REPLACE); + Glib::RefPtr loop = Glib::MainLoop::create(false); Glib::signal_timeout().connect_seconds(sigc::bind(&timeout_callback, loop), DEFAULT_EXIT_SEC); loop->run(); - g_bus_unown_name (owner_id); + Gio::DBus::unown_name(owner_id); return EXIT_SUCCESS; } diff --git a/src/slack-timedate.h b/src/slack-timedate.h index 2f8229f..1c24aa1 100644 --- a/src/slack-timedate.h +++ b/src/slack-timedate.h @@ -18,6 +18,7 @@ #include #include +#include #include #define BUS_NAME "org.freedesktop.timedate1"